Store values in variables, learn Python’s core data types — numbers, strings, booleans, and None — and convert between them with type casting.
Why: a variable is a name that points at a value. You create one by writing a name, an equals sign, and the value — no keyword needed. Names use lower_snake_case by convention.
age = 30
first_name = 'Ada'
is_active = True
# reassign freely — a variable can point at a new value any time
age = age + 1 # now 31
print(age)Why: Python has whole numbers (int) and decimal numbers (float). Normal division with / always gives a float; // does floor division (drops the decimal) and % gives the remainder.
count = 10 # int
price = 19.99 # float
print(7 / 2) # 3.5 (always a float)
print(7 // 2) # 3 (floor division)
print(7 % 2) # 1 (remainder)
print(2 ** 3) # 8 (2 to the power of 3)Why: a boolean is either True or False (note the capital letters). None is a special value that means "nothing here yet" — Python’s version of empty/no value.
is_logged_in = True
has_paid = False
# None means "no value". Check for it with "is".
middle_name = None
print(middle_name is None) # TrueWhy: every value has a type. type(x) tells you what it is, and isinstance(x, int) checks whether x is a given type — handy when you are debugging.
print(type(42)) # <class 'int'>
print(type('hi')) # <class 'str'>
print(isinstance(42, int)) # TrueWhy: input from users or files arrives as text (str). To do maths with it you must convert it to a number first. int(), float(), str(), and bool() convert between types.
quantity = int('5') # the text "5" -> the number 5
total = float('19.99') # the text "19.99" -> 19.99
label = str(42) # the number 42 -> the text "42"
# a classic bug: adding text and a number
age = input('Age? ') # input() always returns a str
next_year = int(age) + 1 # convert before doing maths
print(next_year)