PyLearn Learn Python. Build Skills.
Lesson 06 · Foundations

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.

30–40 minutes Beginner Python Foundations
0%
01

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.

02

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.

Creating a variable
age = 25

print(age)
Output
25

Here, age is the variable name and 25 is the value assigned to it.

03

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 =.

Different variables
name = "Adnan"
age = 30
salary = 75000.50
is_student = False

Python automatically determines the type of value being assigned.

No declaration required

Unlike some programming languages, Python does not require you to specify a variable's data type when creating it.

04

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.

Variable references
x = 10
y = x

print(x)
print(y)
Output
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.

05

Changing Variable Values

A variable can be assigned a new value at any time.

Reassigning a variable
score = 50

print(score)

score = 80

print(score)
Output
50
80

The second assignment changes what score refers to.

Updating a Value

You can also calculate a new value using the existing variable.

Updating a score
score = 50

score = score + 10

print(score)
Output
60
06

Multiple Assignment

Python allows you to assign values to multiple variables in a single statement.

Multiple variables
name, age, city = "Ali", 25, "Multan"

print(name)
print(age)
print(city)
Output
Ali
25
Multan

Python matches the values from left to right with the variable names.

Number of values must match.

If you provide three variables but only two values, Python will raise an error.

07

Assigning the Same Value

You can assign the same value to several variables at once.

Same value
x = y = z = 0

print(x)
print(y)
print(z)
Output
0
0
0

This can be convenient when several variables need the same initial value.

08

Variables and Data Types

A Python variable can refer to many different kinds of objects.

Different data types
name = "Sara"
age = 22
height = 5.6
is_student = True

print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

The built-in type() function tells us the type of object referenced by a variable.

09

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

Valid examples
name = "Ahmed"
age = 25
user_name = "Ahmed"
score2 = 90
_private_value = 100

Invalid Names

Invalid examples
2name = "Ahmed"
user name = "Ahmed"
user-name = "Ahmed"
10

Professional Naming Conventions

Python programmers commonly use snake_case for variable names.

Recommended style
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.

Clear variable names
student_name = "Hassan"
exam_score = 87
monthly_salary = 85000
Good naming reduces comments.

A meaningful variable name can often explain itself without requiring an additional comment.

11

Case Sensitivity

Python variable names are case-sensitive.

This means name, Name and NAME are three different names.

Case-sensitive variables
name = "Ali"
Name = "Ahmed"
NAME = "Sara"

print(name)
print(Name)
print(NAME)
Output
Ali
Ahmed
Sara
Watch your capitalization.

Accidentally changing the capitalization of a variable can cause a NameError or unexpected behavior.

12

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.

Dynamic typing example
value = 100

print(type(value))

value = "Python"

print(type(value))
Output
<class 'int'>
<class 'str'>

The name value first refers to an integer object and later refers to a string object.

Important concept

Dynamic typing makes Python flexible, but you still need to understand what type of value a variable currently refers to.

13

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.

Convention for constants
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.

14

Common Mistakes

1. Starting With a Number

Incorrect
1student = "Ali"

Variable names cannot begin with a number.

2. Using Spaces

Incorrect
student name = "Ali"

Use underscores instead: student_name.

3. Forgetting Case Sensitivity

Potential error
student_name = "Ali"

print(Student_name)

Student_name is different from student_name.

4. Using Unclear Names

Hard to understand
x = 45000

If the value represents a salary, this is much clearer:

Better
monthly_salary = 45000
15

Practice Exercises

Exercise 1

Create Basic Variables

Create variables for your name, age, city and favorite programming language. Print all four values.

Exercise 2

Calculate With Variables

Create variables for the price of a product and its quantity. Calculate and print the total price.

Starting point
price = 500
quantity = 3

# Calculate the total here
Exercise 3

Update a Variable

Create a variable named score with an initial value of 50. Increase it by 25 and print the final value.

Exercise 4

Check Variable Types

Create an integer, float, string and Boolean variable. Use type() to display the type of each variable.

16

Mini Challenge

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.

Example 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)
17

Quick Knowledge Check

1

Which symbol is used for variable assignment in Python?

2

Which is a valid Python variable name?

3

Are Python variable names case-sensitive?

4

What does dynamic typing mean in Python?

5

Which naming style is commonly recommended for Python variables?

18

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_case is the common naming style for Python variables.
  • Python uses dynamic typing.
  • Uppercase names are commonly used by convention for constants.
Key takeaway

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.