Variable Assignment
What is a Variable?
A variable is a name that refers to a value stored in memory. In Python, a variable is best understood as a label attached to an object, not a box that holds the value. When you write x = 10x = 10, Python creates the integer object 1010 and binds the name xx to it. This “name binding” model explains a lot of Python’s behaviour.
Diagram:
graph LR
A[Name: x] -->|refers to| B["Object: 10 (int)"]
C[Name: y] -->|refers to| B
Therefore, by assigning different data types to Python variables, you can store integers, decimals, strings, or any other object — and the name can be rebound to a different object at any time.
Declaring Variables in Python
In Python, variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
Example:
# Declaring variables
a = 10 # an integer
b = 20.5 # a float
c = "Hello, World!" # a string# Declaring variables
a = 10 # an integer
b = 20.5 # a float
c = "Hello, World!" # a stringGetting the Data Type of a Variable
To get the data type of a variable, you can use the type()type() function.
Example:
# Declaring variables
a = 10 # an integer
b = 20.5 # a float
c = "Hello, World!" # a string
# Getting the data type of a variable
print(type(a))
print(type(b))
print(type(c))# Declaring variables
a = 10 # an integer
b = 20.5 # a float
c = "Hello, World!" # a string
# Getting the data type of a variable
print(type(a))
print(type(b))
print(type(c))Output:
C:\Users\Your Name> python variable.py
<class 'int'>
<class 'float'>
<class 'str'>C:\Users\Your Name> python variable.py
<class 'int'>
<class 'float'>
<class 'str'>Single and Multiple Assignments
Python allows you to assign a single value to several variables simultaneously.
Example:
# Single assignment
a = b = c = 10
print(a, b, c)
# Multiple assignments
a, b, c = 10, 20.5, "Hello, World!"
print(a, b, c)# Single assignment
a = b = c = 10
print(a, b, c)
# Multiple assignments
a, b, c = 10, 20.5, "Hello, World!"
print(a, b, c)Output:
C:\Users\Your Name> python variable.py
10 10 10
10 20.5 Hello, World!C:\Users\Your Name> python variable.py
10 10 10
10 20.5 Hello, World!Single Quotes vs Double Quotes
In Python, you can use either single quotes ('') or double quotes ("") to declare a string. Both are valid, but you must use the same type of quotes to start and end a string.
Example:
# Single quotes
a = 'Hello, World!'
print(a)
# Double quotes
b = "Hello, World!"
print(b)# Single quotes
a = 'Hello, World!'
print(a)
# Double quotes
b = "Hello, World!"
print(b)Output:
C:\Users\Your Name> python variable.py
Hello, World!
Hello, World!C:\Users\Your Name> python variable.py
Hello, World!
Hello, World!Case Sensitivity
Python is case-sensitive, which means that aa and AA are different variables. For example:
Example:
# Case-sensitive variables
a = 10
A = 20.5
print(a)
print(A)# Case-sensitive variables
a = 10
A = 20.5
print(a)
print(A)Output:
C:\Users\Your Name> python variable.py
10
20.5C:\Users\Your Name> python variable.py
10
20.5Reassigning and Dynamic Typing
A variable can be rebound to a value of any type at any time — Python does not lock a name to one type.
x = 10 # x refers to an int
x = "ten" # now x refers to a str — perfectly legal
x = [1, 2, 3] # now a list
print(x) # [1, 2, 3]x = 10 # x refers to an int
x = "ten" # now x refers to a str — perfectly legal
x = [1, 2, 3] # now a list
print(x) # [1, 2, 3]Deleting a Variable
The deldel statement removes a name binding. Using the name afterwards raises a NameErrorNameError.
score = 100
del score
# print(score) # NameError: name 'score' is not definedscore = 100
del score
# print(score) # NameError: name 'score' is not definedAliasing: Two Names, One Object
Because a variable is just a name, assigning one variable to another makes both names point to the same object. For mutable objects (like lists) this matters — changing it through one name is visible through the other.
a = [1, 2, 3]
b = a # b and a refer to the SAME list
b.append(4)
print(a) # [1, 2, 3, 4] -> changed through b!a = [1, 2, 3]
b = a # b and a refer to the SAME list
b.append(4)
print(a) # [1, 2, 3, 4] -> changed through b!Try it: Variable Assignment Exercises
Exercise 1 – Assign and Print a Variable
Exercise 2 – Reassign a Variable
Exercise 3 – Swap Two Variables
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
