Course Progress5% complete
🐍 Python Basics Topic 5 / 100
⏱ 8 min read

Variables

Storing data using variables. Naming rules, assignment, and how Python remembers things.

"A variable is how a program remembers. Name your data well and your code will speak for itself."

— ShurAI

What is a Variable?

Imagine you are filling out a form. You write your name in one box, your age in another, your city in a third. Each box has a label and holds a value.

A variable is exactly that — a labelled box in your computer's memory that holds a value. You give the box a name, put something inside it, and use that name later to get the value back.

📦
BOX NAME
name
VALUE INSIDE
"Riya"
📦
BOX NAME
age
VALUE INSIDE
21
📦
BOX NAME
city
VALUE INSIDE
"Pune"

Creating a Variable

In Python, you create a variable with one simple line — a name, an equals sign, and a value. That's it. No extra words, no type declarations needed.

python
# Creating variables — name = value
name  = "Riya"
age   = 21
city  = "Pune"
score = 95.5

# Using variables
print(name)   # Output: Riya
print(age)    # Output: 21
print(city)   # Output: Pune
print(score)  # Output: 95.5
The = Sign is Assignment, Not Equality

In Python, = means "store this value in this variable." It does NOT mean "equals" like in maths. So age = 21 means "put 21 into the box called age." You will learn the equality check operator == in a later topic.

Variable Naming Rules

Python has strict rules about what you can name a variable. Break these and your code will not run:

Start with a letter or underscore
name, _value, myVar → all valid
Can contain letters, numbers, underscores
user_name, score2, total_marks → all valid
Cannot start with a number
2name, 1score → invalid, causes error
Cannot use spaces or special characters
my name, user-score, total$ → all invalid
Cannot use Python reserved keywords
if, for, while, def, class, True → reserved by Python
python — valid vs invalid
# ✅ Valid variable names
student_name = "Amit"
score2       = 88
_total       = 500
myCity       = "Delhi"

# ❌ Invalid — will cause errors
# 2score = 88        → starts with number
# my score = 88      → has a space
# user-name = "Ali"  → hyphen not allowed
# if = 10            → 'if' is a keyword

Naming Best Practice — snake_case

Python has one widely accepted style for naming variables: snake_case — all lowercase letters with underscores between words. This is not a rule enforced by Python, but every professional Python programmer follows it.

python
# ✅ Python style — snake_case
first_name   = "Sneha"
phone_number = "9876543210"
total_score  = 450
is_student   = True

# ❌ These work but are bad Python style
# firstName    → camelCase (used in JavaScript, not Python)
# FirstName    → PascalCase (used for class names, not variables)
# FIRSTNAME    → UPPER_CASE (used for constants only)
Name for Humans, Not Computers

The computer does not care if you name a variable x or student_total_marks. But the human reading your code — including future you — will thank you for the descriptive name. Always choose clarity over brevity.

Updating a Variable

Variables are called "variable" because their value can vary — you can change what's inside the box at any time by simply assigning a new value:

python
score = 0
print(score)    # Output: 0

score = 50
print(score)    # Output: 50

score = 50 + 10
print(score)    # Output: 60

# Shorthand — update based on current value
score += 5       # same as score = score + 5
print(score)    # Output: 65

score -= 10      # same as score = score - 10
print(score)    # Output: 55

Multiple Assignment

Python lets you create multiple variables in one line, or assign the same value to several variables at once:

python
# Assign multiple variables in one line
x, y, z = 10, 20, 30
print(x)  # 10
print(y)  # 20
print(z)  # 30

# Assign same value to multiple variables
a = b = c = 0
print(a, b, c)  # 0 0 0

# Swap two variables — Python makes this elegant
first  = "Hello"
second = "World"
first, second = second, first
print(first)   # World
print(second)  # Hello

Python Figures Out the Type Automatically

In many languages you must declare what kind of data a variable holds before using it. Python does this automatically by looking at the value you assign. You do not need to write anything extra:

python
name       = "Arjun"     # Python knows this is text (str)
age        = 22          # Python knows this is a whole number (int)
height     = 5.9         # Python knows this is a decimal (float)
is_active  = True        # Python knows this is True/False (bool)

# Check the type of any variable using type()
print(type(name))       # <class 'str'>
print(type(age))        # <class 'int'>
print(type(height))     # <class 'float'>
print(type(is_active))  # <class 'bool'>

Putting It All Together

Here is a complete program that uses everything from this lesson — creating variables, naming them well, using them, and updating them:

python — full example
# Student report card program

student_name  = "Kavya"
subject       = "Mathematics"
marks_obtained = 82
total_marks   = 100

# Calculate percentage
percentage = (marks_obtained / total_marks) * 100

# Print the report
print(f"Student : {student_name}")
print(f"Subject : {subject}")
print(f"Marks   : {marks_obtained} / {total_marks}")
print(f"Score   : {percentage}%")
output
Student : Kavya
Subject : Mathematics
Marks   : 82 / 100
Score   : 82.0%

"A well-named variable is a small act of kindness to everyone who reads your code — starting with yourself."

— ShurAI

🧠 Quiz — Question 1

What does this line do in Python?  age = 25

🧠 Quiz — Question 2

Which of these is a valid Python variable name?

🧠 Quiz — Question 3

What is the output of this code?

x = 10
x += 5
print(x)

🧠 Quiz — Question 4

What is the recommended naming style for variables in Python?