Changes

Jump to: navigation, search

OPS435 Python3 Lab 3

42 bytes added, 22:00, 19 September 2019
PART 3 - Iterating Over Lists
== PART 3 - Iterating Over Lists ==
:This last section following demonstrates an extremely useful for listsway of processing a list: the ability to quickly '''loop through every value in the list'''. The '''For loops''' have a set number of times they loop. The '''for''' loop will execute all indented code for each item (element) in the list.
:'''Perform the Following Steps'''
::The following '''for''' loop will store pick one item from the value of each element from list '''list_of_numbers within ''' and assign a variable named name to it called '''item''' and run the code indented below the loop for each item.<br><br>
:#Run this from a temporary Python file:<source lang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
print(item)
</source>
:#As you can see: instead of writing eight function calls for each element of '''item''' in the list, we can call the function in a loop. And we won't have to rewrite the code if the length even number of items in of the list changes.<br><br>
:#Run the following code:<source lang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
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 change the list without making a new list. While you do have to be careful, this can also be useful. A function can modify any given list ''without'' have having to return it.<br><br>
:#To demonstrate, run the following code:<source lang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
def remove_items_from_list(ordered_list, items_to_remove):
# Removes all values, found in items_to_remove list, from my_listordered_list
# Main code
'''Additional Requirements'''
:::*The missing list should have the values: '''1, 2, 3, 4, 5'''
:::*The 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>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3f
</source>
1,760
edits

Navigation menu