Open main menu

CDOT Wiki β

OPS435 Python Lab 3

Revision as of 02:46, 30 May 2017 by Oatley (talk | contribs) (INVESTIGATION 2 - LISTS)

LAB OBJECTIVES

This lab investigates methods for reusing code with both functions and loops.

INVESTIGATION 1: FUNCTIONS

In Python a Function is a predefined set of code, this code is loaded into python, but it does not immediately perform any actions. Functions may accept arguments or return values, but functions do no have to do either. Until a Function is specifically told to execute, it's code will sit unused.


PART 1 - Using Functions

Functions and Strings

Perform the Following Steps:
  1. To start, open ipython and try experimenting with functions.
    ipython3
  2. Whenever you want to create a function, you must start with the keyword "def". The def keyword is used to start the definition of the function, it does not run the code you write. Functions, just like if statements, must have all code under them indented.
    def hello():
        print('Hello World')
        print('Inside a Function')
    </source
    :#Now that our function is created we can use it over and over. To execute the code inside the function, run the function name with "'''()'''" '''brackets''' at the end of the function name.<source>
    hello()
    hello()
    hello()
  3. Next, create a function that returns some data after the function runs. This function does not print out any text, it creates new variables and at the end returns the value.
    def return_text_value():
        name = 'Terry'
        greeting = 'Good Morning ' + name 
        return greeting
  4. The different between returning a value and printing a value, returned values can be caught and stored for later use. Once the returned value has been stored, it can be printed, manipulated, compared in if statements, and more. Below will cover how to store a returned value.
    text = return_text_value()
  5. Now the returned text from the function has been stored in the variable "text". It can be used like any string value now.
    print(text)

Functions and Integers

Perform the Following steps:
  1. In this next example the functions will be returning integer values instead of text. There is not a big difference, but when returning number values, care needs to be taken if you try combining it with a string.
    def return_number_value():
        num1 = 10
        num2 = 5
        num3 = num1 + num2
        return num3
  2. Now try storing the return value of the above function.
    number = return_number_value()
    print(number)
    print(number + 5)
    print(return_number_value() + 10)
  3. The warning again, because this is a number value, using it with strings can cause errors.
    number = return_number_value()
    print('my number is ' + number)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-24-d80d5924146a> in <module>()
    ----> 1 print('my numbr is ' + number)
    
    TypeError: Can't convert 'int' object to str implicitly
  4. If a number needs to be combined with a string use the one of the following syntax below.
    number = return_number_value()
    print('my number is ', number)
    print('my number is ' + str(number))
    print('my number is ' + str(return_number_value()))

Practice Using Functions

Perform the Following Instructions:
  1. Create a new script ~/ops435/lab3/lab3a.py and store the following content inside:
    #!/usr/bin/env python3
    
    # Function returning string value
    def return_text_value():
        name = 'Terry'
        return 'Good Morning ' + name
    
    # Function returning integer value
    def return_number_value():
        num1 = 5
        num2 = 10
        num3 = num1 + num2
        return num3
    
    # This IF Statement may seem cryptic and unclear. Any code written under this if statement will be run when you run your script. 
    if __name__ == '__main__':
        text = return_text_value()
        print(text)
        number = return_number_value()
        print(str(number))
  2. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.
    cd ~/ops435/lab3/
    pwd #confirm that you are in the right directory
    ls CheckLab3.py || wget matrix.senecac.on.ca/~acoatley-willis/CheckLab3.py
    python3 ./CheckLab3.py -f -v lab3a
  3. Before proceeding, make certain that you identify any and all errors in lab3a.py. When the check script tells you everything is ok before proceeding to the next step.
  4. Start up ipython3 shell again.
    ipython3
  5. The if statement line found in lab3a.py is special if statement. It allows scripts to be imported into other python environment while only defining the functions, this powerful feature allows for python scripts to run differently depending on if you run it or import it. Take a look at the code, there is NO reason to run this in ipython3.
    if __name__ == '__main__':
        print('python code')
  6. To show examples of how the above if statement works, run the lab3a.py script. The output will show that all the code found under the if statement was run.
    run lab3a.py
  7. This next example will show when the code under the if statement does NOT run.
    import lab3a
  8. When you import a python script it will run all code found that is not indented, including defining all of the functions. But, when importing, python will not run any code under the special if statement. Now that the script has been imported, we can run functions previously written.
    text = lab3a.return_text_value()
    text
    lab3a.return_number_value()

PART 2 - Providing Functions With Arguments

