1

On rating python quizzes on Solo Learn, I came across a rather puzzling quiz. In it, it assigns booleans to three variables, then prints the sum of them to the console. The answer appears to be consisting of a single character, but I have no idea what it does, and can't find any resources on this.

The code:

a = 4
b = 4
c = 5
x = a == b
y = a == c
a = a != b
puzzle = x + y + a
print (puzzle)

What's going on with this code?

Chris Charley
  • 6,403
  • 2
  • 24
  • 26
OakenDuck
  • 485
  • 1
  • 6
  • 15
  • 2
    booleans are basically integers in Python, where``True`` equals ``1`` and ``False`` equals ``0``. You can check with ``print( 1 == True )``, which prints ``True``. – Mike Scotty Jan 12 '20 at 01:30

2 Answers2

4

When added, True is converted to 1, and False to 0. Some examples:

print(True + True) # 2

print(True + True + True) # 3

print(False + False) # 0
Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
2

In Python, Booleans are actually Integers under the hood, both False == 0 and True == 1 evaluate to True. So with your code example, puzzle evaluates to 1.

Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44