Since Python doesn't auto-convert types the way PHP does, converting between them deliberately is something you'll do constantly — especially when reading text input that needs to become a number.
age_text = "21"
age = int(age_text) # 21, as an actual int
price = float("19.99") # 19.99
text = str(42) # "42"
flag = bool(1) # TrueAnything typed by a user — via input(), a form, a file — arrives as a string, even if it looks like a number. Converting it is not optional if you want to do math with it:
age_text = input("How old are you? ")
age = int(age_text)
print(f"Next year you'll be {age + 1}.")int() is strict, not forgiving
int("21 years") raises a ValueError — unlike PHP's intval(), which quietly reads the leading digits and ignores the rest, Python's int() demands the entire string be a valid number. Wrap conversions of untrusted input in try/except (covered in the Exception Handling lesson) rather than assuming they'll succeed.
Casting anything to bool follows a specific set of rules, which Python applies inside every if statement too:
| Value | Casts to bool as |
|---|---|
| 0, 0.0 | False |
| "" (empty string) | False |
| [], (), {}, set() — any empty collection | False |
| None | False |
| Any other number or non-empty value | True |
This table has the same shape as PHP's, with one difference worth flagging: in Python, the string "0" is truthy — it's a non-empty string, full stop, with no special-casing for what it contains. There's no "0" vs "0.0" trap here the way there was in PHP.