What Are Variables in Python?
Variables are one of the most important concepts in Python programming. A variable is a name used to store data in a program.
You can think of a variable as a container that holds information. This information can be a number, text, decimal value, or other types of data.
Example:
name = "Souvik" age = 25 salary = 130000
Here, name stores the text Shouvik. The age stores the integer value 25. And salary stores an integer value 130000.
Python automatically determines the data type based on the assigned value.
How to Create Variables in Python
Creating a variable in Python is simple. You just need to write a variable name, followed by an equal sign (=), and then the value.
Syntax:
variable_name = value
Example:
city = "Bhopal" temperature = 30 price = 99.50
Note: In Python, you do not need to declare the variable type before assigning a value.
Rules for Naming Variables
Python has certain rules for naming variables:
- A variable name must start with a letter or underscore.
- It cannot start with a number.
- It can contain letters, numbers, and underscores.
- Variable names cannot contain spaces.
- Python keywords cannot be used as variable names.
- Variable names are case-sensitive.
Valid Variable Names
name = "Amit" student_age = 20 total_marks = 450 _price = 100
Invalid Variable Names
2name = "Amit" student age = 20 class = "Python"
These examples are invalid because they violate Python's variable naming rules.
- 2name = "Amit": It cannot start with a number. In Python, a variable name must start with a letter (A–Z or a–z) or an underscore (_).
- student age = 20: Python variable names cannot contain spaces between words.
- class = "Python": Python has reserved keywords that have special meanings in the language. These keywords cannot be used as variable names.
Assigning Multiple Variables
Python allows you to assign values to multiple variables in one statement.
Example:
name, age, city = "Max", 22, "New York" print(name) print(age) print(city)
Output:
Max
22
New York
You can also assign the same value to multiple variables.
a = b = c = 10 print(a, b, c)
Output:
10 10 10
Changing Variable Values
Variables can be updated whenever required. Assigning a new value to an existing variable replaces its previous value.
Example:
marks = 80 print(marks) marks = 95 print(marks)
Output:
80
95
The second assignment replaces the previous value stored in the variable.
Understanding Dynamic Typing
Python is a dynamically typed language. This means you do not have to specify the data type when creating a variable.
Example:
value = 100 print(value) value = "Python" print(value)
Output:
100
Python
The same variable can store different types of values at different times.
Checking Variable Data Types
Python provides the type() function to identify the data type stored in a variable.
Example:
name = "Anita" age = 24 price = 150.50 print(type(name)) print(type(age)) print(type(price))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
Conclusion
Variables are essential for storing and managing information in Python programs. They allow developers to work with different values, update information, and perform calculations.
Understanding variables and their naming rules is an important step toward learning Python programming. Practice creating variables with different data types to improve your understanding.
