Variables in Python
A variable is a name used to store a value in a program. Think of a variable as a container that holds some data.
Example
name = "Uday" age = 21
Here:
nameis a variable"Uday"is the valueageis a variable21is the value
We can use the variables later:
name = "Uday" age = 21 print(name) print(age)
Output:
Uday 21
1. Creating Variables
Python does not require you to declare the variable type separately.
name = "Ram" age = 20 height = 5.8
Python automatically determines the data type.
name = "Ram" # string age = 20 # integer height = 5.8 # float
2. Assigning Values
Use the = operator to assign a value.
x = 10 y = 20
You can change the value later:
x = 10 x = 50 print(x)
Output:
50
3. Different Types of Variables
String
Stores text.
name = "Rahul"
Integer
Stores whole numbers.
age = 20
Float
Stores decimal numbers.
price = 99.50
Boolean
Stores True or False.
is_student = True
Example:
name = "Rahul" age = 20 height = 5.7 is_student = True print(name) print(age) print(height) print(is_student)
4. Multiple Variables
You can create multiple variables in one line:
name, age, city = "Ram", 20, "Janakpur"
You can also assign the same value to multiple variables:
x = y = z = 100
5. Variable Naming Rules
Python variable names must follow some rules.
Valid names
name = "Ram" student_name = "Ram" age2 = 20 _total = 500
Invalid names
2name = "Ram" # Cannot start with a number student-name = "Ram" # Hyphen is not allowed class = 10 # Reserved keyword
Important rules
- A variable can contain letters, numbers, and underscores.
- It cannot start with a number.
- It cannot contain spaces.
- Python variable names are case-sensitive.
- Avoid Python keywords.
6. Case Sensitivity
Python treats uppercase and lowercase names as different variables.
name = "Ram" Name = "Shyam" print(name) print(Name)
Output:
Ram Shyam
So:
name Name NAME
are three different variables.
7. Check the Type of a Variable
Use the type() function.
age = 20 print(type(age))
Output:
<class 'int'>
Example:
name = "Ram" price = 99.5 is_student = True print(type(name)) print(type(price)) print(type(is_student))
8. Changing the Data Type
A variable can hold different types of values at different times.
x = 10 print(x) x = "Hello" print(x)
Output:
10 Hello
Python is dynamically typed, meaning you don't have to specify the variable's type when creating it.
9. Variable Naming Convention
A common Python style is snake_case.
student_name = "Ram" student_age = 20 total_marks = 450
Instead of:
studentname = "Ram" studentAge = 20 TotalMarks = 450
For Python code, prefer:
student_name student_age total_marks
10. Variables with Calculations
Variables can be used in mathematical operations.
a = 10 b = 20 sum = a + b print(sum)
Output:
30
Another example:
price = 100 quantity = 5 total = price * quantity print(total)
Output:
500
11. Getting User Input
Variables are commonly used to store information entered by the user.
name = input("Enter your name: ") print("Hello", name)
If the user enters:
Ram
Output:
Hello Ram
Important
input() normally returns a string.
For numbers, use type conversion:
age = int(input("Enter your age: ")) print(age)
12. Simple Real-Life Example
student_name = "Rahul" student_age = 20 student_marks = 85.5 is_passed = True print("Name:", student_name) print("Age:", student_age) print("Marks:", student_marks) print("Passed:", is_passed)
Output:
Name: Rahul Age: 20 Marks: 85.5 Passed: True
In simple words
Variable = Name + Value
student_name → "Rahul" student_age → 20 student_marks → 85.5
Variables allow a Python program to store, use, modify, and process data.