What is a Variable in Python?

A variable in Python is a name given to a memory location used to store data. Consider variables as labeled containers with values that can be used and modified throughout your program. Whether you are storing numbers, strings, or more complex data, Python variables act as a means to label and access that information easily.
Unlike other programming languages that require you to declare a variable’s type before using it, Python does not require explicit declaration. A variable is automatically created when you assign it a value. For example:
x = 5
name = "Bhrighu"
Here, x is an integer variable holding the value 5, while the name is a string variable holding the value "Bhrighu". Python infers the data type based on the value assigned to a concept known as dynamic typing.
Understanding variables is crucial to mastering Python, as they are the building blocks of every Python program. Working with variables is fundamental, whether you’re managing user input, processing data, or building complex algorithms.
How Variables Work in Python?
One of Python’s powerful features is its dynamically typed nature. This means you don’t have to declare the data type of a variable; Python determines it at runtime based on the value assigned.
For example:
x = 10 # x is an integer
x = "hello" # now x is a string
Here, x first holds an integer and then a string, showing that variables in Python can change type over time. Python handles memory management and data typing internally, making the development process smoother and less error-prone for beginners.
In technical terms, Python variables are references to objects stored in memory. When you assign a value to a variable, Python creates an object, and the variable refers to it. If another variable is assigned the same value, both may reference the same object, especially for immutable types like strings or integers.
a = 5
b = a
print(id(a), id(b)) # Same memory ID for both
This efficient memory management is part of Python’s design philosophy. It makes code more readable and reduces the overhead of manual type declarations.
Rules for Naming Variables in Python
While Python offers flexibility, naming variables correctly is essential to ensure your code runs without errors. Here are the rules and conventions for naming variables:
Syntax Rules
Must begin with a letter (a–z, A–Z) or an underscore (_)
Can include letters, numbers, and underscores (e.g., student_name1)
Cannot use Python reserved keywords (e.g., class, def, return)
Variable names are case-sensitive (score, Score, and SCORE are different)
Valid | Invalid |
---|---|
total_marks | 1st_score |
_student_id | class(keyword) |
marks2025 | full-name |
Naming Conventions
Use snake_case for variable names: total_score
Avoid overly short names like x or y unless in a limited scope
Use meaningful names to enhance code readability
Understanding these rules not only helps in avoiding syntax errors but also improves collaboration in team-based coding environments.
Python Data Types and Variables
Python supports several built-in data types, and variables can hold values of any of these. Here’s a breakdown of the most common types with examples:

Integer
Integers are represented by int class and contain positive and negative whole numbers (without fractions or decimals). Unlike in C or Java, in Python, there is no limit to how long an integer value can be.
age = 25
Float
Floating numbers are represented by float class. Float data type can hold a real number with floating point representation. It is specified by a decimal point and optionally, the character e or E followed by a positive or a negative integer.
price = 19.99
String
Strings are arrays of bytes representing Unicode characters. In Python, there is no character data type. Strings in Python can be created by using single quotes, double quotes, or even triple quotes.
name = "Alice"
Boolean
True and False are the two built-in values for the Python data type. Boolean objects that equal False are false (false), whereas those that equal True are truthy (true). But it is also possible to evaluate non-Boolean items in a Boolean environment and determine whether they are true or false. The class bool is used to represent it.
is_active = True
List
Lists are ordered collections of data, similar to arrays, which are declared in other languages. Because items in a list do not have to be of the same type, they are incredibly flexible. To create a list in Python, simply put the sequence inside square brackets [].
colors = ["red", "blue", "green"]
Tuple
A tuple is an ordered collection of Python objects, just like a list. The immutability of tuples is the only distinction between them and lists. Once formed, tuples cannot be changed. Although it is a little more difficult, tuples can also be made with just one element. One element in parentheses is insufficient to constitute a tuple; a following "comma" is required.
coordinates = (10, 20)
Dictionary
In Python, a dictionary is a group of data values that are used to store data values, like a map. Unlike other Python data types, which only store one value as an element, a dictionary stores a key-value pair. To make the dictionary more efficient, key-value pairs are provided. In a dictionary, a colon (:) separates each key-value pair, while a comma separates each key.
student = {"name": "John", "age": 20}
Python automatically determines the data type at assignment, simplifying the coding process. You can verify the type of a variable using the type() function:
print(type(name)) # Output:
Variable Assignment Techniques
Python provides several ways to assign values to variables:

