Changes

Jump to: navigation, search

OPS435 Python3 Lab 3

533 bytes added, 20:12, 19 September 2019
INVESTIGATION 3: USING LISTS
= INVESTIGATION 3: USING LISTS =
:'''ListsList''' are is one of the most powerful important '''data-types''' in Python. A list is a series of '''comma separated values items found between two square brackets'''. Values Items in a list can be anythingany python objects: '''strings''', '''integers''', '''objects''', and even '''other lists'''. In this section, you will introduce study lists and how to use them effectivelyproperly, and this will set the foundation for you will further user to use lists effectively in later labsand assignments. It is important to realise that although lists may appear very similar to arrays in other languages, they are different in a number of aspects including the fact that they don't have a fixed size.
== PART 1 - Navigating Items in Lists ==
:'''Perform the Following Steps'''
:#Create a new Python file for testing things new concepts in this section.:#Create a few lists with different valuestype of items: list1 contains only '''integersinteger'''objects, list2 contains only '''stringsstring'''objects, list3 contains a combination of both '''integers integer and stringsstring'''objects
:#<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>The best way to access individual '''elementsitem''' 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>:#Inspect specified elements items in your lists:<source lang="python">print(list1[0]) # First element item in list1print(list2[1]) # Second element item in list2print(list3[-1]) # Last element item in list3
</source>
:#You can also retrieve ranges of items from a list (these are called slices): <source lang="python">
:'''Perform the Following Instructions'''
:#Create a Python script called: '''~/ops435/lab3/lab3e.py'''<br>The purpose of this script is to have a number of functions that output '''return''' a different data storage in various elements selected number of items from a given 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 Use the following as the template and fill in the proper code for each named function names and . The code after the special if statementis referred as the main block of the python script:<source lang="python">
#!/usr/bin/env python3
# Create the list called "my_list" here, not within any function defined below.
# That makes it a global variableobject. We'll talk about that in another lab.
def give_list():
# Does not accept any arguments
# Returns all of items in the global variable object my_list unchanged
def give_first_item():
# Does not accept any arguments
# Returns a single string that is the first item in the global object my_listas a string
def give_first_and_last_item():
# Does not accept any arguments
# Returns a list that includes the first and last items in the global object my_list
def give_second_and_third_item():
# Does not accept any arguments
# Returns a list that includes the second and third items in the global object my_list
if __name__ == '__main__': # This section also referred to as a "main codeblock"
print(give_list())
print(give_first_item())
:::*The script should 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 the proper code to '''implement''' the empty named functions - i.e. you have to fill in the bodies for these functions
:::'''Sample Run 1:'''<source>
[200, 300]
</source>
:::'''Sample Run 2 (with import from when imported by another script):'''<source>
import lab3e
lab3e.give_list()
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 lab3e
</source>
print(courses)
</source>
:#Below are some examples of using built-in methods (they are functions that are associated with to list object, you use the dot notation with the name of the list as the prefix when call those methods on the list object) to '''manipulate''' listslist objects. Take your time to see how each function method can be a useful tool for making changes to existing lists:<source lang="python">courses.append('ops235') # Add a new item to the end of the listobject named courses
print(courses)
courses.insert(0, 'hwd101') # Add a new item to the specified index location, # the original item will be pushed to the next index location
print(courses)
courses.remove('ops335') # Remove first occurrence of valuea matching item in the list object
print(courses)
sorted_courses = courses.copy() # Create a copy of the courses list
sorted_courses.sort() # Sort the new list
print(courses)
print(sorted_courses)
</source>
:#In addition to using functions built-in methods to manipulate lists, there are functions that are useful to provide '''information''' regarding the list such as number of elements in a list, the smallest value and largest value in a list:<source lang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
length_of_list = len(list_of_numbers) # Returns the length of the list
1,760
edits

Navigation menu