Python doesn’t do much implicit type conversion compared to JavaScript. Most of the time, we need to convert types explicitly. And when it comes to boolean checks, Python has very clear rules about what’s “truthy” and what’s “falsy”.
Explicit Type Conversion
We convert between types using built-in functions.
# To int
int("42") # 42
int(3.9) # 3 (truncates, does NOT round)
int(True) # 1
int("0b1010", 2) # 10 (binary string to int)
# To float
float("3.14") # 3.14
float(42) # 42.0
# To string
str(42) # "42"
str(3.14) # "3.14"
str([1, 2, 3]) # "[1, 2, 3]"
# To list / tuple
list("hello") # ['h', 'e', 'l', 'l', 'o']
list((1, 2, 3)) # [1, 2, 3]
tuple([1, 2, 3]) # (1, 2, 3)
list({1, 2, 3}) # [1, 2, 3] (order not guaranteed)
If a conversion doesn’t make sense, Python raises a ValueError.
int("hello") # ValueError: invalid literal for int()
Falsy Values
These are the values that evaluate to False in a boolean context. Everything else is truthy.
False— the boolean itselfNone— Python’s null0— zero (int)0.0— zero (float)""— empty string[]— empty list{}— empty dictset()— empty set()— empty tuple
# All of these print "falsy"
for val in [False, None, 0, 0.0, "", [], {}, set(), ()]:
if not val:
print(f"{val!r} is falsy")
This is why we can write clean checks like if my_list: instead of if len(my_list) > 0:.
is vs ==
This trips up a lot of people.
==checks if two objects have the same valueischecks if two variables point to the same object in memory
a = [1, 2, 3]
b = [1, 2, 3]
a == b # True — same value
a is b # False — different objects in memory
c = a
a is c # True — same object (c is just another name for a)
The rule: use is only for None, True, and False. For everything else, use ==.
# Good
if x is None:
pass
# Bad — don't do this
if x is 42: # unreliable, even if it sometimes works
pass
Short-Circuit Evaluation
Here’s something that surprises many people. Python’s and and or don’t just return True or False — they return the actual value that determined the result.
# `or` returns the first truthy value (or the last value if all falsy)
"hello" or "world" # "hello" (first truthy)
"" or "fallback" # "fallback" (first is falsy, so second)
0 or "" or None # None (all falsy, returns last)
# `and` returns the first falsy value (or the last value if all truthy)
"hello" and "world" # "world" (all truthy, returns last)
"" and "world" # "" (first falsy, short-circuits)
This pattern is commonly used for default values.
name = user_input or "Anonymous" # if user_input is empty, use "Anonymous"
In simple language, Python treats “empty” things as False and “non-empty” things as True. And and/or are smarter than we might expect — they return actual values, not just booleans.