A Python variable stores a value you can use and change later — the same idea from Intro to Programming, with Python's own syntax, which is about as minimal as it gets.
No $ sigil, no keyword — just a name and a value:
name = "Amit"
age = 21
print(f"{name} is {age} years old.")The f before the string in f"{name} is {age}..." is an f-string — Python's way of embedding variables directly in a string, similar to PHP's double-quote interpolation. You'll look at f-strings properly in the Strings lesson.
| Rule | Example |
|---|---|
| Must start with a letter or underscore (never a number) | _id — valid, 1id — invalid |
| Can contain letters, numbers, and underscores after that | user_2 — valid |
| No spaces or hyphens allowed | user name — invalid |
| Case-sensitive | Name and name are different variables |
| Convention: snake_case, all lowercase | first_name, not firstName |
snake_case, not camelCase
snake_case (lowercase words joined by underscores) is the standard Python naming convention for variables and functions — this is different from PHP's common camelCase style, and from JavaScript's. It's a convention, not a rule the language enforces, but real Python code follows it consistently.
Just like PHP, you never declare a variable's type — Python figures it out from the value, and a variable can hold a different type later:
value = 10 # an integer
value = "ten" # now a string — perfectly legalPython has no const keyword or define() the way PHP does — there's no way to make a variable truly unchangeable. Instead, the convention is to name a value that shouldn't be changed in UPPER_SNAKE_CASE, as a signal to anyone reading the code:
MAX_UPLOAD_SIZE = 5242880 # 5 MB — nothing stops this from being reassigned,
# but the ALL_CAPS name says "please don't"