⚡0 XP🔥0

Variables

Lesson 4 of 33 ⏱ 5 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs Python

How to store data in variables, naming rules, multiple assignment, swapping values and how Python variables really work.

What is a variable?#

A variable is a name that refers to a value stored in the computer's memory. Think of it as a labelled box - you put a value inside and use the label to get it back later.

student = "Priya"
marks = 87
print(student)
print(marks)
Output
Priya
87

Here student and marks are variables. The = sign is the assignment operator - it means "store the value on the right in the name on the left". It does not mean "equal to" as in maths.

हिंदी में समझें

Variable को एक डिब्बे पर लगे लेबल की तरह समझें। marks = 87 का मतलब है - "87 नाम की value को memory में रखो और उस पर marks का लेबल लगा दो।" बाद में जब भी आप marks लिखेंगे, Python उस डिब्बे से 87 निकाल कर देगा।

ध्यान दें: यहाँ = का मतलब "बराबर है" नहीं, बल्कि "में रखो (store करो)" है।

Creating variables#

In Python you create a variable simply by assigning a value. There is no int x; style declaration.

▶ Try it YourselfPython
Ctrl + Enter to run

Changing a variable's value#

A variable can be given a new value at any time. The old value is replaced.

score = 10
print(score)
score = 25
print(score)
score = score + 5      # take current value, add 5, store back
print(score)
Output
10
25
30

The line score = score + 5 confuses many beginners. Python always works right side first: it calculates score + 5 (25 + 5 = 30), then stores 30 in score.

A variable can even hold a different type of value later (because Python is dynamically typed):

x = 100        # x holds a number
x = "hundred"  # now x holds text - this is allowed

Naming rules#

Rule Valid Invalid
Must start with a letter or _ name, _temp 2name
Only letters, digits and _ total_marks2 total-marks, total marks, price$
Cannot be a keyword class_name class, for, if
Case-sensitive age and Age are different -

Naming conventions (good habits)#

  • Use meaningful names: total_price is better than tp or x.
  • Use snake_case for variables: first_name, monthly_salary.
  • Use ALL CAPS for values that should not change (constants): PI = 3.14159, MAX_USERS = 100.
Common Mistake

Don't use built-in function names as variable names, e.g. print = 5 or list = [1, 2]. It works at first, but then print("hi") stops working in that program!

Multiple assignment#

Many values to many variables#

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

One value to many variables#

a = b = c = 0
print(a, b, c)

Swapping two variables#

In most languages you need a temporary third variable. Python makes it one line:

▶ Try it YourselfPython
Ctrl + Enter to run
हिंदी में समझें

a, b = b, a में Python पहले right side की दोनों values (9, 5) एक साथ उठा लेता है, फिर उन्हें left side के variables में क्रम से रख देता है। इसलिए किसी extra (temporary) variable की ज़रूरत नहीं पड़ती।

How Python variables really work#

In Python, a variable is a reference (pointer) to an object in memory, not the box itself. Two names can refer to the same object:

a = 500
b = a
print(id(a) == id(b))   # id() gives the memory identity of an object
Output
True

id() returns a unique number for each object. Here both a and b point to the same 500. This matters later when you learn about lists.

Deleting a variable#

temp = 42
del temp
print(temp)    # NameError: name 'temp' is not defined

Practical example#

▶ Try it YourselfPython
Ctrl + Enter to run

Summary#

  • A variable is a name that refers to a value: name = value.
  • = means "assign", and the right side is evaluated first.
  • Names: letters/digits/underscore, can't start with a digit, can't be keywords, case-sensitive.
  • Use meaningful snake_case names.
  • x, y = 1, 2 assigns many values; a, b = b, a swaps values.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Store and print

Create a variable name with the value "Aman" and a variable age with the value 21. Then print:

Name: Aman
Age: 21
Expected output:
Name: Aman
Age: 21
2

Swap two values

The variables a and b are given. Swap their values without typing the numbers again so that a becomes 20 and b becomes 10. Keep the print line.

Expected output:
a = 20 b = 10
3

Total bill

Create price = 120 and quantity = 4. Store their product in a variable called total and print it.

Expected output:
480

Quick Quiz

Q1.

Which is a valid variable name?

Q2.

What is printed?

x = 5
x = x + 2
print(x)
Q3.

After a, b = 3, 8 and then a, b = b, a, what is a?

Q4.

What does x = y = 0 do?

Q5.

Which naming style is recommended for Python variables?