Python Variables
Learn how Python variables store and reference data, how to create and update them, how Python handles different data types, and how to choose professional variable names.
What You Will Learn
Variables
Understand what variables are and why programs need them.
Assignment
Learn how to create, update and reuse variables.
Naming
Learn Python's variable naming rules and professional naming conventions.
Dynamic Typing
Understand how a Python variable can reference different types of values.
What Is a Variable?
A variable is a name that refers to a value in a Python program.
Variables allow us to store information and use that information later in our program.
For example, instead of writing the number
25 repeatedly, we can store it in a
meaningful variable called age.
age = 25
print(age)
25
Here, age is the variable name and
25 is the value assigned to it.
Creating Variables
Python does not require a special command to declare a variable.
A variable is created when you assign a value to a name
using the assignment operator =.
name = "Adnan"
age = 30
salary = 75000.50
is_student = False
Python automatically determines the type of value being assigned.
Unlike some programming languages, Python does not require you to specify a variable's data type when creating it.
How Python Stores Variables
Beginners often imagine a variable as a physical box containing a value.
This is a useful beginner-friendly model, but Python's actual behavior is better understood as a name referring to an object.
x = 10
y = x
print(x)
print(y)
10 10
Both x and y refer to the
value 10.
You do not normally need to worry about Python's memory management at this stage, but understanding that variables are names referring to objects becomes very useful as you progress to lists, dictionaries, functions and object-oriented programming.
Changing Variable Values
A variable can be assigned a new value at any time.
score = 50
print(score)
score = 80
print(score)
50 80
The second assignment changes what
score refers to.
Updating a Value
You can also calculate a new value using the existing variable.
score = 50
score = score + 10
print(score)
60
Multiple Assignment
Python allows you to assign values to multiple variables in a single statement.
name, age, city = "Ali", 25, "Multan"
print(name)
print(age)
print(city)
Ali 25 Multan
Python matches the values from left to right with the variable names.
If you provide three variables but only two values, Python will raise an error.
Assigning the Same Value
You can assign the same value to several variables at once.
x = y = z = 0
print(x)
print(y)
print(z)
0 0 0
This can be convenient when several variables need the same initial value.
Variables and Data Types
A Python variable can refer to many different kinds of objects.
name = "Sara"
age = 22
height = 5.6
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
The built-in type() function tells us the
type of object referenced by a variable.
Variable Naming Rules
Python has specific rules for naming variables.
- A variable name can contain letters, numbers and underscores.
- A variable name cannot start with a number.
- A variable name cannot contain spaces.
- A variable name is case-sensitive.
- Python keywords cannot be used as variable names.
Valid Names
name = "Ahmed"
age = 25
user_name = "Ahmed"
score2 = 90
_private_value = 100
Invalid Names
2name = "Ahmed"
user name = "Ahmed"
user-name = "Ahmed"
Professional Naming Conventions
Python programmers commonly use snake_case for variable names.
first_name = "Ahmed"
last_name = "Khan"
total_price = 1500
customer_age = 28
Snake case separates words using underscores. It makes longer variable names easier to read.
Meaningful Names
Choose names that communicate what the variable represents.
student_name = "Hassan"
exam_score = 87
monthly_salary = 85000
A meaningful variable name can often explain itself without requiring an additional comment.
Case Sensitivity
Python variable names are case-sensitive.
This means name, Name and
NAME are three different names.
name = "Ali"
Name = "Ahmed"
NAME = "Sara"
print(name)
print(Name)
print(NAME)
Ali Ahmed Sara
Accidentally changing the capitalization of a
variable can cause a NameError or
unexpected behavior.
Python's Dynamic Typing
Python is a dynamically typed language. You do not need to declare the type of a variable before assigning a value.
A variable can also later refer to an object of a different type.
value = 100
print(type(value))
value = "Python"
print(type(value))
<class 'int'> <class 'str'>
The name value first refers to an integer
object and later refers to a string object.
Dynamic typing makes Python flexible, but you still need to understand what type of value a variable currently refers to.
Constants in Python
Python does not have a special keyword that makes a variable permanently constant.
However, programmers commonly use UPPERCASE names to indicate that a value should not normally be changed.
PI = 3.14159
MAX_LOGIN_ATTEMPTS = 5
COMPANY_NAME = "PyLearn"
These names communicate programmer intent. Python will not prevent you from assigning a new value to them.
Common Mistakes
1. Starting With a Number
1student = "Ali"
Variable names cannot begin with a number.
2. Using Spaces
student name = "Ali"
Use underscores instead:
student_name.
3. Forgetting Case Sensitivity
student_name = "Ali"
print(Student_name)
Student_name is different from
student_name.
4. Using Unclear Names
x = 45000
If the value represents a salary, this is much clearer:
monthly_salary = 45000
Practice Exercises
Create Basic Variables
Create variables for your name, age, city and favorite programming language. Print all four values.
Calculate With Variables
Create variables for the price of a product and its quantity. Calculate and print the total price.
price = 500
quantity = 3
# Calculate the total here
Update a Variable
Create a variable named score with an
initial value of 50. Increase it by 25 and print
the final value.
Check Variable Types
Create an integer, float, string and Boolean
variable. Use type() to display the
type of each variable.
Mini Challenge
Student Profile
Build a small Python program that stores information about a student.
Your program should contain variables for:
- Student name
- Student age
- Student city
- Course name
- Exam score
- Whether the student passed
Then print the information in a clean format.
student_name = "Ali"
student_age = 21
student_city = "Multan"
course_name = "Python"
exam_score = 85
passed = True
print(student_name)
print(student_age)
print(student_city)
print(course_name)
print(exam_score)
print(passed)
Quick Knowledge Check
Which symbol is used for variable assignment in Python?
Which is a valid Python variable name?
Are Python variable names case-sensitive?
What does dynamic typing mean in Python?
Which naming style is commonly recommended for Python variables?
Lesson Summary
Variables are one of the most fundamental concepts in Python. They allow programs to store, access and work with information.
- A variable is a name that refers to an object.
-
Variables are created using the assignment operator
=. - Python does not require explicit variable declarations.
- Variables can be reassigned new values.
- Python supports multiple assignment.
- Python variable names are case-sensitive.
- Variable names cannot begin with numbers or contain spaces.
-
snake_caseis the common naming style for Python variables. - Python uses dynamic typing.
- Uppercase names are commonly used by convention for constants.
Choose meaningful variable names and understand what kind of object each variable currently refers to. Clear variables make every future Python program easier to build and understand.
You've reached the end!
Complete the practice exercises and knowledge check, then mark this lesson as complete.