Mingming Li — Click any blue heading below to expand the content.
A variable is like a labeled box that stores information. In Python, you just pick a name, use =, and put something inside.
# Variable examples
name = "Alice" # a string (text)
age = 25 # an integer (whole number)
height = 1.68 # a float (decimal)
is_student = True # a boolean (True/False)
name = "Alice" means "put the word 'Alice' into a box called 'name'."
player_score, game_active.
int -> whole numbers: 42, -7, 1000 (no decimal point)float -> decimal numbers: 3.14, -0.5, 2.0 (has decimal point)str -> text: "hello", 'Python' (inside quotes)bool -> True / False (like yes/no switches)You can check a variable's type with type():
score = 100
print(type(score)) # <class 'int'>
score = "high"
print(type(score)) # <class 'str'>
x = 5 (int) -> x = "now text" (str) - Python doesn't mind!
apples = 10
apples = apples + 5 # apples becomes 15
apples += 3 # shorthand: apples becomes 18
# Combining strings
first = "Hello"
last = "World"
message = first + " " + last # "Hello World"
# f-strings (Python 3.6+)
name = "Mingming"
print(f"Welcome, {name}!") # Welcome, Mingming!
+= is a shortcut! apples += 3 is the same as apples = apples + 3.
score and Score are DIFFERENT variables!player_health instead of ph (your future self will thank you).my_variable not my variable.if, for, while, True.MAX_SPEED = 100.score and Score are different! Python is case-sensitive.
# Creating variables
name = "value"
number = 42
# Updating
count = count + 1
count += 1 # same as above
# Multiple assignment
a, b = 5, 10 # a=5, b=10
# Swapping values (magic!)
x, y = y, x # swaps values in one line!
Look at this code from our game (Code-1):
player = pygame.Rect(80, 300, 40, 40)
cactus = pygame.Rect(800, 300, 30, 40)
gravity = 0
game_active = False
Questions:
gravity is? (int, float, str, or bool?)game_active?player, cactus, gravity, game_activegravity is an int (integer) - it stores whole numbers like 0, 1, 2...game_active is a bool (boolean) - it stores either True or FalseTry these exercises to practice variables. Write your code in a Python environment (like Thonny).
city and assign it the name of your favorite city. Then create a variable population and assign it a number (in millions). Print both using print().a = 15 and b = 4. Calculate and store their sum, difference, product, and quotient in new variables. Print each result.total = 50. Use += to add 25, then -= to subtract 10. What is the final value of total?greeting = "Hello" and name = "Sam". Combine them into message that prints "Hello, Sam!""My name is [first] [last] and I am [age] years old."x = 10 and y = 20 so they become x=20, y=10 (one-line trick!).temperature = 22.5 (float) to an integer and store in temp_int. Print both.x = 5
y = x
x = 10
print(y)is_raining = True.input(), convert to int, print double.=.str().1st_place is invalid. Use first_place instead.print() to see what's inside your variables. Add print(gravity) to see its value change!
int and float?+= do?=int = whole numbers, float = numbers with decimalstype(variable_name)x += 5 is the same as x = x + 5x, y = y, xtype() to check variable types= and +=+ and f-strings