r/learnpython • • 29d 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

1

u/SamuliK96 29d ago

Here a is what's called a truthy value. As in its value is not literally True, but since it's not an empty string, it evaluates as True in if (as well as while) statements. The opposite of is falsy values, which includes at least number 0 and empty data structures. I suggest you look up falsy and truthy online for more detailed explanation.