Page 1
Variables and assignment
A variable is a name that refers to a value. You create a variable with an assignment statement: the name goes on the left of the equals sign (=) and the value goes on the right.
age = 15
name = "Ada"
age = age + 1 # reassign: age is now 16
print(name, age) # Ada 16The equals sign means "store the value on the right under the name on the left"; it is not a test for equality. Python has no separate declaration step: a variable exists from the moment you first assign to it.
You can reassign a variable at any time, even to a value of a different type, because Python is dynamically typed. The name simply points at the new value.
Multiple assignment sets several variables in one line: x, y = 1, 2 sets x to 1 and y to 2. You can also give several names the same value with a = b = 0.
Page 2
Naming rules
A variable name can contain letters, digits and underscores (_). A name may start with a letter or an underscore. It cannot start with a digit, so 2nd_place is invalid but place_2 is fine. It cannot contain spaces or hyphens.
Names are case-sensitive: score, Score and SCORE are three different variables. A name cannot be a keyword such as if, for, while, def, class, True, False or None, because keywords already have a fixed meaning in Python.
By convention, Python variable names use snake_case: lowercase words joined by underscores, like total_price. A name written in all capitals, like MAX_SIZE, signals a constant that shouldn't change, though Python doesn't enforce it.
user_name = "sam" # valid, snake_case
_count = 3 # valid: may start with an underscore
MAX_SIZE = 100 # a constant, by convention
# 2nd_place = 1 # SyntaxError: can't start with a digit
# my-name = "x" # SyntaxError: hyphen not allowedPage 3
Basic types
Every value in Python has a type. The basic built-in types are:
- int: whole numbers like 42, -7 or 0. Python ints have no fixed size limit.
- float: numbers with a decimal point like 3.14 or -0.5. Floats are stored approximately, so 0.1 + 0.2 gives 0.30000000000000004.
- str: a string of text in quotes, like "hello".
- bool: one of the two values True and False.
- NoneType: the type of the single value None, which means "no value".
The built-in function type() tells you the type of a value:
print(type(42)) # <class 'int'>
print(type(3.0)) # <class 'float'>
print(type("42")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>Note that "42" in quotes is a str, not an int. In Python, bool is a subtype of int: True behaves like 1 and False like 0.
Page 4
Casting between types
Casting means converting a value to another type by calling the type's name as a function:
- int() converts to an integer. int("42") gives 42. int(3.9) gives 3, because int() truncates toward zero instead of rounding.
- float() converts to a float. float("2.5") gives 2.5 and float(7) gives 7.0.
- str() converts to a string. str(10) gives "10".
- bool() converts to True or False. bool(0) and bool("") give False; most other values give True.
age_text = "16"
age = int(age_text) # "16" -> 16
print(age + 1) # 17
print(str(age) + " years") # 16 years
print(int(-2.7)) # -2A string that doesn't look like a number can't be converted: int("hello") raises a ValueError. int("3.5") also raises a ValueError; use int(float("3.5")) instead. The input() function always returns a str, so cast it before doing arithmetic.
Finished reading?
Mark it as read for +20 XP. Optional: practice is open either way.