What is a Variable in Python: A Comprehensive Guide for 2026

| Summary: Python variables let you store and work with data that can change as your program runs. Using meaningful variable names and assigning appropriate values makes your code easier to read and maintain. It is also important to understand how Python handles different data types and where variables can be accessed within a program. These basics become useful when working with functions, loops, classes, and larger Python programs. |
If you are starting with Python, understanding variables is one of the first concepts you will need to learn. However, what is a variable in Python, and how does it work? Simply put, a variable lets you refer to a value in your program. You can assign a value to a variable and later use or change it as needed.
Python handles variables differently from languages that require you to declare a data type before assigning a value. This makes variable assignment relatively simple for beginners. In this article, you will learn how to create and assign variables, follow Python’s naming rules, understand common variable types, and see how variables are used in Python programs.
What is a Variable in Python with an Example?
A variable in Python is a name that refers to an object or value stored in memory. You can use the variable to access that value throughout your program and assign it a different value when needed. Python does not require you to declare a variable’s data type before assigning a value. It determines the type automatically based on the object assigned to the variable. For example:
| //Pythonname = “Riya”age = 22height = 5.6 |
Here, name, age, and height are variables. They indicate an integer, string, and floating-point value.
You can use these variables in other parts of your program:
name = “Riya”age = 22print(name)print(age) Output:Riya22 |
The value assigned to a variable can also change as the program runs. For example:
age = 22age = 23 print(age)Output:23 |
In this example, ‘age’ first refers to 22 and is later assigned the value 23. This makes variables useful for information that can change during program execution.
You can also assign values to multiple variables in a single statement:
| name, age, city = “Riya”, 22, “Delhi” |
Python assigns each value to the corresponding variable. It can shorten your code when you need to initialize several variables at once. Understanding variables matters because you use them throughout Python programs. You will work with them when taking user input, performing calculations, storing data, and working with functions, loops, and other Python concepts.


