A variable is a name for a value. Python does not declare types; the value has a type. Read it with type().
Goal
Assign int, float, str, bool, and None, then print each type.
Assignment
name = "Ada"
year = 2024
pi = 3.14
ok = True
missing = None
print(name, year, pi, ok, missing)
print(type(name), type(year), type(pi), type(ok), type(missing))Rebinding
The name can point at a new value. The old value is forgotten if nothing else refers to it.
score = 90
print(score, type(score))
score = 90.5
print(score, type(score))
score = "A"
print(score, type(score))Multiple names
city, country = "Nairobi", "Kenya"
print(city)
print(country)Convert types
print(int("42"))
print(float("3.5"))
print(str(2024))
print(bool(0), bool(1), bool(""))int("3.5") fails — use int(float("3.5")) or round.
print(int(float("3.5")))
print(round(3.5))Pitfall
= is assignment. == tests equality. n = 5 does not print True.