Variables
Variables are one of the most important concepts in Python programming. A variable is used to store data that can be used later in a program.
What is a Variable?
A variable is a name that refers to a value stored in memory. In Python, you do not need to declare the variable type separately.
Python determines the type automatically based on the value assigned to the variable.
Creating a Variable
A variable is created when you assign a value using the assignment operator =.
name = "Samadhan"
age = 25
course = "Python"
How Variable Assignment Works
name
=
"Samadhan"
Printing Variables
Once a value is stored in a variable, we can use the variable name to access that value.
name = "Samadhan"
age = 25
print(name)
print(age)
Output
Samadhan
25
Multiple Variables
Python allows you to create multiple variables in the same program.
name = "Samadhan"
age = 25
salary = 45000.50
is_student = True
Multiple Assignment
Python allows multiple variables to be assigned values in a single statement.
name, age, course = "Samadhan", 25, "Python"
print(name)
print(age)
print(course)
Output
Samadhan
25
Python
Assigning the Same Value
The same value can be assigned to multiple variables.
x = y = z = 100
print(x)
print(y)
print(z)
Output:
100
100
100
Checking the Variable Type
The type() function can be used to find the data type of a variable.
name = "Samadhan"
age = 25
print(type(name))
print(type(age))
Output
<class 'str'>
<class 'int'>
Dynamic Typing in Python
Python is dynamically typed. This means the type of a variable can change when a different type of value is assigned to it.
value = 100
print(value)
value = "Python"
print(value)
Output
100
Python
Variable Naming Rules
Python has some rules that should be followed when creating variable names.
- A variable name can contain letters, numbers, and underscores.
- A variable name cannot start with a number.
- Variable names are case-sensitive.
- Spaces should not be used in variable names.
- Python keywords should not be used as variable names.
Valid and Invalid Variable Names
| Variable | Status | Reason |
|---|---|---|
| student_name | Valid | Uses letters and underscore |
| age25 | Valid | Number is not at the beginning |
| student name | Invalid | Contains a space |
| 25age | Invalid | Starts with a number |
Variables are Case-Sensitive
Python treats uppercase and lowercase letters as different characters.
name = "Samadhan"
Name = "CIIT"
print(name)
print(Name)
Output
Samadhan
CIIT
Example 🌍🔓
Let's create variables for a student learning Python at CIIT Training Institute 👀❤️...!.
student_name = "Samadhan"
institute = "CIIT Training Institute 👀❤️...!"
course = "Python Full Stack 🖥️"
experience = 0
print("Student:", student_name)
print("Institute:", institute)
print("Course:", course)
print("Experience:", experience)
Output:
Student: Samadhan
Institute: CIIT Training Institute 👀❤️...!
Course: Python Full Stack 🖥️
Experience: 0
CIIT Learning Point
Use meaningful variable names. A good variable name makes your code easier to understand, maintain, and debug. Prefer names such as student_name instead of unclear names such as x.
CIIT Practice Task
Create variables for your name, age, city, and favorite programming language. Display all the values using print().
name = "Samadhan"
age = 25
city = "Pune"
language = "Python"
print("Name:", name)
print("Age:", age)
print("City:", city)
print("Language:", language)
Output:
Name: Samadhan
Age: 25
City: Pune
Language: Python
Summary :
Variables are used to store and work with data in Python programs. You learned how to create variables, assign multiple values, check variable types, understand dynamic typing, and follow proper variable naming rules. Meaningful variables make Python programs easier to read and maintain.