Composite Part 2 Flashcards
When passing a list as a parameter to a function in Python, it is important to understand that the parameter ——-rather than ———. This means that any modifications made to the list within the function will ——-
receives a reference to the list
a new copy of the list
affect the original list outside the function.
Reassigning references vs modifying them
However, it’s important to note that reassigning a new value to the reference itself (the parameter) will not affect the original list outside the function. For example, if you assign a new list to the parameter, it will only create a local variable inside the function, and the original list remains unchanged. Therefore, when working with list parameters in Python, modifications made to the list itself (such as appending elements, modifying values) will affect the original list, while reassigning a new list to the parameter will not affect the original list.
How many list and references in this function:
def fun1(): aListCopy = [1, 2, 3] aListCopy = [2, 3 ,4] print (aListCopy) fun1()
In the given fun1() function, there are two lists and one reference.
The first list [1, 2, 3] is created and assigned to the variable aListCopy.
The second list [2, 3, 4] is created and assigned to the same variable aListCopy. This reassignment causes the previous list [1, 2, 3] to be discarded.
Finally, the value of aListCopy (which is the second list [2, 3, 4]) is printed.
Therefore, there are two lists created in the function, but the reference aListCopy refers to the second list [2, 3, 4]. The first list [1, 2, 3] is no longer accessible after the reassignment.
Passing variables not lists you are….Changes made to the paraeters are only made …..
making a copy of the value stored in the variable and passing it into the function. Changes made to the paraeters are only made to local variables so you mucg return value back to the caller inorder to be retained.
when you return lists?
So, whether you need to return and pass lists as parameters depends on the specific requirements and design of your code. If you want to modify the original list, it’s not necessary to return and pass it explicitly. But if you want to create a modified version of the list without modifying the original, returning and passing the list can be a good practice.
ex:
~~~
def double_list_elements(my_list):
doubled_lst = [2 * element for element in my_list]
return doubled_lst
my_list = [1, 2, 3, 4, 5]
~~~
Which is faster? Passing by value or passing by reference with a list?
pass by reference
Common way to avoid overflowing a list (2)
-Use a constant in conjuction with the list
- use the length function len toget the length of list
Lens function
The len() function in Python is a built-in function that returns the length of an object. The object can be a string, list, tuple, dictionary, or any other iterable or container data type.
Here’s the general syntax of the len() function:
my_list = [1, 2, 3, 4, 5] length = len(my_list) print(length) # Output: 5
Pass by value vs pass by reference difference
In “pass by value,” a copy of the argument’s value is passed to the function. Any changes made to the parameter inside the function do not affect the original value of the argument outside the function. The function works with its own local copy of the value.
In “pass by reference,” a reference to the argument is passed to the function. Instead of a copy of the value, the function receives a reference to the original object. Any modifications made to the parameter inside the function will directly affect the original object outside the function.
Immutable objects (like numbers(floast, integers), strings, tuples) are passed by
value
Any modifications made to the parameter inside the function do not affect the original object outside the function.
Mutable objects (like lists, dictionaries)
Any modifications made to the parameter inside the function will affect the original object outside the function.
whats the output:
~~~
my_string = “Hello, World!”
length = len(my_string)
print(length)
~~~
13
how to use lens to find rows and columns:
my_2d_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
num_rows = len(my_2d_list) print("Number of rows:", num_rows) # Output: 3 num_columns = len(my_2d_list[0]) print("Number of columns:", num_columns) # Output: 3
2-d List is a list…..
containing rows of 1D lists
For 2-D lists, lists elements are specified in the order of
[row] [column]