Functions can take arguments that work similarly to Python scripts. In this section, the functions created will be given arguments, these functions will use the given arguments to provide different output.

Perform the Following Steps:
  1. Start the ipython3 shell
    ipython3
  2. To create a new function that accepts arguments, provide variable names in the functions brackets. But if a function is given arguments in the definition, it must always get the same number of arguments when you call the function.
    def square(number):
        return number ** 2
  3. To square a number in math your multiply using number * number or use exponents number ** 2 to the power of two. This function takes one argument number, the function will use exponents to multiply the number given by itself.
    square(5)
    square(10)
    square(12)
    square(square(2))
  4. Providing more than one argument in a function requires the use of commas. Be careful NOT to provide strings or decimals, as you may cause errors.
    def sum_numbers(number1, number2):
        return int(number1) + int(number2)
  5. Running functions with multiple arguments is the same. When you put a function as a argument of another function, the inner-most function will run first, and the return value will be used as the argument on the outer function. For example, in this case below, sum_numbers(5, 5) will return 10, and provide square with that value square( 10 ).
    sum_numbers(5, 10)
    sum_numbers(50, 100)
    square(sum_numbers(5, 5))

Practice Creating Functions With Return Values

Perform the Following Instructions:
  1. The next script is NOT COMPLETE, your job is to make the script do what it askes. It contains 3 functions, one of adding, one for subtracting, and one for multiplying. Make sure each function performs the correct operation and returns a integer value. Create a new script ~/ops435/lab3/lab3b.py with the following contents:
    #!/usr/bin/env python3
    
    def sum_numbers(number1, number2):
        # Make this function add number1 and number2 and return the value
    
    def subtract_numbers():
        # Make this function subtract number1 and number2 and return the value
        # Remember to make sure the function accepts 2 arguments
    
    def multiply_numbers():
        # Make this function multiply number1 and number2 and return the value
        # Remember to make sure the function accepts 2 arguments
        
    # Place any code you want to test inside the if statement for practice
    if __name__ == '__main__':
        print(sum_numbers(10, 5))
        print(subtract_numbers(10, 5))
        print(multiply_numbers(10, 5))
  • The script should have a Shebang line
  • The script should have a function sum_numbers(number1, number2)
  • The script should have a function subtract_numbers(number1, number2)
  • The script should have a function multiply_numbers(number1, number2)
  • All functions should accept two arguments
  • All functions should return a integer
  • The script should contain no errors
2. Sample Run 1:
run lab3b.py
15
5
50
3. Sample Import 1:
ipython3

import lab3b

sum_numbers(10, 5)
15

sum_numbers(25, 25)
50

subtract_numbers(10, 5)
5

subtract_numbers(5, 10)
-5

multiply_numbers(10, 5)
50

multiply_numbers(10, 2)
20
4. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecac.on.ca/~acoatley-willis/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3b
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

The next function we make in this section if going to be more advanced and contain logic inside it. First try the following to practice logic in functions.

Perform the Following Steps:
  1. Start the ipython3 shell
    ipython3
  2. Now try making some functions that uses if statements, BUT BEWARE, making an if statement inside a function means that you are indented two times to get to the if statement.
    def check_temperature(temp):
        if temp > 30:
            return 'hot'
        elif temp < 0:
            return 'cold'
        elif temp == 20:
            return 'perfect'
        return 'ok'
  3. Remember to note the extra indentation on the code under the if statements. The final return "ok" will only take place if a previous return has not taken place before it. Once return has been used in a function, the function immediately exits and returns the value.
    check_temperature(50)
    'hot'
    
    check_temperature(20)
    'perfect'
    
    check_temperature(-50)
    'cold'
    
    check_temperature(25)
    'ok'
    
    check_temperature(10)
    'ok'

Practice Multiple Levels of Indentation

Perform the Following Instructions:
  1. Create the ~/ops435/lab3/lab3c.py script. The purpose of the script is to make a single function that can perform addition, subtraction, or multiplication on a pair of numbers. But the function will allow us to choose exatly what operation we are performing on it when we call the function. If the operate function does NOT understand the operator given, it should return a error message.
  2. Use this template to get started:
    #!/usr/bin/env python3
    
    def operate(number1, number2, operator):
        # Place logic in this function
    
    if __name__ == '__main__':
        print(operate(10, 5, 'add'))
        print(operate(10, 5, 'subtract'))
        print(operate(10, 5, 'multiply'))
        print(operate(10, 5, 'divide'))
  • The script should have a Shebang line
  • The script should have a function operate(num1, num2, operator)
  • The script should use if statements inside the operate function
  • The operate function should accept three arguments
  • The operate function should return the answer
  • The operate function should return a error message if the operation is unknown
  • The script should contain show the exact output as the sample imports
  • The script should contain no errors
