A variable is a name that points to a value. Once a variable exists, you can use the name anywhere you’d use the value — and you can change what the name points to.
Creating a variable
In Python, you create a variable just by assigning a value to a name:
name = "Manikandan"
age = 30
height = 1.75
is_student = False
That’s it. No var, no let, no type declaration. The = sign means “make the name on the left point to the value on the right”.
You can use a variable wherever you’d use its value:
print(name)
print(age + 1)
Manikandan
31
Variables can change
A variable’s value can be replaced at any time:
score = 10
print(score)
score = 20
print(score)
score = score + 5
print(score)
10
20
25
The line score = score + 5 reads the current value of score, adds 5, then assigns the result back to score.
Python figures out the type
Unlike statically typed languages, you don’t tell Python the type of a variable — it picks one based on the value:
count = 42 # Python sees an integer → type is int
price = 9.99 # Python sees a decimal → type is float
name = "Python" # Python sees text → type is str
is_open = True # Python sees True/False → type is bool
You can check a value’s type with the built-in type function:
print(type(count))
print(type(price))
print(type(name))
<class 'int'>
<class 'float'>
<class 'str'>
Type hints — declaring types on purpose
Even though Python infers types automatically, we’ll often write them out using type hints. A type hint goes after a colon:
count: int = 42
price: float = 9.99
name: str = "Python"
is_open: bool = True
Type hints don’t change how the program runs. They give the type checker (pyright) enough information to catch mistakes:
score: int = 10
score = "twenty" # pyright will flag this — string can't go into an int variable
Throughout this course, we’ll add type hints anywhere they’re not obvious from the value. They’re a habit worth building from day one.
Naming rules
Python has two rules and a strong convention.
Rules — the program won’t run unless you follow these:
- Names can contain letters, digits, and underscores:
count,total_2024,user_name. - Names must start with a letter or underscore, not a digit:
2nd_placeis invalid. - Names are case-sensitive:
Total,total, andTOTALare three different variables. - You can’t use Python’s reserved words:
if,for,class,True,None, and similar.
Convention — every Python codebase follows this, and so will we:
- Use snake_case for variables and functions:
user_name,total_price,is_logged_in. - Use ALL_CAPS for constants — values that don’t change:
MAX_RETRIES = 5. - Use PascalCase for class names (we’ll meet classes in Section 12):
class UserProfile:.
Good names matter
The single biggest jump in code quality comes from picking good names.
# bad
x = 9.81
t = 2.0
d = 0.5 * x * t ** 2
# good
gravity_ms2: float = 9.81
time_seconds: float = 2.0
distance_metres: float = 0.5 * gravity_ms2 * time_seconds ** 2
The good version reads like a sentence. The bad version forces the reader to remember what x, t, and d stand for.
A good name answers two questions: what is this value and (if relevant) what units is it in. The second one alone has prevented countless bugs in real-world code — including one that crashed a NASA Mars orbiter.
What’s next
You can name and store values. Next, we’ll look at Python’s number types in detail.