LearnFrenzy
Sign in Take a free test

Variables and Data Types · Lesson 4 of 39

Variables in Python

5 min read Updated Nov 7, 2026 Saurabh Samir

A program is only useful if it can remember things – a user's name, a price, a score. Python remembers values with variables. In this lesson you will see what a variable actually is (it is not quite a "box"), how to name one well, and a few tricks that make Python code short and clear.

In this lesson

  • Creating variables and changing their values
  • The "name tag" model: how variables really work
  • Naming rules and the style Python developers use
  • Multiple assignment, swapping and constants

Creating a variable

You create a variable by writing a name, an equals sign and a value. No keyword, no type declaration:

city = "Bengaluru"
population = 13_600_000
temperature = 24.5
is_raining = False

print(city, population, temperature, is_raining)

Output

Bengaluru 13600000 24.5 False

Read = as "gets" or "refers to", not "equals": city gets "Bengaluru". The underscores in 13_600_000 are only there to make the number readable – Python ignores them.

Four names, four values

  • city"Bengaluru"str – text
  • population13600000int – whole number
  • temperature24.5float – decimal
  • is_rainingFalsebool – yes / no

Variables are name tags, not boxes

Many books describe a variable as a box that holds a value. In Python a better picture is a name tag tied to an object. The value (the object) lives in memory; the variable is just a label pointing at it. Two names can point at the same object:

a = [1, 2, 3]
b = a            # b now points at the SAME list
b.append(4)

print(a)
print(a is b)

Output

[1, 2, 3, 4]
True

Two tags, one object

  1. aname tag
  2. [1, 2, 3, 4]one list object in memory
  3. banother name tag on the same list

This surprises almost every beginner, so remember it now: b = a never copies anything. It only adds another name. You will learn how to make real copies in the Lists lesson.

Reassigning moves the tag

score = 10
print(score)
score = score + 5    # read the old value, then point at the new one
print(score)
score = "ten"        # a name can point at any type
print(score)

Output

10
15
ten

Because the type belongs to the value, not the name, Python is called dynamically typed. It is allowed, but changing a variable from a number to text usually makes code confusing – avoid it.

Naming rules

Python enforces a few rules. Break them and you get a SyntaxError:

  • Use letters, digits and underscores only: total_2026 ✓, total-2026 ✗
  • Do not start with a digit: player1 ✓, 1player ✗
  • Names are case-sensitive: age, Age and AGE are three different variables
  • Do not use keywords such as if, for, class, True, None
import keyword
print(len(keyword.kwlist), "keywords, for example:", keyword.kwlist[:6])

Output

35 keywords, for example: ['False', 'None', 'True', 'and', 'as', 'assert']

Naming style (PEP 8)

Beyond the rules, Python developers follow the style guide PEP 8. Following it makes your code look professional in interviews and code reviews:

Kind of nameStyleExample
Variables and functionssnake_casetotal_price, get_user()
ConstantsUPPER_SNAKE_CASEMAX_LOGIN_ATTEMPTS
ClassesPascalCaseBankAccount
"Internal" helpersleading underscore_cache

Pick names that say what the value means: days_left beats d, and price_with_gst beats p2.

Common mistake: shadowing built-in names

Naming a variable list, str, sum or input is legal, but it hides Python's own function of that name. After sum = 10, calling sum([1, 2]) fails with TypeError: 'int' object is not callable. Use total, items or text instead.

Handy assignment tricks

Several variables in one line

name, age, city = "Meera", 27, "Kochi"
x = y = z = 0
print(name, age, city)
print(x, y, z)

Output

Meera 27 Kochi
0 0 0

Swapping two values

In most languages you need a temporary variable to swap. Python does it in one line:

left, right = "tea", "coffee"
left, right = right, left
print(left, right)

Output

coffee tea

Augmented assignment

balance = 1000
balance += 250    # same as balance = balance + 250
balance -= 100
balance *= 2
print(balance)

Output

2300

Constants

Python has no real constants. By convention, a name in capitals means "please don't change this":

GST_RATE = 0.18
price = 500
print("Price with GST:", price + price * GST_RATE)

Output

Price with GST: 590.0

Practice

Try it yourself

  1. Store a product name, its price and quantity in three variables, then print the total cost.

    Show solution
    product = "Notebook"
    price = 45
    quantity = 6
    print(product, "total:", price * quantity)

    Output

    Notebook total: 270
  2. Swap the values of first = 5 and second = 9 without a third variable.

    Show solution
    first, second = 5, 9
    first, second = second, first
    print(first, second)

    Output

    9 5

Quick check

Which names are invalid: my_var, 2nd_place, class, _hidden?

2nd_place (starts with a digit) and class (a keyword).

After a = [1]; b = a; b.append(2), what is a?

[1, 2] – both names point at the same list.

What does "dynamically typed" mean?

Types belong to values, not to variable names, so the same name can later point at a value of a different type.

Summary

  • A variable is a name pointing at a value: name = value.
  • b = a adds a second name for the same object; it does not copy.
  • Use snake_case names that explain the meaning; avoid keywords and built-in names.
  • Python can assign several names at once and swap values in one line.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Popular