3. Sample Run 1:
run lab3c.py
15
5
50
Error: function operator can be "add", "subtract", or "multiply"
4. Sample Import 1:
import lab3c

operate(10, 20, 'add')
30

operate(2, 3, 'add')
5

operate(100, 5, 'subtract')
95

operate(10, 20, 'subtract')
-10

operate(5, 5, 'multiply')
25

operate(10, 100, 'multiply')
1000

operate(100, 5, 'divide')
Error: function operator can be "add", "subtract", or "multiply"

operate(100, 5, 'power')
Error: function operator can be "add", "subtract", or "multiply"
5. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecac.on.ca/~acoatley-willis/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3c
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

This last part of the investigation will give you access to system commands within your python scripts. While we are able to run some bash commands inside the ipython3 environment, these do not transfer over into the python code we write. It is not usually a good idea to run system commands in Python, this makes your Python code less portable and makes it require a speicifc operating system or a system that has those commands available. There is also the case of security, allowing python to execute commands on the system can be a security problem if care isn't taken. For these reason you should only use subprocess and the system commands as a last resort and stick to Python code only.

Perform the Following Steps:

  1. Start the ipython3 shell:
    import subprocess
    dir(subprocess)
  2. There are many available modules and attributes available as part of subprocess, we are interested in "Popen". This method subprocess.Popen() can be used to run system commands as a child process to the Python script. This below output will create a new child process, in Python we can control this through the new Python object we just created, "p". "p" now has a collection of methods(functions that are apart of a object) available, view them with dir().
    p = subprocess.Popen(['date'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    dir(p)
  3. This next step is going to communicate with the process and get the stdout and stderr from the command we previously.
    stdout, stderr = p.communicate()
    stdout
    # The above stdout is stored in bytes
    # Convert stdout to a string and strip off the newline characters
    stdout = stdout.decode('utf-8').strip()
    stdout
  4. While many of these system commands could be instead written in simply Python, the exercise of running system commands is important.

Practice Running System Commands From Python

Perform the Following Instructions:
  1. Create the "~/ops435/lab3/lab3d.py" script. The purpose of this script is to create a Python function that can return the linux system's root directory free space.
  • The script should have a Shebang line
  • The script should import subprocess
  • The script should use the linux command "df -h | grep '/$' | awk '{print $4}'"
  • The script should contain the function free_space()
  • The function free_space() should return a string which is in utf-8 and has newline characters stript
  • Note: your output may be completely different, the free/available disk space on every computers root directory may be different.
2. Sample Run 1:
run lab3d.py
9.6G
3. Sample Import 1:
import lab3d

lab3d.free_space()
'9.6G'
4. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecac.on.ca/~acoatley-willis/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3d
5. Before proceeding, make certain that you identify any and all errors in lab3d.py. When the check script tells you everything is ok before proceeding to the next step.

INVESTIGATION 2 - LISTS

Lists are ones 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 we will introduce lists and how to use them effectively, we will come back to lists again in later labs.


PART 1 - Navigating Items in Lists

Perform the Following Steps
  1. Start the ipython3 shell
    ipython3
  2. Create a few lists with different values: list1 contains only integers, list2 contains only strings, list3 contains a combination of both.
    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 ]
  3. 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.
    list1[0] # First item in list1
    list2[1] # Second item in list2
    list3[-1] # Last item in list3
  4. Instead of just getting the first and last, lists can give ranges of items.
    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
  5. Lists can also contain other lists. This means lists can contain: lists and strings and integers all together.
    list4 = [ [1, 2, 3, 4], ['a', 'b', 'c', 'd'], [ 5, 6, 'e', 'f' ] ]
  6. This list still only has 3 index locations. Each index points to another list.
    list4[0]
    list4[1]
    list4[2]
  7. 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.
    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
  8. Using different items from different lists to create new lists.
    first_only_list = [ list1[0], list2[0], list3[0] ]
    first_only_list

Practice Using Functions and Using the List Index

Perform the Following Instructions
  1. 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.
  2. The template function names and boiler plate if statement:
    !/usr/bin/env python3
    
    # Put the list here
    
    
    def give_list():
        # Returns the entire list unchanged
    
    def give_first_item():
        # Returns a single string that is the first item in the list
    
    def give_first_and_last_item():
        # Returns the a list that includes the first and last items in the list
    
    def give_second_and_third_item():
        # Returns the a list that includes the second and third items in the list
    
    if __name__ == '__main__':
        print(give_list())
        print(give_first_item())
        print(give_first_and_last_item())
        print(give_second_and_third_item())
  • 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:
run  lab3e.py
[100, 200, 300, 'six hundred']
100
[100, 'six hundred']
[200, 300]
4. Sample Import 1:
import lab3e

lab3e.give_list()
[100, 200, 300, 'six hundred']

lab3e.give_first_item()
100

lab3e.give_first_and_last_item()
[100, 'six hundred']

lab3e.give_second_and_third_item()
[200, 300]
5. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecac.on.ca/~acoatley-willis/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3e
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:
  1. First start with the smallest change. Change a single item at a single point in a list.
    courses = [ 'uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635' ]
    courses[0]
    courses[0] = 'eac150'
    courses[0]
    courses
  2. 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.
    dir(courses)
    help(courses)
  3. Next search and find more information on a number list functions for changing lists.
    help(courses.append)
    courses.append('ops235')    # Add a new item to the end of the list
    courses
    
    help(courses.insert) 
    courses.insert(0, 'hwd101') # Add a new item to the specified index location
    courses
    
    help(courses.remove)
    courses.remove('ops335')    # Remove first occurrence of value
    courses
    
    help(courses.sort)
    sorted_list = courses.copy() # Create a copy of the courses list
    sorted_list.sort()           # Sort the new list
    courses
    sorted_courses
  4. Using Python functions we can get more information out of lists.
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    len(list_of_numbers)    # Returns the length of the list
    min(list_of_numbers)    # Returns the smallest value in the list
    max(list_of_numbers)    # Returns the largest value in the list

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.

number = 10
help(list_of_numbers.index)
list_of_numbers.index(number)           # Return index of the number searched for
  1. 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.
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    number = 10
    if number in list_of_numbers:                   # Returns True if value in list, returns False if item not in list
        number_index = list_of_numbers.index(number)
    else:                                           # If the statement is False, the else will run
        print(str(number) + ' is not in list_of_numbers'

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
  1. The for loop will create a new variable that contains the value from the list of the current iteration.
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    for item in list_of_numbers:
        print(item)
  2. 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.
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    def square(num):
        return num * num
    
    for value in list_of_numbers:
        square(value)
  3. 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.
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    
    # Squares each item in a list of numbers, returns new list with squared numbers
    def square_list(number_list):
        new_list = []
        for number in number_list:
            new_list.append(number * number)
        return new_list
    
    new_list_of_numbers = square_list(list_of_numbers)
    new_list_of_numbers
  4. 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.
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    def delete_numbers(numbers):
        numbers.remove(5)
        numbers.remove(6)
        numbers.remove(8)
        numbers.remove(5)
    delete_numbers(list_of_numbers)
    list_of_numbers


Practice Functions, Lists, Loops

Perform the Following Instructions:
  1. Create the ~/ops435/lab3/lab3f.py script. The purpose of this script is to use functions to modify items inside a list.
    # Place my_list here
    
    def add_item_to_list(my_list):
        # Appends new item to end of list which is the (last item + 1)
    
    def remove_items_from_list(my_list, items_to_remove):
        # Removes all values, found in items_to_remove list, from my_list
    
    if __name__ == '__main__':
        print(my_list)
        add_item_to_list(my_list)
        add_item_to_list(my_list)
        add_item_to_list(my_list)
        print(my_list)
        remove_items_from_list(my_list, [1,5,6])
        print(my_list)
  • 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:
run lab3f.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8]
[2, 3, 4, 7, 8]
3. Sample Import 1:
from lab3f import *                                                                                                                                                            [1/1899]

my_list
[1, 2, 3, 4, 5]

add_item_to_list(my_list)
add_item_to_list(my_list)
add_item_to_list(my_list)

my_list
[1, 2, 3, 4, 5, 6, 7, 8]

remove_items_from_list(my_list, [1,5,6])

my_list
[2, 3, 4, 7, 8]
4. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecac.on.ca/~acoatley-willis/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3f
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)

 
Students should be prepared with all required commands (system information) displayed in a terminal (or multiple terminals) prior to calling the instructor for signoff.


Have Ready to Show Your Instructor:
Output of: ./CheckLab3.py -f -v
Output of: cat lab3a.py lab3b.py lab3c.py lab3d.py lab3e.py lab3f.py
Lab3 logbook notes completed



Practice For Quizzes, Tests, Midterm & Final Exam

  1. x
  2. x
  3. x