Variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value.
- Unlike Java and many other languages, Python variables do not require explicit declaration of type.
- Type of the variable is inferred based on the value assigned.
x = 5
name = "Samantha"
print(x)
print(name)
Output
5 Samantha
Rules for Naming Variables
To use variables effectively, we must follow Python’s naming rules:
- Variable names can only contain letters, digits and underscores (_).
- A variable name cannot start with a digit.
- Variable names are case-sensitive like myVar and myvar are different.
- Avoid using Keywords like if, else, for as variable names.
Below listed variable names are valid:
age = 21
_colour = "lilac"
total_score = 90
Below listed variables names are invalid:
1name = "Error" # Starts with a digit
class = 10 # 'class' is a reserved keyword
user-name = "Doe" # Contains a hyphen
Assigning Values to Variables
Basic Assignment: Variables are assigned values using the = operator.
x = 5
y = 3.14
z = "Hi"
Dynamic Typing: variables are dynamically typed, meaning the same variable can hold different types of values during execution.
x = 10
x = "Now a string"
Multiple Assignments
Assigning Same Value: allows assigning the same value to multiple variables in a single line, which can be useful for initializing variables with the same value.
a = b = c = 100
print(a, b, c)
Output
100 100 100
Assigning Different Values: we can assign different values to multiple variables simultaneously, making the code concise and easier to read.
x, y, z = 1, 2.5, "Python"
print(x, y, z)
Output
1 2.5 Python
Concept of Object Reference
Let us assign a variable x to value 5.
x = 5
When x = 5 is executed, Python creates an object to represent the value 5 and makes x reference this object.

Now, let's assign another variable y to the variable x.
y = x
This statement creates y and references the same object as x, not x itself. This is called a Shared Reference, where multiple variables reference the same object.

Now, if we write
x = 'Geeks'
Python creates a new object for the value "Geeks" and makes x reference this new object.

The variable y remains unchanged, still referencing the original object 5. Now, If we assign a new value to y:
y = "Computer"

- Python creates yet another object for "Computer" and updates y to reference it.
- The original object 5 no longer has any references and becomes eligible for garbage collection.
- Python variables hold references to objects, not the actual objects themselves.
- Reassigning a variable does not affect other variables referencing the same object unless explicitly updated.
Understanding Variable Reassignment
In this example, we check whether modifying one variable affects another when both initially reference the same object.
x = 1
y = x
y = y + 1
print(x)
print(y)
Output
1 2
Explanation:
- Initially, both x and y reference the same object 1
- When y = y + 1 is executed, python creates a new object 2
- y now references this new object and x still references the original object 1
- So, changing y does NOT affect x
Type and Casting a Variable
- Type of a variable can be determine using type() function which returns the type of the object passed to it.
- Type casting means converting the value of one data type into another. Python provides several built-in functions to facilitate casting, including int(), float() and str() among others.
- For example, int() converts compatible values to an integer, float() to a floating point number and str() to a string.
Deleting a Variable
We can remove a variable from the namespace using the del keyword. This deletes the variable and frees up the memory it was using.
x = 10
del x
print(x)
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 3, in <module>
NameError: name 'x' is not defined
Explanation:
- del x removes the variable x from memory.
- After deletion, trying to access the variable x results in a NameError indicating that the variable no longer exists.
Practical Examples
1. Swapping Two Variables: Using multiple assignments, we can swap the values of two variables without needing a temporary variable.
a, b = 5, 10
a, b = b, a
print(a, b)
Output
10 5
2. Counting Characters in a String: Assign the results of multiple operations on a string to variables in one line.
word = "Python"
length = len(word)
print("Length of the word:", length)
Output
Length of the word: 6
Recommended Problems : Type Conversion, TypeCast And Double It, Swap, Sum of N Numbers, Int Str