Python Foundation

Variables in Python

01 · What is a Variable? Variables · 1 of 11

What is a Variable?

A variable is a name that refers to a value stored in the computer’s memory.

You can think of a variable as a labeled box. You put a value inside the box and give the box a name so you can use that value later.

Python
name = "Kundan"
age = 25
city = "Hyderabad"

print(name)
print(age)
print(city)
Output
Kundan
25
Hyderabad

In the above example, name, age and city are variables.

02 · Why Do We Need Variables? Variables · 2 of 11

Why Do We Need Variables?

Without variables, we would have to write the same value again and again.

Without Variables

Python
print("Student name is Kundan")
print("Kundan is learning Python")
print("Kundan lives in Hyderabad")

With Variables

Python
name = "Kundan"
city = "Hyderabad"

print("Student name is", name)
print(name, "is learning Python")
print(name, "lives in", city)
Output
Student name is Kundan
Kundan is learning Python
Kundan lives in Hyderabad

If the name changes, you only need to update it in one place.

03 · Creating a Variable Variables · 3 of 11

Creating a Variable

In Python, you create a variable by simply assigning a value to a name using the = operator.

Python
x = 10
message = "Hello Python"
price = 99.50
is_active = True

print(x)
print(message)
print(price)
print(is_active)
Output
10
Hello Python
99.5
True

Syntax: variable_name = value

04 · Rules for Naming Variables Variables · 4 of 11

Rules for Naming Variables

Python has strict rules for variable names:

  1. A variable name can contain letters (a-z, A-Z), digits (0-9) and underscore (_)
  2. A variable name cannot start with a digit
  3. Variable names are case-sensitive (age, Age and AGE are different)
  4. You cannot use Python keywords as variable names (if, for, while, class, etc.)
  5. No spaces are allowed in variable names

Valid Variable Names

Python
name = "Ravi"
student_name = "Priya"
age2 = 22
_total = 500
myVariable = 100

Invalid Variable Names

Python
# 2name = "Ravi"        → cannot start with number
# student-name = "Amit" → hyphen not allowed
# my name = "Kiran"     → space not allowed
# if = 10               → 'if' is a keyword
05 · Multiple Assignment Variables · 5 of 11

Multiple Assignment

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

Same value to multiple variables

Python
a = b = c = 30
print(a, b, c)
Output
30 30 30

Different values to multiple variables

Python
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
Output
10
20
30

Unpacking a list / tuple

Python
name, age, city = ["Kundan", 25, "Hyderabad"]
print(name)
print(age)
print(city)
Output
Kundan
25
Hyderabad
06 · Changing Variable Values Variables · 6 of 11

Changing Variable Values

You can change the value of a variable at any time by assigning a new value to it.

Python
score = 50
print("Initial score:", score)

score = 75
print("Updated score:", score)

score = score + 10
print("Final score:", score)
Output
Initial score: 50
Updated score: 75
Final score: 85

The variable still has the same name, but the value it refers to has changed.

07 · Dynamic Typing Variables · 7 of 11

Dynamic Typing in Python

Python is a dynamically typed language. This means you do not need to declare the type of a variable. The type is decided automatically based on the value you assign.

Python
x = 10
print(x, type(x))

x = "Hello"
print(x, type(x))

x = 3.14
print(x, type(x))

x = True
print(x, type(x))
Output
10 <class 'int'>
Hello <class 'str'>
3.14 <class 'float'>
True <class 'bool'>

The same variable x can hold different types of values at different times.

08 · Deleting a Variable Variables · 8 of 11

Deleting a Variable

You can delete a variable using the del keyword. After deletion, the variable no longer exists.

Python
name = "Kundan"
print(name)

del name

# print(name)   → This will cause NameError
Output
Kundan

After del name, using name again will raise NameError: name 'name' is not defined.

09 · Good Naming Practices Variables · 9 of 11

Good Naming Practices

Writing clear variable names makes your code easier to read and maintain.

Recommended Practices

  • Use meaningful names → student_name is better than sn
  • Use lowercase letters with underscores (snake_case) → total_marks
  • Avoid single letter names except in loops → i, j, k are okay in loops
  • Be consistent with naming style

Good Examples

Python
student_name = "Ravi"
total_marks = 450
is_passed = True
average_score = 89.5

Bad Examples

Python
a = "Ravi"           # not clear
tm = 450              # unclear abbreviation
x1 = True             # meaningless
AverageScore = 89.5    # better to use snake_case
10 · Common Mistakes Variables · 10 of 11

Common Mistakes Beginners Make

1. Using a variable before creating it

Python
print(score)   # NameError
Error
NameError: name 'score' is not defined

2. Case sensitivity mistake

Python
Age = 25
print(age)     # NameError
Error
NameError: name 'age' is not defined

3. Using keywords as variable names

Python
# if = 10          → SyntaxError
# for = "loop"     → SyntaxError
# class = "A"      → SyntaxError

4. Starting variable name with a number

Python
# 1name = "Ravi"   → SyntaxError
11 · Summary Variables · 11 of 11

Summary – Variables in Python

  • A variable is a name that refers to a value in memory.
  • Create a variable using name = value.
  • Variable names are case-sensitive and cannot start with a number.
  • Python is dynamically typed – type is decided by the value.
  • You can assign multiple variables in one line.
  • Use meaningful names (snake_case is preferred).
  • Use del to delete a variable.

Next Topic → Data Types in Python

Home Variables in Python

Variables in Python

Learn how to store and manage data using variables.

Select any topic from the left sidebar to begin.


Start Learning →