Skip to content

Beginner · First steps in Python · topic 3 of 50 · 20 min

Variables: giving names to values

Explanation

A variable is a name that points to a value, like price = 120. You create it with the = sign and use the name later instead of the value. You can change it at any time, and good names make your code easy to read.

Why we need variables

Programs work with values: prices, names, marks. If you want to use a value more than once, or change it later, you give it a name. That name is called a variable.

python
price = 120
quantity = 3
total = price * quantity
print(total)
Shows 360. The names make the meaning clear.
Think of it like this: A variable is like a labelled jar in your kitchen. The label says "sugar", and inside is the sugar. When the recipe says "add sugar", you pick the jar by its label. You can also empty the jar and fill it with something new; the label stays the same.

The = sign means "store", not "equals"

In maths, = means both sides are equal. In Python, = is the assignment operator: it takes the value on the right and stores it in the name on the left. Assignment means "give a value to a name".

python
marks = 70
marks = marks + 5
print(marks)
First marks is 70. Then Python works out 70 + 5 and stores 75 back in marks.

Rules for variable names

  • Use letters, digits and the underscore _. No spaces.
  • A name cannot start with a digit: 2nd_place is wrong, second_place is fine.
  • Names are case-sensitive: Age and age are two different variables.
  • Do not use Python's own keywords such as if, for, class or print as names.
Tip: Python programmers write names in snake_case: small letters with underscores between words, like total_marks or student_name. Choose names that say what the value means: bill_amount is better than x.

Values have types

Every value has a type, which tells Python what kind of thing it is. The most common types are int (whole numbers), float (numbers with a decimal point), str (text) and bool (True or False). The function type() tells you the type.

python
name = "Priya"
age = 21
height = 5.4
is_student = True
print(type(name), type(age), type(height), type(is_student))
str, int, float and bool.

Assigning several variables at once

python
a, b = 5, 10
a, b = b, a
print(a, b)
Python can swap two values in one line. Shows 10 5.
Careful: A variable must be created before you use it. If you write print(total) before total = ..., Python raises a NameError: name 'total' is not defined.

Watch the lesson

Variables: giving names to values · 1/7

Variables

In this lesson you will learn about variables, the names that hold values in your program.

Prefer a video by a teacher? Find free videos on “python variables and data types for beginners” on YouTube ↗

In real life: Splitting a restaurant bill

Aman and three friends had dinner. The bill is 1,840 rupees and they want to add a 10% tip, then split it equally. Variables keep each value labelled, so anyone reading the code knows what each number means.

python
bill = 1840
tip = bill * 10 / 100
friends = 4
each_pays = (bill + tip) / friends
print("Each friend pays", each_pays, "rupees")
Each friend pays 506.0 rupees.

Practice (0/2 done)

Task 1 · Code · Python

Inside the function total_cost(), create a variable price = 45 and a variable quantity = 4, then return price multiplied by quantity.

Task 2 · Code · Python

Write a function swap(a, b) that returns the two values in the opposite order, as a pair: swap(1, 2) should give (2, 1).

Quick quiz

Question 1

1.After x = 5 and then x = x + 2, what is x?

Question 2

2.Which is a valid variable name?

Question 3

3.What is the type of 3.5?

Question 4

4.Are Age and age the same variable?

Question 5

5.What does = do in Python?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: print() and comments