Python Variables
Store and reuse information using variables, follow naming rules, assign multiple values, and understand basic scope.
After completing this chapter, you will be able to:
- Create and update variables to store values in Python.
- Apply Python's variable naming rules and readable snake_case style.
- Assign multiple variables in one line.
- Understand the basic difference between global and local variables.
What Are Variables?
A variable is a named container that stores a value in memory so you can reuse or update it later in your program.
Think of a variable like a labeled storage box: the label is the variable name, and whatever you put inside is the value.
Creating Variables
In Python, you create a variable the moment you assign a value to it using a single equals sign =. You do not need a special command like var or let, and you do not have to state the type beforehand.
variable_name = valueRead '=' as 'is assigned' rather than 'is mathematically equal to'.
Variables can be reassigned to a new value—or even a different type—at any time.
student_name = "Maya"
score = 88
print(student_name, "scored", score)
score = 94
print("Updated score:", score)Expected Output
Maya scored 88 Updated score: 94
Variable Naming Rules
Python enforces strict rules for variable names:
- A variable name must start with a letter (
a-z,A-Z) or an underscore (_). - A variable name cannot start with a number.
- A variable name can only contain letters, numbers, and underscores (
A-z,0-9,_). - Variable names are case-sensitive (
age,Age, andAGEare three different variables). - You cannot use reserved Python keywords (such as
if,for,True,and) as variable names.
By Python convention, multi-word variable names use snake_case (all lowercase words separated by underscores), like first_name or total_price.
Names like 2nd_score or total-cost are invalid in Python. Start with a letter or underscore, and use underscores instead of hyphens.
2nd_score = 90
total-cost = 50second_score = 90
total_cost = 50Assigning Multiple Values
Python lets you assign values to several variables on a single line, or assign the same value to multiple variables at once.
Assign distinct values using commas, or share one value using chained equals signs.
x, y, z = "Red", "Green", "Blue"
print(x, y, z)
a = b = c = 100
print(a, b, c)Expected Output
Red Green Blue 100 100 100
Understanding Variable Types
Every value stored in a variable has a data type (such as text or an integer). You can check a variable's type anytime with type(), and you can reassign a variable to a new type if needed.
Python tracks the type of whatever value is currently stored in the variable.
item = "Notebook"
print(item, type(item))
item = 5
print(item, type(item))Expected Output
Notebook <class 'str'> 5 <class 'int'>
Global and Local Variables (Basic Introduction)
Where you create a variable determines where in your program it can be used:
- Global variables are created outside of any function and can be read anywhere in the file.
- Local variables are created inside a function (a reusable named block defined with
def) and exist only inside that function.
message_global works everywhere, while message_local only belongs inside greet().
course = "Python Basics" # Global variable
def show_info():
topic = "Variables" # Local variable
print("Inside function:", course, "-", topic)
show_info()
print("Outside function:", course)Expected Output
Inside function: Python Basics - Variables Outside function: Python Basics
Quick Check
Which of the following is a valid Python variable name?
Chapter Summary
- Variables store values under descriptive names using
=. - Names must start with a letter or
_and cannot contain spaces or hyphens. - Python supports multiple assignments on one line (
x, y = 1, 2). - Variables created outside functions are global; variables created inside functions are local to that function.