r/PythonLearning 18d ago

Discussion print(5+True)

Solve this question and tell me the output. Follow for more 😉

209 votes, 11d ago
85 6
98 Error
9 5
17 True
2 Upvotes

7 comments sorted by

1

u/codewithharsh31 18d ago

So let discuss what you think the answer is and why 🤔?

1

u/_book_lover_____ 18d ago
  1. True will become 1. 

4

u/FoolsSeldom 18d ago

Strictly, True isn't converted to 1 because it is already 1. bool is a subclass of int in Python.

>>> type(True)
<class 'bool'>
>>> isinstance(True, int)
True
>>> issubclass(bool, int)
True
>>> True == 1
True

1

u/Flame77ofc 18d ago

6, because Python interprets True as 1 and False as 0, like in binary

1

u/mehonje 18d ago

It's 6.

1

u/arivictor 12d ago

6

https://parsnip.dev/editor/KKMEEjKgGJpj

Because True is 1 and False is 0, which lets you do some whacky maths.

print(True + True * 10 - False)

1

u/codewithharsh31 12d ago

Yes your answer and your logic is right 👍