Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

310 bytes added, 03:45, 30 May 2017
PART 2 - Manipulating Items in Lists
20
</source>
:::4. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3b
</source>
:::5. Before proceeding, make certain that you identify any and all errors in lab3b.py. When the check script tells you everything is ok before proceeding to the next step.
'''Multiple Arguments and IF Statements'''
Error: function operator can be "add", "subtract", or "multiply"
</source>
:::5. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3c
</source>
:::6. Before proceeding, make certain that you identify any and all errors in lab3c.py. When the check script tells you everything is ok before proceeding to the next step.
== PART 3 - Running System Commands with Subprocess ==
:'''Perform the Following stepsSteps'''
:#Start the ipython3 shell<source>
ipython3
</source>
 :#Create a few lists with different values: list1 contains only integers, list2 contains only strings, list3 contains a combination of both.<source>
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 get individual items from a list is using the list index. The index is a number starting from 0 to (number_of_items - 1), the index starts counting at 0.<source>
list1[0] # First item in list1
list2[1] # Second item in list2
list3[-1] # Last item in list3
</source>
 :#Instead of just getting the first and last, lists can give ranges of items. <source>
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 lists can contain: lists and strings and integers all together. <source>
list4 = [ [1, 2, 3, 4], ['a', 'b', 'c', 'd'], [ 5, 6, 'e', 'f' ] ]
</source>
 :#This list still only has 3 index locations. Each index points to another list. <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>
list4[0][0] # First item in first list
list4[0][-1] # Last item in first list
list4[2][0:2] # First two items in third list
</source>
 :#Using different items from different lists to create new lists.<source>
first_only_list = [ list1[0], list2[0], list3[0] ]
first_only_list
</source>
'''lab3ePractice Using Functions and Using the List Index''':'''Perform the Following Instructions''':#Create the '''~/ops435/lab3/lab3e.py''' script. The purpose of this script is to have a number of functions that output a different part of the list. Each function will return either a single item from the list OR will create a new list and return the entire new list.  :#The template function names and boiler plate if statement:<source>
!/usr/bin/env python3
print(give_first_and_last_item())
print(give_second_and_third_item())
 
</source>
 :::*The script should have a '''Shebang line''':::*The script should have a list called my_list:::*The list my_list should have the values: 100, 200, 300, 'six hundred':::*The script should have a function called give_list() which returns a list:::*The script should 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 list  :::3. Sample Run 1:<source>
run lab3e.py
[100, 200, 300, 'six hundred']
[200, 300]
</source>
 :::4. Sample Import 1:<source>
import lab3e
</source>
  :::5. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3e
</source>
:::6. Before proceeding, make certain that you identify any and all errors in lab3e.py. When the check script tells you everything is ok before proceeding to the next step. 
== PART 2 - Manipulating Items in Lists ==
There are a number of ways to get information about lists, as well as change what is inside a list. This section will cover the different ways to manipulate lists.
:'''Perform the Following Steps:''':#First start with the smallest change. Change a single item at a single point in a list.<source>
courses = [ 'uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635' ]
courses[0]
courses
</source>
 :#Now lets use the dir() and help() functions to see what functions and attributes lists have. The help() function will also give us tips on how to use some functions.<source>
dir(courses)
help(courses)
</source>
  :#Next search and find more information on a number list functions for changing lists.<source> 
help(courses.append)
courses.append('ops235') # Add a new item to the end of the list
</source>
 :#Using Python functions we can get more information out of lists.<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
len(list_of_numbers) # Returns the length of the list
max(list_of_numbers) # Returns the largest value in the list
</source>
 :#Now on to some of the more powerful features of Python lists. Searching for values inside lists and finding locations of values in a list. The index() function allows searching inside a list for a value, it will return the index number of the first occurence. <source>
number = 10
help(list_of_numbers.index)
list_of_numbers.index(number) # Return index of the number searched for
</source>
 :#The problem that comes up here is if the item searched for doesn't exist, Python will throw a error. Lets make sure it exists before asking for the index location. To find out if a value is in a list, just ask using a if statement, if the statement is True, then the value is found in the list.<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
number = 10
== PART 3 - Iterating Over Lists ==
This final section explains the best part about 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''' will one by one run all indented code for each item in the list.  :'''Perform the Following Steps''' :#The '''for loop''' will create a new variable that contains the value from the list of the current iteration.<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
for item in list_of_numbers:
print(item)
</source>
 :#Now instead of running functions over and over, from our previous sections, we can put them in a loop. The next sequence of code will apply a function to every item in the list.<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
def square(num):
</source>
 :#But this only prints out each new value. Lets try making a new function that loops through lists, squares the values, and returns a new list.<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
new_list_of_numbers
</source>
:#The above is just one example of quick, powerful, for loops mixed with lists. But be careful when passing lists into functions. When you give a function a list, 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.<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
def delete_numbers(numbers):
'''lab3fPractice Functions, Lists, Loops''':'''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.  <source>
# Place my_list here
</source>
:::*The script should have a '''Shebang line''':::*The list '''my_list''' should have the values: '''1, 2, 3, 4, 5''':::*The script should have a function called add_item_to_list(my_list) :::*The script should have a function called remove_items_from_list(my_list, items_to_remove) :::*The function add_item_to_list(my_list) takes a single argument which is a list. This function will look at the value of the last item in the list, it will then append a new value that is +1 bigger then the previous number. This function modifies the list without returning any value:::*The function remove_items_from_list(my_list, list_of_numbers_to_remove) takes two arguments, a list, and a list of numbers to remove from the list. This function will then check if those items are in the list, if they are it will remove them. This function modifies the list without returning any value. :::2. Sample Run 1:<source>
run lab3f.py
[1, 2, 3, 4, 5]
[2, 3, 4, 7, 8]
</source>
 :::3. Sample Import 1:<source>
from lab3f import * [1/1899]
my_list
[2, 3, 4, 7, 8]
 
</source>
 :::4. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3f
</source>
:::5. Before proceeding, make certain that you identify any and all errors in lab3f.py. When the check script tells you everything is ok before proceeding to the next step. 
= LAB 3 SIGN OFF (SHOW INSTRUCTOR) =
198
edits