How Do Variables Work in Python?
To learn more about variables in Python, it helps to understand how Python stores and uses the values assigned to them. When you assign a value to a variable, Python associates the variable name with an object containing that value. You can then use the variable name to access the value in your program. For example:
| score = 85print(score) |
Here, ‘score’ refers to the integer value 85. When Python executes print(score), it retrieves the value associated with score and displays it.
Python creates variables when you assign a value to them. You do not need to specify the data type separately. For example:
| course = “Python”duration = 8 |
In this example, Python identifies course as a string and duration as an integer based on the values you assign.
You can understand how Python variables work by looking at how they are reassigned, how their data types can change, and how they are used in expressions. Here’s an overview:
i. Reassigning Variables
Python allows you to update the value of a variable during program execution. For example:
| price = 500price = 450 print(price) Output:450 |
After the second assignment, ‘price’ refers to 450 instead of 500. This lets variables represent data that changes as your program runs.
ii. Variables Can Store Different Data Types
Python is dynamically typed so that the same variable name can be assigned values of different types at different points in a program. For example:
| value = 100value = “Python” print(value) Output:Python |
The variable ‘value’ first refers to an integer and later refers to a string. While Python allows this, using a variable consistently can improve your code’s readability.
iii. Variables and Expressions
You can also use variables in expressions to perform calculations or combine values. For example:
| price = 500discount = 50 final_price = price – discount print(final_price) Output:450 |
Here, Python uses the values associated with ‘price’ and ‘discount’ to calculate ‘final_price.’ This is why variables are useful in practical programs. Instead of repeatedly writing the same values, you can give them meaningful names and use those names wherever you need them.
Read More: Exploring career options in Python. Check out our guide on Python developers’ average salary in India to explore the right role for yourself.
Rules for Naming Variables in Python
Now, you may already understand what a variable is in Python programming and how to create one. Further, explore the rules for naming them. Every Python program works with data, whether it’s a customer’s name, a product’s price, or a game score. Variables hold that data so you can reuse it. Creating one takes a single line: choose a name, use the = sign, and assign a value. No setup step and no need to declare a type, because Python figures it out from the value.
This section walks through the process step by step, from choosing a valid name to assigning several variables at once, updating their values, and following best practices. By the end, you’ll be able to create and manage variables confidently in your own programs.
Step 1: Choose a Valid Variable Name
Pick a name that follows Python’s naming rules:
- Start with a letter or underscore, never a digit
- Avoid reserved keywords such as if, for, or class
- Remember that names are case-sensitive, so price and Price are different variables
| user_name = “Asha” # valid_count = 5 # valid2nd_place = “Ravi” # invalid: starts with a digit |
Step 2: Use the Assignment Operator
On the left, write the name, then the ‘=’ sign in the middle, and state the value on the right. Python reads this as ‘store the value on the right under the name on the left.’
| product = “Notebook”price = 149.50quantity = 3in_stock = True |
Step 3: Check the Value and Type
Use print() to see what a variable holds and type() to see what kind of data it is.
| print(product) # Notebookprint(type(price)) # <class ‘float’>print(type(in_stock)) # <class ‘bool’> |
Step 4: Assign Multiple Variables at Once
Python has shortcuts for creating multiple variables in a single statement.
Different values:
| x, y, z = 1, 2, 3 |
The same value:
| a = b = c = 0 |
Unpacking a list:
| colors = [“red”, “green”, “blue”]first, second, third = colors |
Step 5: Update a Variable Using Its Current Value
Augmented assignment operators change a variable based on its current value.
| count = 5count += 2 # 7count -= 1 # 6count *= 3 # 18count //= 4 # 4 |
Step 6: Swap Two Variables
Python lets you swap values without a temporary variable.
| a, b = 5, 10a, b = b, aprint(a, b) # 10 5 |
Step 7: Delete a Variable (Optional)
If you no longer need a variable, remove it with del.
| temp = 100del temp # temp no longer exists |
Step 8: Add Type Hints (Optional)
Python doesn’t enforce types, but hints make code easier to read and help editors catch mistakes.
| title: str = “Python Basics”pages: int = 250 |
Types of Variables in Python
Python variables can behave differently depending on where you define them and how you use them. A variable created inside a function has a different scope from one defined outside the function. Similarly, variables defined within a class can be shared across objects created from that class.
Two important types of variables in Python are:
What Is a Global Variable in Python?
A global variable is defined outside a function or class and can be accessed from different parts of the program. You can access its value inside a function, but you need the global keyword to modify it.
Here is an example of what a global variable is in Python:
| name = “Priya” def greet(): print(name) greet() |
Here, name is a global variable because you defined it outside the greet() function. The function can access its value directly.
What Is a Class Variable in Python?
A class variable is defined inside a class but outside its methods. It belongs to the class and is generally shared by all objects created from that class.
Here is an example of what a class variable is in Python:
| class Student: school = “ABC School” student1 = Student()student2 = Student() print(student1.school)print(student2.school) |
Here, school is a class variable. Both student1 and student2 can access the same school value because the variable belongs to the Student class.


Conclusion
Knowing what a variable is in Python gives you a strong foundation for working with data in your programs. Variables allow you to store information, reuse values, and update data as your code runs. You have learned how to create and assign variables, follow naming rules, and understand global and class variables. You also learned how to change variable values when needed. These concepts are essential for writing and understanding Python programs. Once you are comfortable with variables, use them with functions, loops, conditional statements, lists, and other Python concepts as you continue developing your programming skills.
Check out our blog to find the best Python learning platform for you and level up your knowledge.
FAQs
Yes, you can assign a new value to the same variable. For example, age = 25 followed by age = 30 changes age to 30.
A global variable is defined outside functions or classes and can be accessed from different parts of a program. A class variable is defined inside a class and is shared by objects created from that class.
A variable name should begin with a letter or an underscore. It cannot have spaces, special characters, or reserved Python keywords. Variable names are also case-sensitive.
Yes. Multiple variables can store or refer to the same value. For example, x = 10 and y = 10 are both valid assignments.
