Skip to content

Variable Assignment

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 = 10, Python creates the integer object 10 and binds the name x 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.

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.

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

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

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'>

Python allows you to assign a single value to several variables simultaneously.

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!

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.

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!

Python is case-sensitive, which means that a and A are different variables. For example:

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

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]

The del statement removes a name binding. Using the name afterwards raises a NameError.

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

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!

Exercise 1 – Assign and Print a Variable

Section titled “Exercise 1 – Assign and Print a Variable”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading