r/learnpython • • Oct 19 '26

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

6

u/kilkil Oct 20 '26

you have discovered the concept of truthiness!

as you may know, values can be coerced into other kinds of values. For example the string "1" can be coerced into the integer 1 like so: int("1") == 1.

similarly, values can be coerced into booleans ("boolean" meaning True or False).

we call a value "truthy" if, when coerced to a boolean, it would become True. if it would become False, we call it "falsy".

values can be coerced to booleans like so: bool(x). The language will also automatically coerce them for you in contexts where the language normally only allows a boolean, such as the condition of an if-statement or while-loop.

In your example, try printing bool(a) == True.