Variables and Types¶
A value is one of the basic things a program works with — a number, a piece of text, a true/false flag. Every value belongs to a type. Python can tell you the type of any value:
>>> type(42)
<class 'int'>
>>> type(3.14)
<class 'float'>
>>> type('Aria')
<class 'str'>
>>> type(True)
<class 'bool'>
Variables¶
A variable is a name that refers to a value. An assignment statement creates a variable and gives it a value:
score = 0
player_name = "Aria"
player_speed = 5.5
is_alive = True
Python infers the type from the value on the right-hand side — you never declare types explicitly.
Variable names can contain letters, numbers, and underscores, but must not
start with a number. Names are case-sensitive: Health and health
are different variables. By convention, use lowercase with underscores
(player_speed, not PlayerSpeed).
Keywords — words Python reserves for its own use — are illegal as variable
names. These include if, for, while, class, def,
return, True, False, and None. Attempting to use one gives
a SyntaxError.
Choose mnemonic variable names that tell the reader what the variable represents. Compare:
# hard to read
x1q3 = 35.0
x2q3 = 12.5
x3q3 = x1q3 * x2q3
# immediately clear
hours = 35.0
rate = 12.5
pay = hours * rate
Both programs do exactly the same thing. Good names make the intent obvious without a comment.
Numbers¶
Python has two numeric types used constantly in game math:
int — whole numbers (health points, lives, level number)
float — decimal numbers (position, speed, elapsed time)
health = 100 # int
player_speed = 4.75 # float
gravity = -9.8
Arithmetic operators: + - * / ** (exponentiation)
// (floor division) % (modulus/remainder).
Python follows standard precedence rules (PEMDAS): parentheses first, then exponentiation, then multiplication/division, then addition/subtraction. When in doubt, add parentheses.
// and % are especially useful in games:
tile_col = player_x // 32 # which 32-pixel tile column?
is_even_wave = wave_number % 2 == 0 # every second wave is a boss wave
Strings¶
Strings store text. Use them for player names, dialog lines, and asset file paths.
level_name = "The Haunted Swamp"
model_path = "models/player/hero"
The + operator concatenates (joins) strings end to end — it is not
addition:
greeting = "Welcome back, " + player_name + "!"
f-strings (formatted string literals) embed values directly, which is cleaner for building HUD text or log messages:
hud_text = f"HP: {health}/{max_health} Score: {score}"
Booleans¶
Booleans are either True or False. They drive every decision in
game logic:
is_alive = True
has_key = False
game_over = not is_alive
Comparison operators produce booleans:
health_low = health < 25
at_max_health = health == max_health # == not =
door_reachable = distance <= 5.0
A single = is assignment; == is comparison. Confusing them is the
most common beginner error.
None¶
None represents the absence of a value. Use it to mark something that
has not been set yet:
current_target = None # no enemy targeted yet
if current_target is None:
print("No target selected")
Type Conversion¶
Python will not automatically mix types in ways that make no sense. Use the built-in conversion functions when you need to change a value’s type:
health_str = str(health) # int → string, for display
bonus = int("50") # string → int, from a config file
ratio = float(hits) / total # ensure float division
int() truncates floats rather than rounding: int(3.99) is 3.