#🔒 #Beginner here – want to learn Python and make friends👀

56 messages · Page 1 of 1 (latest)

wary lark
#

Hi everyone 👋
I’m new to Python and just started learning.
I want to improve my skills and make some coding friends here.
Looking forward to learning from you all 😊

tidal pendantBOT
#

@wary lark

Python help channel opened

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.

wary lark
#

#Beginner here – want to learn Python and make friends👀

bold copper
#

#❓|how-to-get-help @wary lark

waxen thicket
wary lark
#

I am confused about if-else statements. Can anyone explain with an example?

waxen thicket
#

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")```

tidal pendantBOT
waxen thicket
#

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)

tidal pendantBOT
waxen thicket
#

!e py name = "Peter" print(name == "Peter") print(name == "Sally")

tidal pendantBOT
waxen thicket
#

and we can plug these expressions into if and elif

#

!e ```py
name = "Peter"
if name == "Peter":
print("A")

if name == "Sally":
print("B")```

tidal pendantBOT
waxen thicket
#

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")

tidal pendantBOT
waxen thicket
#

!e py if False: print("A") elif True: print("B") else: print("C")

tidal pendantBOT
waxen thicket
#

!e py if False: print("A") elif False: print("B") else: print("C")

tidal pendantBOT
waxen thicket
#

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 ...

tidal pendantBOT
waxen thicket
#

@wary lark Does this help clarify the matter?

wary lark
#

@waxen thicket
"Got it! So if/elif checks conditions one by one from the top, and else is the final backup if nothing matches.

waxen thicket
#

Correct.

#

!e ```py
if "abc":
print("A")

if "":
print("B")```Empty strings are falsy. Strings of nonzero length are truthy.

tidal pendantBOT
golden grove
#

@wary lark yep true bro

waxen thicket
#

!e ```py
if [123]:
print("A")

if []:
print("B")```Lists with things in them are truthy. Empty lists are falsy.

tidal pendantBOT
waxen thicket
#

!e ```py
if -1:
print("A")

if 0:
print("B")

if 1:
print("C")```

tidal pendantBOT
waxen thicket
#

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.

wary lark
#

@waxen thicket Clear! Non-zeros = truthy, zero = falsy. And while follows the same logic.

waxen thicket
#

while being if on repeat.

#

It even has an else.

#
while ...:
    ...
else:
    ...```
wary lark
#

@waxen thicket Thanks for the explanation😊☺

tidal pendantBOT
#
Python help channel closed for inactivity

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.