Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement
#1
Hello!
Thats a python code, and im a beginner

s='Hungry'
p='Im full'
r=7

if 8:
    print('Im hungry')
elif p:
    print('I didnt eat')
elif z:
    print('Im full')
I got

I'm hungry

Although 8 wasn't mention in the beginning I had "Im hungry" printed out, why is that?
Reply
#2
It is because after "if" a boolean expression is expected: something resulting in True or False. An integer can be converted to a boolean and if that happens a value of "0" is interpreted as False and any other value as True.
Reply
#3
Thank you a looooot.

What confuses me more is that I used s,p and r as conditions, so aren't we supposed to get an error if we use anything different then s,p and r?
s='Hungry'
p='Im full'
r=7
Reply
#4
No, because the values they refer to can be treated as True (non-zero ints, non-empty strings) or False. Further, collections like lists and dictionaries can also be treated like that. Given a list called values, say, the idiomatic way to test that it's non-empty is to write

if values:
    ...
rather than

if len(values) > 0:
    ...
for example.

Also, your variable names are quite meaningless. Try to use names that are descriptive, because they help people (you included!) to read and understand the code.
Reply
#5
Shocked 
Thanks a lot @ndc85430.
and what is
if len(values) > 0:
    ...

Im really new with python, please could you try to explain it simply than that and give me exmples. I would be very grateful.
Reply
#6
len is a function that returns the length of its argument. You might want to at least be aware that the documentation for functions and other things that come with Python can be found here. len, for example, can be found under section 2, "Built-in functions".
Reply
#7
(Feb-01-2020, 07:00 PM)el_bueno Wrote: Thank you a looooot.

What confuses me more is that I used s,p and r as conditions, so aren't we supposed to get an error if we use anything different then s,p and r?
s='Hungry'
p='Im full'
r=7

Still, I want to understand why aren't we having an error when we use 8?

So lets get this straight one and for all!

So If is influenced by False and True.

Which leads us to have two cases:

First case
1- We can use False and True after if
if True: or
if False:
Second case
2-we can use values
a-integers:
0 means False
Different than 0 means True
b-If we use strings:
They should be predefined in order to use them, when defined they means true and if they aren't defined they means False and we get an error

Am I getting it right?
Reply
#8
(Feb-01-2020, 08:25 PM)el_bueno Wrote: They should be predefined in order to use them, when defined they means true and if they aren't defined they means False and we get an error
Almost correct. When you use a variable that is not defined it is neither True nor False: you get an error:
>>> if a:
...     print(True)
... else:
...     print(False)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
If the variable is defined but it is empty string (or a zero integer) it means False:
>>> a = ""
>>> if a:
...     print(True)
... else:
...     print(False)
... 
False
If the variable is defined and has a value it is true:
>>> a = "Hello"
>>> if a:
...     print(True)
... else:
...     print(False)
... 
True
All these boolean side effects of variables stem from a long forgotten era of coding in machine language. In those days only 0 and 1 bits existed. 0 meant false. By coincidence 0 also meant 0. This is when programmers started to mis-use numbers as booleans thus obfuscating their programs. Now we have modern languages like Python and we do not have to rely on the side effects. So my advice is: use boolean variables if you mean to work with True and False. And else use string and int variables. This will make your program clear and easy to understand.
So I would advice to re-write your program like this.:
is_hungry   = False
is_noteaten = True
is_full     = False

if is_hungry:
    print('Im hungry')
elif is_noteaten:
    print("I didn't eat")
elif is_full:
    print('Im full')
else:
    print('I do not know what I feel')
Does this help you?
Reply
#9
Thank you a lot, really thank you!

Yes it is beyond helping.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020