-1

I have the below python code. I am unable to understand that how the result prints the x value as true, and the Z value as false. And a as 11 and b as 10.

x = (1 == True)  
y = (2 == False)  
z = (3 == True)  
a = True + 10  
b = False + 10  

print("x is", x)  
print("y is", y)  
print("z is", z)  
print("a:", a)  
print("b:", b)  
  • Hello, see this answer for details: "True and False are keywords and will always be equal to 1 and 0" https://stackoverflow.com/a/2764099/11547576 – neutrino_logic Sep 01 '20 at 03:29

2 Answers2

2

Let's see what True and False are made of

>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> bool.__bases__
(<class 'int'>,)

True and False are bool type which itself is a specialization of int. And they behave like the integers 1 and 0.

>>> int(True)
1
>>> int(False)
0

But unlike regular variables, you can't assign anything to them. That's because they are also python language Keywords and the compiler won't allow the names "True" and "False" to be variables.

>>> True = 10
  File "<stdin>", line 1
SyntaxError: cannot assign to True
>>> 

In your examples, True is 1 and False is 0, so

This...       is the same as...  resulting in...
1 == True     1 == 1             True
2 == False    2 == 1             False
3 == True     3 == 1             False  
True + 10     1 + 10             11  
False + 10    0 + 10             10 
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

In python, True = 1, False = 0, so x=(1==True) means x = True if 1==1 else False.

For z, z=(3==True) means z=(3==1). 3 doesn't equal 1, so z = False.

For a, a=True+10 means a = 1+10=11. For b, b=False+10 means b=0+10=10.

Ahmed Mamdouh
  • 696
  • 5
  • 12