Skip to content

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:

diagram variables are names bound to objects mermaid
How a variable name references a value in memory

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:

variable.py
# Declaring variables
a = 10 # an integer
b = 20.5 # a float
c = "Hello, World!" # a string
variable.py
# Declaring variables
a = 10 # an integer
b = 20.5 # a float
c = "Hello, World!" # a string

Getting the Data Type of a Variable

To get the data type of a variable, you can use the type()type() function.

Example:

variable.py
# 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))
variable.py
# 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:

command
C:\Users\Your Name> python variable.py
<class 'int'>
<class 'float'>
<class 'str'>
command
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:

variable.py
# Single assignment
a = b = c = 10
print(a, b, c)
 
# Multiple assignments
a, b, c = 10, 20.5, "Hello, World!"
print(a, b, c)
variable.py
# 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:

command
C:\Users\Your Name> python variable.py
10 10 10
10 20.5 Hello, World!
command
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:

variable.py
# Single quotes
a = 'Hello, World!'
print(a)
 
# Double quotes
b = "Hello, World!"
print(b)
variable.py
# Single quotes
a = 'Hello, World!'
print(a)
 
# Double quotes
b = "Hello, World!"
print(b)

Output:

command
C:\Users\Your Name> python variable.py
Hello, World!
Hello, World!
command
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:

variable.py
# Case-sensitive variables
a = 10
A = 20.5
print(a)
print(A)
variable.py
# Case-sensitive variables
a = 10
A = 20.5
print(a)
print(A)

Output:

command
C:\Users\Your Name> python variable.py
10
20.5
command
C:\Users\Your Name> python variable.py
10
20.5

Reassigning 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.

variable.py
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]
variable.py
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.

del.py
score = 100
del score
# print(score)   # NameError: name 'score' is not defined
del.py
score = 100
del score
# print(score)   # NameError: name 'score' is not defined

Aliasing: 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.

alias.py
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!
alias.py
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 coffee

Was this page helpful?

Let us know how we did