Let's break down the code step by step:
def clear_list(lst): lst.clear()
-
A function named clear_list is defined.
-
It takes a parameter lst, which is expected to be a list.
-
Inside the function, lst.clear() is called.
-
The .clear() method empties the original list in place — it removes all elements from the list but does not create a new list.
-
values = [10, 20, 30]
-
A list named values is created with three integers: [10, 20, 30].
clear_list(values)
-
The clear_list function is called with values as the argument.
-
Inside the function, the list is modified in place, so values becomes [] (an empty list).
print(values)
-
Since the original list values was cleared inside the function, this prints:
[]
✅ Final Output:
[]
๐ก Key Concept:
-
Methods like .clear(), .append(), .pop(), etc., modify the list in place.
-
Because lists are mutable objects in Python, passing a list into a function allows the function to modify the original list, unless the list is reassigned.


0 Comments:
Post a Comment