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 = 20Here:
nameis a variable storing"John".ageis a variable storing20.
Think of a variable like a label attached to a value.
name → "John"
age → 20We can then use these variables whenever we need their values.
name = "John"
age = 20
print(name)
print(age)Output:
John
20Creating Variables
Python makes creating variables very simple. You don't need to specify the data type separately.
name = "John"
age = 21
height = 5.9Python automatically determines the type of value being stored.
For example:
Variable | Value | Type |
|---|---|---|
|
| String |
|
| Integer |
|
| 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
21The 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 = 25Invalid Names
2age = 20 # Cannot start with a number
student-name = "John" # Hyphen is not allowedA 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, orclass.
It's also a good practice to use meaningful names:
student_age = 20is much easier to understand than:
x = 20Variables 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
JasonAssigning Values to Variables
Variables can store different kinds of values:
name = "John"
age = 20
price = 99.99
is_student = TruePython 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:
300Instead 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: 85This is the basic idea behind variables: store information, give it a meaningful name, and use that information whenever needed.