Changes

Jump to: navigation, search

OPS435 Python Lab 3

1,139 bytes removed, 10:35, 29 January 2018
INVESTIGATION 3: USING LISTS
= INVESTIGATION 3: USING LISTS =
:'''Lists''' are one of the most powerful '''data-types''' in Python. A list is a series of '''comma separated values found between square brackets'''. Values in a list can be anything: '''strings''', '''integers''', '''objects''', even '''other lists'''. In this section, you will introduce lists and how to use them effectively, you will further user lists in later labs. It is important to realise that although lists may appear very similar to arraysin other languages, they are different in a number of aspects including which functions are used to manipulate lists as opposed to which functions are used to manipulate arraysthe fact that they don't have a fixed size.
== PART 1 - Navigating Items in Lists ==
:'''Perform the Following Steps'''
:#Start the ipython3 shellCreate a new Python file for testing things in this section.:<source>ipython3</source>You will now create #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:<source lang="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 access individual '''elements''' from in 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 Inspect specified elements in the ipython shell to obtain stored datayour lists:<source lang="python">print(list1[0] ) # First element in list1print(list2[1] ) # Second element in list2print(list3[-1] ) # Last element in list3
</source>
:#Issue the following to You can also retrieve ranges (slices) of items from a list(these are called slices): <source lang="python">print(list1[0:5] ) # Starting with index 0 and stopping before index 5print(list2[2:4] ) # Starting with index 2 and stopping before index 4print(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:<source lang="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[0]list4[1]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.<source lang="python">list4[0][0] # First element in first listlist4[0][-1] # Last element in first listlist4[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:<source lang="python">first_only_list = [ list1[0], list2[0], list3[0] ]first_only_list
</source>

Navigation menu