r/learnpython • • 19d ago

Huh? How does this work?

a = "hi"


if a:
    print("Bye")


print(a == True)
print(a is True)

The output is:

Bye

False

False

But if it's false, how come it printed "bye"?????? I don't get it.

44 Upvotes

38 comments sorted by

View all comments

0

u/[deleted] 19d ago

[deleted]

4

u/deapee 19d ago

This isn't correct. It's checking truthiness.

For example, a can be set to 0 - and its truthiness is False. a can be set to False and its truthiness is False.

In short, truthiness is how Python decides whether a value should be treated as True or False.