A variable is a name associated with a value. Variables let you store data, reuse it, and change it without rewriting the whole program.
Creating a Variable
The = operator assigns a value to a name:
name = 'Alex'
age = 16
height = 1.72
is_student = True
Python determines each value type automatically:
print(type(name)) # str
print(type(age)) # int
print(type(height)) # float
print(type(is_student)) # bool
Using and Updating Values
score = 0
print(score) # 0
score = 10
score += 5
print(score) # 15
A new assignment replaces the previous value. score += 5 is shorthand for score = score + 5.
Naming Rules
A variable name:
- starts with a letter or
_; - contains letters, digits, and
_; - contains no spaces or hyphens;
- is not a Python keyword;
- is case-sensitive:
ageandAgeare different names.
Use descriptive snake_case names:
user_name = 'Maria'
total_score = 100
rocket_speed = 25
Constants that should not change by convention use UPPER_CASE:
MAX_ATTEMPTS = 3
SECONDS_IN_MINUTE = 60
Never store real passwords, tokens, or API keys directly in source code.
Regular Strings
A regular string stores text:
message = 'Launch ready'
city = "Tbilisi"
Single and double quotes are equivalent. The opening and closing quote styles must match.
f-Strings: Insert Values
The f prefix lets you insert variables and expressions inside {}:
pilot = 'Alex'
altitude = 120
status = f'Pilot {pilot}, altitude {altitude} km'
next_status = f'In one minute: {altitude + 10} km'
print(status)
print(next_status)
Without f, braces remain ordinary text.
r-Strings: Preserve Backslashes
The r prefix creates a raw string. Backslashes do not become \n, \t, or other escape sequences:
project_path = r'C:\Users\student\python-course'
print(project_path)
r-strings are useful for paths, regular expressions, and ASCII art. They cannot end with an odd number of backslashes immediately before the closing quote.
Multiline Strings
Triple single or double quotes preserve line breaks:
mission = """Check systems
Start engines
Reach orbit"""
print(mission)
Prefixes can be combined. For example, fr"..." inserts values while preserving backslashes:
user_name = 'student'
folder = fr'C:\Users\{user_name}\project'
print(folder)
Common Mistakes
# NameError: used before assignment
print(level)
level = 1
# Prints the word name, not the variable value
name = 'Anna'
print('name')
# These are different variables
score = 10
Score = 20
Create a variable before using it, remove quotes around its name when printing its value, and respect capitalization.
Mini Practice
- Create
name,city, andagevariables. - Build a greeting with an f-string.
- Store a path containing backslashes in an r-string.
- Display both results with
print().
Summary
name = valuecreates or updates a variable;- descriptive names use
snake_case; - a regular string stores text;
- an f-string inserts values and evaluates expressions;
- an r-string preserves backslashes;
- triple quotes store multiline text.
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!