1,234
edits
Changes
→PART 3 - Iterating Over Lists
new_list_of_numbers = square_list(list_of_numbers)
print(list_of_numbers)print(new_list_of_numbers)</source>The above is just one example of a quick use of for loops mixed with lists. But be careful when passing lists into functions. When you give a function a list as an argument, it is the actual list reference and NOT a copy. This means a function can completely change the list without making a new list. While you do have to be careful this is can also be useful, a function can modify any given list, without have to return or store it.<br><br>
:#To demonstrate, run the following code:<source lang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
numbers.remove(5)
delete_numbers(list_of_numbers)
print(list_of_numbers)
</source>
def add_item_to_list(ordered_list):
# Appends new item to end of list which is with the value (last item + 1)
def remove_items_from_list(ordered_list, items_to_remove):
# Removes all values, found in items_to_remove list, from my_list
# Main code
if __name__ == '__main__':
print(my_list)
:::*The missing list should have the values: '''1, 2, 3, 4, 5'''
:::*The script program should have a function called '''add_item_to_list(ordered_list)'''<dd><dl>This function takes a single argument which is a list name itself. It will then look at the value of the last existing item in the list, it will then append a new value that is one unit bigger (i.e. '''+1''' and modifying that same list without returning any value).</dl></dd>:::*The script should have a function called '''remove_items_from_list(ordered_list, items_to_remove)'''<dd><dl>This function takes two arguments: a list, and a list of numbers to remove from the list. This function will then check if those items exist within that list, and if they exist, then they will be removed. This function will modify the list without returning any value.</dl></dd>
:::'''Sample Run 1:'''<source>
python3 ./CheckLab3.py -f -v lab3f
</source>
:::3. Before proceeding, make certain that you identify any and all errors in lab3f.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.
= LAB 3 SIGN OFF (SHOW INSTRUCTOR) =