#🔒 #Beginner here – want to learn Python and make friends👀
56 messages · Page 1 of 1 (latest)
@wary lark
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
#Beginner here – want to learn Python and make friends👀
#❓|how-to-get-help @wary lark
Do you have any particular question at this time?
I am confused about if-else statements. Can anyone explain with an example?
Sure.
First, we need to understand that if and elif are looking at exactly one object each. Let's start by looking at if by itself.
!e ```py
if True:
print ("A")
if False:
print("B")```
:white_check_mark: Your 3.14 eval job has completed with return code 0.
A
First, the if looks at the object to its right. If that object is truthy, that if's block is run. We see that A is printed.
True is truthy.
Then we see the next if. It looks at False, which is falsy, and does not print B.
We will often use expressions to form the objects True and False by other means.
!e py print(1 + 1 == 2) print(3 + 4 == 9001)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | True
002 | False
!e py name = "Peter" print(name == "Peter") print(name == "Sally")
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | True
002 | False
and we can plug these expressions into if and elif
!e ```py
name = "Peter"
if name == "Peter":
print("A")
if name == "Sally":
print("B")```
:white_check_mark: Your 3.14 eval job has completed with return code 0.
A
elif stands for else, if
An if compound statement us comprised of...py if condition: "Exactly one if" elif some_other_condition: "Zero or more elifs" elif ...: ... # etc else: "Zero or one else"In that order. The first condition to be satisfied has its block run.
!e py if True: print("A") elif True: print("B") else: print("C")
:white_check_mark: Your 3.14 eval job has completed with return code 0.
A
!e py if False: print("A") elif True: print("B") else: print("C")
:white_check_mark: Your 3.14 eval job has completed with return code 0.
B
!e py if False: print("A") elif False: print("B") else: print("C")
:white_check_mark: Your 3.14 eval job has completed with return code 0.
C
Imagine a deck of cards with conditions. You draw from the top of the deck and stop when you hit a card with a satisfied condition.
That is an if/elif. Else is if you hit the bottom of the deck and exhausted your cards.
!e py if ...: ... "Writing other code here ends the if/elif/else." elif ...: # making this illegal ...
:x: Your 3.14 eval job has completed with return code 1.
001 | File [35m"/home/main.py"[0m, line [35m4[0m
002 | [1;31melif[0m ...: # making this illegal
003 | [1;31m^^^^[0m
004 | [1;35mSyntaxError[0m: [35minvalid syntax[0m
@wary lark Does this help clarify the matter?
@waxen thicket
"Got it! So if/elif checks conditions one by one from the top, and else is the final backup if nothing matches.
Correct.
!e ```py
if "abc":
print("A")
if "":
print("B")```Empty strings are falsy. Strings of nonzero length are truthy.
:white_check_mark: Your 3.14 eval job has completed with return code 0.
A
@wary lark yep true bro
!e ```py
if [123]:
print("A")
if []:
print("B")```Lists with things in them are truthy. Empty lists are falsy.
:white_check_mark: Your 3.14 eval job has completed with return code 0.
A
!e ```py
if -1:
print("A")
if 0:
print("B")
if 1:
print("C")```
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | A
002 | C
Nonzeroes are truthy. Zero is falsy.
while is related to if in that it, too, checks the truthiness of the object to its right.
while condition:
...```
and runs the indent or not, depending.
Except that it will run it on repeat while the condition is truthy at the head of the repeat.
Until such time as it is falsy.
@waxen thicket Clear! Non-zeros = truthy, zero = falsy. And while follows the same logic.
@waxen thicket Thanks for the explanation😊☺
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.