Variable Assignment
What is a Variable?
Section titled “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 = 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:
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
Section titled “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:
Section titled “Example:”# Declaring variables
a = 10 # an integer
b = 20.5 # a float
c = "Hello, World!" # a stringGetting the Data Type of a Variable
Section titled “Getting the Data Type of a Variable”To get the data type of a variable, you can use the type() function.
Example:
Section titled “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))Output:
C:\Users\Your Name> python variable.py
<class 'int'>
<class 'float'>
<class 'str'>Single and Multiple Assignments
Section titled “Single and Multiple Assignments”Python allows you to assign a single value to several variables simultaneously.
Example:
Section titled “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)Output:
C:\Users\Your Name> python variable.py
10 10 10
10 20.5 Hello, World!Single Quotes vs Double Quotes
Section titled “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:
Section titled “Example:”# 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!Case Sensitivity
Section titled “Case Sensitivity”Python is case-sensitive, which means that a and A are different variables. For example:
Example:
Section titled “Example:”# Case-sensitive variables
a = 10
A = 20.5
print(a)
print(A)Output:
C:\Users\Your Name> python variable.py
10
20.5Reassigning and Dynamic Typing
Section titled “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.
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
Section titled “Deleting a Variable”The del statement removes a name binding. Using the name afterwards raises a NameError.
score = 100
del score
# print(score) # NameError: name 'score' is not definedAliasing: Two Names, One Object
Section titled “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.
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
Section titled “Try it: Variable Assignment Exercises”Exercise 1 – Assign and Print a Variable
Section titled “Exercise 1 – Assign and Print a Variable”Exercise 2 – Reassign a Variable
Section titled “Exercise 2 – Reassign a Variable”Exercise 3 – Swap Two Variables
Section titled “Exercise 3 – Swap Two Variables”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading