Chapter 04 of 11

Variables in Python

While writing programs, we often need to store information such as a person's name, age, price, or score. This is where variables come in.

A variable is one of the first concepts you'll use in almost every Python program, so let's understand it with some simple examples.

What is a Variable?

A variable is a name used to store a value in a Python program.

For example:

name = "John"
age = 20

Here:

  • name is a variable storing "John".

  • age is a variable storing 20.

Think of a variable like a label attached to a value.

name → "John"
age  → 20

We can then use these variables whenever we need their values.

name = "John"
age = 20

print(name)
print(age)

Output:

John
20

Creating Variables

Python makes creating variables very simple. You don't need to specify the data type separately.

name = "John"
age = 21
height = 5.9

Python automatically determines the type of value being stored.

For example:

Variable

Value

Type

name

"John"

String

age

21

Integer

height

5.9

Float

We'll learn data types in more detail later.

Changing a Variable's Value

A variable's value can be changed during program execution.

age = 20
print(age)

age = 21
print(age)

Output:

20
21

The variable age first stores 20, and later we replace that value with 21.

Multiple Variables

You can create multiple variables in a program:

name = "John"
age = 20
city = "Delhi"

print(name)
print(age)
print(city)

You can also assign multiple variables in one line:

name, age, city = "John", 20, "Delhi"

This is perfectly valid Python.

Variable Naming Rules

Python has some rules for naming variables.

Valid Names

name = "John"
student_age = 20
totalMarks = 95
age2 = 25

Invalid Names

2age = 20       # Cannot start with a number
student-name = "John"  # Hyphen is not allowed

A variable name:

  • Can contain letters, numbers, and underscores (_).

  • Cannot start with a number.

  • Cannot contain spaces.

  • Is case-sensitive.

  • Cannot use Python keywords such as if, for, or class.

It's also a good practice to use meaningful names:

student_age = 20

is much easier to understand than:

x = 20

Variables are Case-Sensitive

Remember that Python treats uppercase and lowercase letters differently.

name = "John"
Name = "Jason"

These are two different variables.

print(name)
print(Name)

Output:

John
Jason

Assigning Values to Variables

Variables can store different kinds of values:

name = "John"
age = 20
price = 99.99
is_student = True

Python doesn't require you to write something like int age or string name. The value itself tells Python what type it is.

Using Variables in Calculations

Variables become especially useful when working with calculations.

price = 100
quantity = 3

total = price * quantity

print(total)

Output:

300

Instead of writing 100 * 3 directly, we store the values in variables. This makes the program easier to understand and modify.

Checking the Type of a Variable

You can use Python's type() function to find out what type of value a variable contains.

age = 20
name = "John"

print(type(age))
print(type(name))

Output:

<class 'int'>
<class 'str'>

We'll explore these types properly when we learn Python Data Types.

A Practical Example

Let's say we're creating a small program to store a student's information:

name = "John"
age = 20
marks = 85

print("Name:", name)
print("Age:", age)
print("Marks:", marks)

Output:

Name: John
Age: 20
Marks: 85

This is the basic idea behind variables: store information, give it a meaningful name, and use that information whenever needed.