Single Assignment
x = 10
Multiple Assignment (Same Values)
x = y = z = 100
All three variables will reference the value 100.
Multiple Assignment (Different Values)
a, b, c = 1, 2, 3
Each variable gets a distinct value in a single line, a technique that enhances code brevity.
Type Conversion
Python allows changing data types using type-casting functions:
x = "25"
y = int(x) # Convert string to integer
This is useful when working with user input or file data, which often comes in string format.
Variable Scope in Python
Scope refers to the region in a program where a variable is accessible.
Local Variables
Declared inside a function and accessible only within it.
def greet():
message = "Hello"
print(message)
greet()
# print(message) # This will raise an error
Global Variables
Declared outside functions and accessible throughout the program.
name = "Alex"
def say_hello():
print("Hello", name)
say_hello()
Using global Keyword
Allows a function to modify a global variable:
count = 0
def increment():
global count
count += 1
increment()
print(count) # Output: 1
Understanding variable scope helps prevent unintended modifications and improves code reliability.
Learning Python Variables with Bhrighu Academy
At Bhrighu Academy, we believe in teaching Python through hands-on, industry-relevant training. Our Python programming courses start with the basics of variable declaration, data types, and scope, ensuring learners build a strong foundation.
With guided exercises, real-world projects, and expert mentorship, students learn how variables work and apply them in practical scenarios such as web development, automation, and data analysis.
Whether you are a beginner or an experienced coder looking to strengthen your Python skills, our structured learning paths offer the clarity and direction you need.
Python for Beginners: Variables, Data Types, and Control Flow
Python Projects: From Basics to Real-World Applications
Advanced Python with Data Structures and OOP
Start your Python journey with us and gain the confidence to write clean, efficient code from day one.
Conclusion
Variables are at the heart of Python programming. They enable you to store, manipulate, and retrieve data effectively. From simple numbers and strings to complex data structures, Python variables support a wide range of data types, all managed dynamically without manual declarations.
To recap:Python variables are created when you assign values.
They point to memory objects and can change types.
Follow naming rules to avoid syntax errors.
Understand data types, assignment methods, and variable scope for more efficient coding.
Mastering these fundamentals is essential for building robust Python applications. Ready to dive deeper? Want to go from writing variables to building real-world Python apps? Join Bhrighu Academy’s hands-on courses with mentorship, project-based learning, and certification support.
Frequently Asked Questions about Python Variables
What are __ variables in Python?
__ variables in Python, also called "dunder" (double underscore) variables, are used for special purposes. For example, __name__ determines if a file is run directly or imported. These variables often indicate system-defined or private attributes and methods used internally by Python to support various functionalities.
Is __init__ a variable?
No, __init__ is not a variable; it is a special method in Python classes known as a constructor. It is automatically called when a class object is created, allowing you to initialize object properties. The double underscores signal that it's a built-in Python method, not a regular variable.
Is name a variable in Python?
Yes, name can be a user-defined variable in Python, such as name = "Alice". However, __name__ is a special built-in variable that determines how a script is being executed—whether directly or imported as a module—making it useful for controlling code behavior during execution.
Why use __ in Python?
Double underscores (__) are used in Python to define special methods (like __init__) or system-defined variables (like __name__). They distinguish built-in functionality from user-defined elements, enhance code readability, and enforce naming conventions for private or special-use methods and attributes within classes or modules.