Changes

Jump to: navigation, search

OPS435 Python Lab 3

165 bytes removed, 12:29, 14 August 2017
PART 1 - Navigating Items in Lists
ipython3
</source>You will now create a few lists with different values: list1 contains only '''integers''', list2 contains only '''strings''', list3 contains a combination of both '''integers and strings'''.<br><br>
:#Issue the following from the ipython shell:<sourcelang="python">
list1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
list2 = [ 'uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635' ]
list3 = [ 'uli101', 1, 'ops235', 2, 'ops335', 3, 'ops435', 4, 'ops535', 5, 'ops635', 6 ]
</source>List are constructed similar to arrays. The best way to get individual '''elements''' from a list is using the list '''index'''.<br>The index is a number starting from 0 to ('''number_of_items - 1'''), the list index starts counting at '''0'''.<br><br>
:#Issue the following in the ipython shell to obtain stored data:<sourcelang="python">
list1[0] # First element in list1
list2[1] # Second element in list2
list3[-1] # Last element in list3
</source>
:#Issue the following to provide retrieve ranges (slices) of items in from a list: <sourcelang="python">
list1[0:5] # Starting with index 0 and stopping before index 5
list2[2:4] # Starting with index 2 and stopping before index 4
list3[3:] # Starting with index 3 and going to the end
</source>Lists can also contain other lists. This means data can be contained in: lists of strings, lists of integers, or lists contains a combination of strings and integers.<br><br>
:#Issue of the following to create a list that contains lists:<sourcelang="python">
list4 = [ [1, 2, 3, 4], ['a', 'b', 'c', 'd'], [ 5, 6, 'e', 'f' ] ]
</source>The list just created only has 3 index locations. Each index points the individual list stored as the list element. <source>
list4[2]
</source>
:#To access a list inside another list, a second index is needed. Spend some time trying out the syntax and try and navigate to a specific spot in the list.<sourcelang="python">
list4[0][0] # First element in first list
list4[0][-1] # Last element in first list
list4[2][0:2] # First two elements in third list
</source>
:#You can use different elements from existing lists to create new lists. To demonstrate, issue the following:<sourcelang="python">
first_only_list = [ list1[0], list2[0], list3[0] ]
first_only_list
:#Create a Python script called: '''~/ops435/lab3/lab3e.py'''<br>The purpose of this script is to have a number of functions that output a different data storage in various elements of a list. Each function will return either a single item from the list OR will create a new list and return the entire new list.<br><br>
:#The template function names and the special if statement:<sourcelang="python">
#!/usr/bin/env python3
:::'''Additional Requirements'''
:::*The script should have declare a list called '''my_list''' created BEFORE any function definition:::*The list called '''my_list''' should have the values: '''100''', '''200''', '''300''', and ''''six hundred'''':::*The script should have a function called '''give_list()implement''' which '''returns''' a list:::*The script should the empty functions - i.e. you have a function called '''give_first_item()''' which '''returns''' a string:::*The script should have a function called '''give_first_and_last_item()''' which '''returns''' a list:::*The script should have a function called '''give_second_and_third_item()''' which '''returns''' a listto fill in the bodies for these functions
:::'''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/CheckLab3.pypython3 .master/LabCheckScripts/CheckLab3.py -f -v lab3e
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab3e.py. When the checking script tells you everything is OK before proceeding to the next step.

Navigation menu