#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read

Variables and Naming

Learn how to store and name data in Python

What You'll Learn

  • What variables are
  • How to create variables
  • Rules for naming variables
  • Good naming habits

What is a Variable?

A variable is like a box that holds information. You give the box a name so you can use it later.

Simple example:

code.py
age = 25

Here, age is the box name, and 25 is what's inside.

Creating Variables

Creating a variable is super easy:

code.py
name = "John"
age = 30
price = 19.99

Format: variable_name = value

Using Variables

Once you create a variable, you can use it:

code.py
price = 100
quantity = 3
total = price * quantity

print(total)
# Shows: 300

Naming Rules

What You CAN Do

✅ Use letters:

code.py
name = "Sarah"
city = "Paris"

✅ Use numbers (but not at start):

code.py
student1 = "Mike"
room2 = "Lab"

✅ Use underscores:

code.py
user_name = "Alex"
total_price = 500

What You CANNOT Do

❌ No spaces:

code.py
user name = "Alex"  # Wrong!
user_name = "Alex"  # Correct!

❌ No special characters:

code.py
user-name = "Alex"  # Wrong!
user@email = "test" # Wrong!

❌ Can't start with number:

code.py
1student = "Mike"   # Wrong!
student1 = "Mike"   # Correct!

❌ Can't use Python words:

code.py
print = 100    # Wrong! (print is a Python command)
for = 50       # Wrong! (for is a Python command)

Good Naming Habits

Use Clear Names

Bad:

code.py
a = 100
b = 5
c = a * b

Good:

code.py
price = 100
quantity = 5
total = price * quantity

Clear names help you understand code later!

Use Lowercase

code.py
# Good style
user_age = 25
total_cost = 100

# Works but not standard
UserAge = 25
TOTAL_COST = 100

Use Underscores for Long Names

code.py
# Easy to read
user_first_name = "John"
total_price_with_tax = 120

# Hard to read
userfirstname = "John"
totalprice = 120

Changing Variable Values

You can change what's in a variable:

code.py
score = 10
print(score)  # Shows: 10

score = 20
print(score)  # Shows: 20

score = score + 5
print(score)  # Shows: 25

Multiple Variables at Once

code.py
# One way
name = "Sarah"
age = 28
city = "London"

# Shorter way
name, age, city = "Sarah", 28, "London"

Practice Examples

Example 1: Store personal info

code.py
first_name = "Emma"
last_name = "Smith"
age = 25
country = "USA"

print("Name:", first_name, last_name)
print("Age:", age)
print("Country:", country)

Example 2: Simple calculator

code.py
number1 = 10
number2 = 5

sum_result = number1 + number2
difference = number1 - number2

print("Sum:", sum_result)
print("Difference:", difference)

Example 3: Shopping cart

code.py
item_name = "Laptop"
item_price = 800
tax = 50
shipping = 20

total_cost = item_price + tax + shipping

print("Item:", item_name)
print("Total:", total_cost)
# Shows: Total: 870

Quick Tips

  • Choose names that explain what the variable holds
  • Use total_price not tp
  • Use user_age not ua
  • Longer names are better than confusing short names

Common Mistakes

Mistake 1: Forgetting quotes for text

code.py
name = John   # Wrong!
name = "John" # Correct!

Mistake 2: Using spaces

code.py
user age = 25     # Wrong!
user_age = 25     # Correct!

Mistake 3: Starting with number

code.py
1user = "Mike"    # Wrong!
user1 = "Mike"    # Correct!

Summary

  • Variables store information
  • Use clear, meaningful names
  • Follow naming rules (no spaces, no special characters)
  • Use lowercase with underscores
  • Change variable values anytime

What's Next?

Now let's learn about different types of data you can store in variables!

SkillsetMaster - AI, Web Development & Data Analytics Courses