0
# x = 1
# x = 0

if x:
    print(f'x = {x}, and therefore it\'s truthy')
else:
    print(f"x = {x}, and therefore it's falsey")

Hi everyone, I am a little confused with this exercise. The code doesn't have any explanation as when the system should print truthy or falsey yet it knows when to print it. Why is that?

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • What is the output you are seeing? Does that give you any clues at all about what is happening? – quamrana Feb 24 '22 at 17:34
  • 1
    `if x` check for whether `x` is truthy before going to a following statement (in this case `else:`) – ti7 Feb 24 '22 at 17:38
  • `print(f'x = {x}, and therefore it\'s {bool(x)}y')` Maybe this will help you understand. – OneMadGypsy Feb 24 '22 at 17:39
  • @MichaelGuidry thank you I understand how to tell if it is a true or false in a boolean I just wondered how the system knew what to do. In your example it is clear by the way you coded it what the system needs to print. But in my example, I have given it 1 or 0. In the print it has the format x = {whatever value I decide to uncomment} and then a string. No other instructions have been given. Why it isn't just giving me the first output I ask. If I uncomment x = 0, why it doesn't default to the first line of code x = 0 and therefore it's a truthy? How does it know it falls under else? – Francisca Feb 24 '22 at 17:58
  • I'm confused about what you are asking.. if x is truthy (1 or any non-zero value), the if will be true so it will be executed and not the else, and if x is zero it's falsy so the if doesn't execute and the else is executed. What are you saying by "the first output I ask" ? – Peterrabbit Feb 24 '22 at 18:08

4 Answers4

2

I think it's just a way to show that 0 is interpreted as False in a boolean expression like a comparison or an if etc, and 1 (or any other non-zero number) is interpreted as True.

Edit: and like said in some comments, the fact that a value is "truthy" doesn't mean that is strictly equal to True for example:

print(1 == True) # True
print(0 == False) # True
print(2 == True) # False
print(not not 2) # True
print(not 0) # True
print(not not 0) # False
print(0 and True) # False
print(2 and False) # False
print(2 and True) # True
print(0 or 2) # True
# etc ...
Peterrabbit
  • 2,166
  • 1
  • 3
  • 19
0

If x is an int and has a value of zero it is equal to False and therefore falsey. If x is an int and has a value of 1 it is equal to True and is therefore truthy. However, if x is an int and is neither 0 nor 1 it is truthy but not equal to True. Therefore, when x is of type int, if x: can be interpreted as 'if x is non-zero'

DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

Its all in the if x: line.

To execute this line, I like to think that python translates it to:

if bool(x):

because bool() will only ever return True or False, then the first option is only ever taken when the result is True.

See this answer for details of a comprehensive list of values which are considered Falsey.

quamrana
  • 37,849
  • 12
  • 53
  • 71
-1

the funny part is if you use

x = 0.00000000000001

it will also consider as true. cause it is not actually equal to zero. in math we consider this as zero for easy calculation. but computer dont do this cause this can create big problem in calculation if we consider a big picture. Example:

y = x ** x
print(y)

so if we consider x as zero, y will be also zero. but its not. i hope you enjoyed to learn why 0.000000000001 is not equal to zero in computer.Thanks.