If you are learning Python for the first time, one of the first concepts you need to understand is variables.
Variables are used in almost every Python program. They allow us to store information such as numbers, names, marks, prices, and other values so that we can use them later in the program.
In this article, we will understand what variables are, how to create them, how to assign values, how to change values, and how variables are used in Python programs.
What Is a Variable in Python?
A variable is a name used to refer to a value stored in a program.
For example:
name = "Rahul"
age = 20
Here:
nameis a variable."Rahul"is the value stored inname.ageis a variable.20is the value stored inage.
We can use these variables later:
print(name)
print(age)
Output:
Rahul
20
So, a simple way to understand a variable is:
Variable = Name given to a value
Why Do We Need Variables?
Imagine you are writing a student management program.
You may need to store:
Student name
Student age
Student roll number
Student marks
Student percentage
Instead of writing the actual values repeatedly, we can store them in variables.
Example:
name = "Amit"
age = 19
marks = 85
Now we can use these values whenever required.
print(name)
print(age)
print(marks)
This makes programs easier to write, understand, and modify.
How to Create a Variable in Python
Python makes creating variables very simple.
The general syntax is:
variable_name = value
For example:
x = 10
Here:
x→ variable name=→ assignment operator10→ value
Another example:
city = "Kolkata"
Here, city stores the value "Kolkata".
Variable Assignment
Giving a value to a variable is called assignment.
Example:
number = 50
The value 50 is assigned to the variable number.
We can then use:
print(number)
Output:
50
Variables Can Store Different Values
Python variables can store different kinds of values.
For example:
name = "Shivam"
age = 20
percentage = 85.5
Here:
namestores text.agestores a whole number.percentagestores a decimal number.
Python determines the type of the value automatically.
Changing the Value of a Variable
A variable's value can be changed during program execution.
For example:
age = 20
print(age)
age = 21
print(age)
Output:
20
21
Initially, age contains 20.
Later, we assign 21 to the same variable.
Therefore, the value of the variable changes.
Example: Storing Student Information
Consider this program:
name = "Rahul"
roll = 15
marks = 87
print("Name:", name)
print("Roll:", roll)
print("Marks:", marks)
Output:
Name: Rahul
Roll: 15
Marks: 87
Here, three variables are used:
name
roll
marks
Each variable stores different information.
Multiple Variables in Python
Python allows us to create multiple variables in a program.
Example:
name = "Anita"
age = 20
city = "Kolkata"
We can display all of them:
print(name)
print(age)
print(city)
Output:
Anita
20
Kolkata
Assigning Multiple Variables
Python also allows multiple variables to be assigned in a single statement.
Example:
a, b, c = 10, 20, 30
Now:
a = 10
b = 20
c = 30
We can print them:
print(a)
print(b)
print(c)
Output:
10
20
30
This is a convenient feature of Python.
Assigning the Same Value to Multiple Variables
The same value can also be assigned to multiple variables.
Example:
a = b = c = 100
Now all three variables contain 100.
print(a)
print(b)
print(c)
Output:
100
100
100
Variables Can Be Used in Calculations
Variables become particularly useful when performing calculations.
For example:
a = 10
b = 20
sum = a + b
print(sum)
Output:
30
The program stores values in a and b and then uses those values to calculate their sum.
Another example:
length = 10
breadth = 5
area = length * breadth
print(area)
Output:
50
Taking Input and Storing It in a Variable
Variables are also commonly used with the input() function.
Example:
name = input("Enter your name: ")
print("Hello", name)
If the user enters:
Rahul
The output will be:
Hello Rahul
Here, the value entered by the user is stored in the variable name.
Example: Add Two Numbers
Variables can be used to create a simple calculator.
a = 10
b = 20
sum = a + b
print("Sum =", sum)
Output:
Sum = 30
The calculation can also be performed using input:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)
For example, if the user enters:
Enter first number: 15
Enter second number: 25
Output:
Sum = 40
Important Rules for Variable Names
While creating variables, you should use proper variable names.
For example:
student_name = "Rahul"
student_age = 20
These names clearly describe the information being stored.
Good variable names make programs easier to understand.
Examples of Good Variable Names
Some meaningful variable names are:
student_name = "Amit"
student_age = 20
total_marks = 450
average_marks = 90
phone_number = 9876543210
These names clearly describe their purpose.
Avoid Unclear Variable Names
Consider:
x = "Rahul"
a = 20
p = 450
These names may work, but it is difficult to understand what they represent.
A better version would be:
student_name = "Rahul"
student_age = 20
total_marks = 450
Good naming is especially important when programs become large.
Variables Are Case-Sensitive
Python variable names are case-sensitive.
For example:
name = "Rahul"
Name = "Amit"
Python treats name and Name as two different variables.
Example:
name = "Rahul"
Name = "Amit"
print(name)
print(Name)
Output:
Rahul
Amit
Therefore, uppercase and lowercase letters matter in Python.
A Variable Can Store the Result of an Expression
A variable does not only have to store a direct value.
It can also store the result of a calculation.
Example:
a = 10
b = 20
result = a + b
Here, result stores the result of the expression:
a + b
Therefore:
print(result)
Output:
30
Example: Calculate Rectangle Area
Let's use variables in a practical program.
length = 20
breadth = 10
area = length * breadth
print("Length =", length)
print("Breadth =", breadth)
print("Area =", area)
Output:
Length = 20
Breadth = 10
Area = 200
This example shows how variables can store both input values and calculated results.
Common Mistakes with Variables
1. Using a variable before assigning a value
For example:
print(age)
If age has not been created earlier, Python will produce an error.
We should first assign a value:
age = 20
print(age)
2. Confusing uppercase and lowercase
Remember:
name = "Rahul"
Name = "Amit"
These are different variables.
3. Using unclear names
Instead of:
x = 500
you may use:
total_marks = 500
The second version is easier to understand.
Real-Life Example of Variables
Suppose we are developing a student result program.
We could use:
student_name = "Rahul"
english = 80
maths = 90
computer = 95
total = english + maths + computer
print("Student:", student_name)
print("Total Marks:", total)
Output:
Student: Rahul
Total Marks: 265
Here variables are used to store the student's name, marks, and calculated total.
This is one of the main reasons variables are so important in programming.
Quick Revision
Let's summarize what we learned.
| Concept | Example |
|---|---|
| Creating a variable | age = 20 |
| Text value | name = "Rahul" |
| Decimal value | price = 99.50 |
| Changing a value | age = 21 |
| Multiple assignment | a, b = 10, 20 |
| Same value | a = b = 100 |
| Calculation | total = a + b |
| User input | name = input() |
Practice Questions
Try solving these programs yourself.
Question 1
Create variables to store your name, age, and city and display them.
Question 2
Create two variables containing two numbers and find their sum.
Question 3
Create variables for length and breadth and calculate the area of a rectangle.
Question 4
Create variables for three subject marks and calculate the total marks.
Question 5
Create a variable containing your age. Change its value and display both values.
Final Thoughts
Variables are one of the fundamental building blocks of Python programming.
Whenever a program needs to remember information, variables provide a simple way to store and use that information.
Once you understand variables, concepts such as data types, input, calculations, conditions, loops, and functions become much easier to understand.
So, don't just memorize the syntax. Try creating small programs using variables and experiment with different values.
Comments
Post a Comment