Since Python doesn't auto-convert types, 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 — Python's int() demands the entire string be a valid number; it won't quietly read just the leading digits and ignore the rest. 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 |
One thing worth flagging specifically: in Python, the string "0" is truthy — it's a non-empty string, full stop, with no special-casing for what it contains.
The same int()/str() pattern works for collection types too — list(), tuple(), set(), and dict() each build one collection type from another:
numbers = [1, 2, 2, 3]
unique = set(numbers) # {1, 2, 3} — a set, so duplicates disappear
back_to_list = list(unique) # a list again, order not guaranteed to match the originalConverting to a set and back is a fast, common trick for removing duplicates from a list.