Variables are one of the most fundamental concepts in any programming language. In Python, variables are used to store data that can be referenced and manipulated in a program. This section will cover the various aspects of Python variables, including their types, naming conventions, scope, and best practices.
What is a Variable?
A variable in Python is a name that refers to a memory location where data is stored. The value of a variable can change throughout the execution of a program. Variables are essential for writing flexible and dynamic code.
Creating and Assigning Variables
In Python, variables are created when you assign a value to them using the assignment operator (=).
x = 10
y = "Hello"
z = 3.14
In the examples above:
xis assigned the integer value10.yis assigned the string value"Hello".zis assigned the float value3.14.
Variable Naming Conventions
Python has specific rules and conventions for naming variables:
- Must start with a letter or an underscore (_):
- Valid:
_my_var,myVar - Invalid:
1myVar,-myVar
2. Can only contain alphanumeric characters and underscores:
- Valid:
var1,my_var - Invalid:
my-var,my var
3. Case-sensitive:
myVarandmyvarare considered different variables.
4. Cannot use Python reserved keywords: Examples of reserved keywords are class, finally, return, is, try, except, etc.
Dynamic Typing
Python is a dynamically typed language, meaning you don’t have to declare the type of a variable explicitly. The type is inferred based on the value assigned.
a = 5 # a is an integer
a = "Hello" # a is now a string
a = 3.14 # a is now a float
Multiple Assignments
You can assign values to multiple variables in a single statement.
a, b, c = 1, 2, "Hello"
You can also assign the same value to multiple variables simultaneously.
x = y = z = 0
Variable Scope
The scope of a variable determines where it can be accessed in the program. Python has three types of variable scopes:
- Local Scope: Variables declared inside a function are local to that function.
- Global Scope: Variables declared outside all functions are global and can be accessed anywhere in the program.
- Nonlocal Scope: Variables declared inside nested functions that are not in the local or global scope.

x = "global"
def outer_function():
x = "outer local"
def inner_function():
# nonlocal x
x = "inner local"
print("Inner:", x)
inner_function()
print("Outer:", x)
outer_function()
print("Global:", x)
Global Keyword
If you need to modify a global variable inside a function, you can use the global keyword.
x = 10
def change_global():
global x
x = 20
change_global()
print(x) # Output: 20
Deleting Variables
You can delete a variable using the del keyword.
a = 10
del a
# print(a) would raise a NameError since 'a' is deleted
Best Practices for Using Variables
- Meaningful Names: Use descriptive and meaningful variable names to make your code more readable.
num_students = 50
avg_score = 75.5
- Consistency: Stick to a naming convention throughout your code. Common conventions are:
- snake_case:
my_variable - camelCase:
myVariable - PascalCase:
MyVariable
- Avoid Single Letter Names: Except for in temporary or loop variables.
for i in range(10):
pass
- Use Comments: Add comments to explain the purpose of variables if it’s not immediately clear.
interest_rate = 0.05 # Annual interest rate in percentage
Discover more from Learn with Anu Arora
Subscribe to get the latest posts sent to your email.
