Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

91 bytes removed, 19:07, 14 August 2017
PART 3 - Iterating Over Lists
== PART 3 - Iterating Over Lists ==
:This last section demonstrates an extremely useful for lists: the ability to quickly '''loop through every value in the list'''. '''For loops''' have a set number of times they loop. The '''for''' loop (a "determinant" loop) will execute all indented code for each item (element) in the list. Using loops with list allow for efficient processing of stored data.
:'''Perform the Following Steps'''
::Let's take a moment to understand how the '''for''' loop works. The This '''for ''' loop will store data the value of each list element within a temporary variable (in the demonstration below, the variable called: named '''item''') and run code that are indented below the loop for loop. This operation will repeat until ALL contained items within the list have been usedeach item.<br><br>:#Issue the following in the ipython shell:<sourcelang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
for item in list_of_numbers:
print(item)
</source>
:#Instead As you can see: instead of writing eight function calls for each element of re-calling functions over and overthe list, we can use them call the function in a loop. When combined with using lists, this is a very efficient method And we won't have to rewrite code if the length of processing large volumes of datathe list changes.<br><br>:#Issue Run the followingcode:<sourcelang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
 
def square(num):
return num * num
print(square(value))
</source>You should see from the previous demonstration the advantage of using lists with functions. Although useful, the demonstration The code above only prints the squares as opposed to saving and does not save them for future use. The next demonstration example uses a function that loops through listslist, squares the values, but and also saves the squares in a new list.<br><br>:#Issue Run the followingcode:<sourcelang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
new_list_of_numbers = square_list(list_of_numbers)
list_of_numbers
new_list_of_numbers
</source>The above is just one example of a quick, powerful, 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 also useful, a function can modify any given list, without have to return or store it.<br><br>:#To demonstrate, issue run the followingcode:<sourcelang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
def delete_numbers(numbers):
:'''Perform the Following Instructions:'''
:#Create the '''~/ops435/lab3/lab3f.py''' script. The purpose of this script is to use functions to modify items inside a list. <sourcelang="python">
#!/usr/bin/env python3
def add_item_to_list(my_listordered_list):
# Appends new item to end of list which is the (last item + 1)
def remove_items_from_list(my_listordered_list, items_to_remove):
# Removes all values, found in items_to_remove list, from my_list
remove_items_from_list(my_list, [1,5,6])
print(my_list)
 
 
</source>
'''Additional Requirements'''
:::*The missing list '''my_list''' should have the values: '''1, 2, 3, 4, 5''':::*The script should have a function called '''add_item_to_list(my_listordered_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(my_listordered_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>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3f
</source>