6.1 Variables are References
In Python, variables are references to objects, not the variable itself. This means that when you create a variable in Python, you're just creating a reference to an object in memory.
This feature of the language has important implications for working with variables, especially when passing objects to functions or assigning them to other variables.
When you create a variable and assign it a value, Python allocates an object in memory for that value and makes the variable a reference to that object. If you assign one variable to another, the new variable will refer to the same object as the original variable. This means that changes made through one variable will reflect on another if the object is mutable.
Example:
a = [1, 2, 3]
b = a
b.append(4)
print(a) # Outputs: [1, 2, 3, 4]
We added the number 4
to the variable b
, but it also
got added to the list in the variable a
because both variables are just
references to the list object [1, 2, 3].
6.2 Immutable Objects
In Python, objects are categorized into mutable and immutable. Mutable objects, like lists and dictionaries, can be changed after creation.
Immutable objects, like strings and numbers, cannot be changed after creation. When you work with immutable objects, changes lead to the creation of a new object:
Assignment never changes the object:
a = 5 # a holds a reference to the object 5
b = a # b also holds a reference to object 5
b = 3 # now b holds a reference to object 3
print(a) # Outputs: 5
An object can be changed by calling its function:
a = [1, 2, 3]
b = a
b.append(4)
print(a) # Outputs: [1, 2, 3, 4]
But there are immutable objects that have functions. These functions don't change the original object but return a new one containing the new result:
name = "Alex"
print(name) # Outputs: Alex
name2 = name.lower()
print(name2) # Outputs: alex
Strings are immutable objects. And even though they have methods, these methods never change the string object — instead, they always return a new string (a new object). The old object remains unchanged.
6.3 Working with None Type
In Python, there's a special data type representing the absence of a value —
None
. It's used to denote empty variables or as a return value
for functions that don't return anything. Understanding None is important
for managing program behavior, especially in the context of conditional
statements and data handling.
Basics of Using None
None
is the sole instance of the NoneType
class.
It's used to indicate that a variable has no value.
In conditions, None
evaluates as false (False
).
a = None
if a:
print("a is True")
else:
print("a is None or False")
Comparing with None
Checking a variable against None should be done using the
is operator
, not ==. The is
operator checks for
identity, not value equality.
a = None
if a is None:
print("a is None")
else:
print("a has value")
None in Functions
None
is often used in functions that don't need to explicitly
return a value. If a function doesn't return a value, it implicitly returns
None
.
def func():
print("This function returns None")
result = func()
print(result) # Outputs: None
None and Lists
None
can be used in lists, tuples, or other collections to
indicate the absence of an element at a specific position.
my_list = [1, None, 3]
print(my_list) # Outputs: [1, None, 3]
GO TO FULL VERSION