OPS435 Python Lab 3

From CDOT Wiki
Revision as of 20:23, 9 August 2017 by Andrew (talk | contribs) (LAB OBJECTIVES)
Jump to: navigation, search

LAB OBJECTIVES

In previous labs, you learned some programming tools in order to make your Python scripts more functional and allowed your Python script to run differently based on different data or situations. These tools included variables, condition statements and loops. The utilization of these basic tools not only apply to Python scripts, but basically all programming languages including interpreted (including Perl scripts, Bash Shell scripts, JavaScript, etc ) and compiled languages (including C, C++, Java, etc).
In this lab, you will learn functions, lists, and loops, with the primary focus on creating reusable code.
Objectives
Write Python code in order to:
  • Create reusable functions that can be imported by ipython3 or other python scripts
  • Using and manipulating lists to allow for processing a large amount of data quickly
  • Looping through lists using Functions. Looping (iteration) is the ability for your program to repeatedly run the same code over and over. In this way, you can run a loop that contains a list to better send data to functions for better, more efficient execution of your Python script.



INVESTIGATION 1: CREATING SIMPLE FUNCTIONS

A very simple definition of using functions is to create and reuse smaller programs within a larger program. In programming languages such as C, C++ and Java, commonly used functions are pre-packaged in Libraries. This relates to dependency issues that were discussed when compiling C programming code in your OPS25 course: if a supporting library is missing, the program would be able to run the called function. We do not go into detail involving Libraries for this course since Python is an interpreted (i.e. not a compiled) language.
Usually, a function will contain programming code in some part of the main program (most likely near the top of the program BEFORE the main program). We refer to that as a "function declaration".
When a program is run, the function's code is read into internal memory, ready to be run when the function is run (referred to as calling the function). Until a Function is specifically told to execute, it's code will sit (in internal memory) unused.
When creating programs that define and use functions, a large programming task can be broken-down into smaller elements (or modules). This is why creating programs that use functions is referred to as "modular programming".

PART 1 - How User-Defined Functions are Declared and Run

Functions may be designed not to accept arguments or return a value, designed to not accept arguments but not return a value, designed to accept arguments and not return a value, or designed to both accept arguments and return a value. In this investigation, will will focus of creating functions that either do NOT return a value, or return a value.

Functions and Strings

You will now learn how to define and run functions that will return string data when a function is called.
Let's experiment with defining and running functions. One neat thing with using scripting languages like Python or Bash, is that you can define and run functions from the shell and call them from the shell to test them out prior to adding them into scripting files. You will learn how to do this first in your ipython3 shell, and then incorporate them into your Python script files to run.
Perform the Following Steps:
  1. To start, open the ipython3 shell:
    ipython3
    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.

  2. Enter the following code in your ipython3 shell:
    def hello():
        print('Hello World')
        print('Inside a Function')
    Remember to press ENTER a second time to return to the ipython prompt. You may have notices that nothing happened. Well actually, something did happen... the function called hello() has been defined and stored in internal memory in order for it to run when called by its function name. Now that our function was created, we can use it over and over.

  3. To execute the code inside the function, run the function name with "()" brackets at the end of the function name.
    Try running the hello() function by name three times by issuing the following in the ipython3 shell:
    hello()
    hello()
    hello()
    You should notice that the function just does the same thing over-and-over no matter how many times your call the function by name. By the way, that is OK. On the other hand, you may want to create and use a function to do something, like perform error checking or some other task that returns a value to the main program for further processing. For example, a true or false value if the error checking function that was called was detected no errors or detected an error. But let's stick to some simple examples first, before tackling more complex use of functions.

  4. Let's create a function that returns some data after the function is called. This function does not print out any text: instead; it creates new variables and at the end returns the value of one of the variables.
    def return_text_value():
        name = 'Terry'
        greeting = 'Good Morning ' + name 
        return greeting
  5. Call the function in your ipython3 shell by issuing the following:
    return_text_value()
    One major difference between a function returning a value and simply printing a value is that returned values can be caught and stored in variables used in the program (that called the function) for later use. Once the returned value has been stored, it can be printed, manipulated, compared in IF statements, etc. Below will cover how to store a returned value.

  6. Enter the following function in the ipython3 shell to see returning a variable's value works:
    text = return_text_value()
  7. 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 Numbers (Integers)

You will now learn how to define and run functions that will return integer data when a function is called. In this section, you will define a function that 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!
Perform the Following steps:
  1. Issue the following in your ipython3 shell:
    def return_number_value():
        num1 = 10
        num2 = 5
        num3 = num1 + num2
        return num3
  2. Now, issue the following in the ipython3 shell to call the return_number_value() function:
    number = return_number_value()
    print(number)
    print(number + 5)
    print(return_number_value() + 10)
    What do you notice?

  3. Now, issue the following which use the print() statement to display both strings and numbers:
    number = return_number_value()
    print('my number is ' + number)
    What do you notice? You should notice a warning message. This occurs because the returning value is a number and NOT a string! Combining numbers and strings in a statement (such as print()) can cause errors. The error message should appear similar to the one displayed below:
    ---------------------------------------------------------------------------
    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 str() predefined function that was discussed in a previous lab in order to convert the returned number into a string:
    number = return_number_value()
    print('my number is ', number)
    print('my number is ' + str(number))
    print('my number is ' + str(return_number_value()))
Make notes for all of your observations in your lab log book, and proceed to Part 2.

PART 2 - Creating a Python Script with Functions and Importing Functions

Creating a Python Script

Now it's time to create a Python script that uses two functions. One function does not return a value and is simply used (when called) to greet the user, where the other function returns the result of adding two values (stored in variables within the function).
Now it's time to create a new script to prompt the user to enter data and display that data on their terminal. Refer to variable name and prompt text information when creating your Python script. Refer to Sample Runs displayed below for exact prompt and output requirements.
Perform the following Instructions:
  1. Create a new script ~/ops435/lab3/lab3a.py
Input / Output Requirements
  • The script should have a Shebang line
  • Below the Shebang line, add an empty line followed by a comment stating: # return_text_value() function
  • Add an empty line followed by the return_text_value() function definition that you previously entered in the shell.
  • Add another empty line followed by a comment stating: # return_number_value() function
  • Add another empty line following by the return_number_value() function definition that you previously entered in the shell.
  • Add a couple of empty lines, following by a comment stating: # Main Program
  • Add another couple of empty lines, followed by the statements displayed below:
    if __name__ == '__main__':
        print('python code')
        text = return_text_value()
        print(text)
        number = return_number_value()
        print(str(number))
2. Run your lab3a.py script in your ipython3 shell by issuing:
run lab3a.py
You should have seen three lines being displayed: the text "python code", a greeting, and a result of a math calculation.

Importing Functions From other Python Scripts

The IF statement in the code displayed in the previous section is considered to be a special IF statement. It allows variables and functions from other python scripts to be used in your current Python script (or in the ipython shell) without having to re-define those functions. In order to use functions from other scripts, you are required to either issue the import command in your ipython3 shell environment, or use the import command within your Python script itself.

We can demonstrate the use of re-using functions from another script by simply issuing statements from the ipython shell by calling a function that is related to your lab3a.py Python script. But care MUST be taken to first use the import command to load in the function declarations from your Python script to your ipython shell environment first.

Perform the following Instructions:
  1. Let's see what happens if we forget to import functions from your lab3a.py script prior to calling a function. Issue the following:
    text = lab3a.return_text_value()
    You should notice an error indicating "name 'lab3a' is not defined". This error occurs since you failed to instruct the ipython shell to import or "load existing defined functions from your lab3a.py script" to internal memory.

  2. Issue the following within the ipython shell:
    import lab3a
    text = lab3a.return_text_value()
    text
    lab3a.return_number_value()
    You should notice that all of the function calls should now work. NOTE: since you are in the ipython shell, the import command only will work as long as you are logged into that shell. You will need to use other methods (contained in other Python Scripts (or future ipython shell sessions) to make these function imports persistent, but you will learn about that at a later time.

  3. 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
  4. Before proceeding, make certain that you identify any and all errors in lab3a.py. When the checking script tells you everything is OK before proceeding to the next step.
Make notes for all of your observations in your lab log book, and proceed to INVESTIGATION 2.



INVESTIGATION 2: CREATING ADVANCED FUNCTIONS

PART 1 - Providing Functions With Arguments

Functions can pass-up arguments to be used for processing. In this section, you will learn how to define functions that accept arguments and learn how to perform function calls that pass up arguments for processing (such as mathematical operations or testing conditions, which is useful for error-checking).

Passing up Single and Multiple Arguments to a Function

Perform the Following Steps:
  1. Start the ipython3 shell:
    ipython3
    When passing arguments up to functions, you MUST contain data such as strings, numbers, or variable names within brackets immediately following the function name.

    NOTE: If a function accepts arguments, then those arguments must be defined (using variable names) when the function is declared.. Those declared variable names are then used within the function for processing. Also, when you call a function with arguments, the number of arguments passed up to the function must correspond (match) the number of arguments that were specified in the function declaration.

  2. Issue the following from the ipython shell to declare a function called square():
    def square(number):
        return number ** 2
    FYI:You may have learned that you multiple a number by itself in order to "square" the number. In many computing languages, this can be performed by the mathematical expression: number ** 2.

    In this case, your function takes one argument (number), and the function will perform the mathematical operation. You should notice that the square of the number sent up to the function, and the result is returned to the main program (in our case, the ipython3 shell).

    When calling functions that contain arguments to be passed, multiple arguments are separated by commas. Be careful NOT to provide strings (without using quotes) or using numbers that use decimals in these examples, as you may cause errors.
  3. Issue the following to test your square() function:
    square(5)
    square(10)
    square(12)
    square(square(2))
    square('2')
    The last function call should produce an error message. This is caused by sending a string instead of a number that is processed by the function. By using the int() function, any value passed up by a string by mistake will be converted to an integer number.

  4. Issue the following to declare the function sum_numbers():
    def sum_numbers(number1, number2):
        return int(number1) + int(number2)
  5. Issue the following from the ipython shell to see what happens:
    sum_numbers(5, 10)
    sum_numbers(50, 100)
  6. Issue the following to issue a function within another function:
    square(sum_numbers(5, 5))
    NOTE: 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 the value 10 from the will be used as the argument for the outer function. In the example below, sum_numbers(5, 5) will return 10, thus providing that result to be square with that value square(10).

Practice Creating a Function that Accepts Arguments and Returns a Value

It is time to practice creating a shell script that uses a function that accepts arguments, and returns a value.
Perform the Following Instructions:
  1. Create a new script ~/ops435/lab3/lab3b.py. Refer to the Python Script template and the Additional Requirements sections when creating your Python script. Refer to Sample Run and Sample Imports displayed below for exact prompt and output
Python Script Template
#!/usr/bin/env python3

def sum_numbers(number1, number2):
    # Make this function add number1 and number2 and return the value

def subtract_numbers(number1, number2):
    # Make this function subtract number1 and number2 and return the value
    # Remember to make sure the function accepts 2 arguments

def multiply_numbers(number1, number2):
    # Make this function multiply number1 and number2 and return the value
    # Remember to make sure the function accepts 2 arguments

if __name__ == '__main__':
    print('python code')
    print(sum_numbers(10, 5))
    print(subtract_numbers(10, 5))
    print(multiply_numbers(10, 5))
Additional Requirements
  • All functions should accept two arguments
  • All functions should return an integer
  • The script should contain no errors
Sample Run:
run lab3b.py
15
5
50
Sample Run Using import:
import lab3b

lab3b.sum_numbers(10, 5)
15
lab3b.sum_numbers(25, 25)
50
lab3b.subtract_numbers(10, 5)
5
lab3b.subtract_numbers(5, 10)
-5
lab3b.multiply_numbers(10, 5)
50
lab3b.multiply_numbers(10, 2)
20
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 lab3b
3. Before proceeding, make certain that you identify any and all errors in lab3b.py. When the checking script tells you everything is OK before proceeding to the next step.

Passing up Multiple Arguments and Using Logic Statements

You will now create a more complex function that will not only pass-up arguments, but also include logic to control the direction (flow) of the function, and affect how your Python script will be run. You will create a function that uses an if/elif/else logic statement.
Perform the Following Steps:
  1. Start the ipython3 shell
    ipython3
  2. Define the following function in your ipython shell:
    def check_temperature(temp):
        if temp > 30:
            return 'hot'
        elif temp < 0:
            return 'cold'
        elif temp == 20:
            return 'perfect'
        return 'ok'
    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.
  3. Issue the following functions (with arguments) to confirm the results:
    check_temperature(50)
    'hot'
    check_temperature(20)
    'perfect'
    check_temperature(-50)
    'cold'
    check_temperature(25)
    'ok'
    check_temperature(10)
    'ok'

Create a Python Script Using an Advanced Function

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'))
  • The operate() function should use logic statements
      FYI: Remember that you MUST consistently indent ALL code for within each logic section (or test): otherwise, it may not allow the logic statement to work correctly.
  • The operate() function should accept three arguments
  • The operate() function should return the result
  • The operate() function should return an error message if the operation is unknown
      FYI: Use single quotes to return a string value. You should be able to use double quotes within the message as shown in the sample runs.
  • The script should contain show the exact output as the sample imports
  • The script should contain no errors
Sample Run 1:
run lab3c.py
15
5
50
Error: function operator can be "add", "subtract", or "multiply"
Sample Run 2 (with import):
import lab3c
lab3c.operate(10, 20, 'add')
30
lab3c.operate(2, 3, 'add')
5
lab3c.operate(100, 5, 'subtract')
95
lab3c.operate(10, 20, 'subtract')
-10
lab3c.operate(5, 5, 'multiply')
25
lab3c.operate(10, 100, 'multiply')
1000
lab3c.operate(100, 5, 'divide')
Error: function operator can be "add", "subtract", or "multiply"
lab3c.operate(100, 5, 'power')
Error: function operator can be "add", "subtract", or "multiply"
3. 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
4. Before proceeding, make certain that you identify any and all errors in lab3c.py. When the checking script tells you everything is OK before proceeding to the next step.
Make notes for all of your observations in your lab log book, and proceed to Part 2.



PART 2 - Running System Commands with Subprocess

The remainder of this lab will allow you to run operating system commands via your Python script. Although there are different ways in which to issue operating system commands, you will learn how to issue them within a Python script to run in a secure manner regardless of the type of operating system platform (eg Linux, Windows, MacOSX).

Perform the Following Steps:

  1. Start the ipython3 shell:
    ipython3
    You can issue operating system commands by using the system() function.

  2. Issue the following in ipython:
    system(ls)
    system(whoami)
    system(ifconfig)
    You should notice output from your the Linux commands that you issued.

  3. Issue the following in ipython:
    system(ipconfig)
    You should notice an error message: 'ipconfig command not found'. That error occurs since that command was an MS Windows command, and our current platform is Linux.

    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 specific operating system or a system that has those commands available. Also, allowing python to execute commands on the operating system can be a security problem. For these reasons you should only use sub-process and system commands as a last resort and stick to Python code only.

    As you may recall from lab2, you issued import sys to import special variables from the system. You can import a subprocess in order to run common non OS specific commands securely.

  4. Issue the following in ipython:
    import subprocess
  5. To view the available modules and attributes to obtain non OS specific command-like information, issue the following:
    dir(subprocess)
    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().

  6. To demonstrate, issue the following:
    p = subprocess.Popen(['date'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    dir(p)
  7. 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
  8. 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 called: free_space()
  • The function free_space() should return a string which is in utf-8 and has newline characters strip
  • Note: your output may be completely different, the free/available disk space on every computers root directory may be different.
Sample Run 1:
run lab3d.py
9.6G
Sample Run 2 (with import):
import lab3d
lab3d.free_space()
'9.6G'
3. 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
4. Before proceeding, make certain that you identify any and all errors in lab3d.py. When the checking script tells you everything is OK before proceeding to the next step.

Make notes for all of your observations in your lab log book, and proceed to INVESTIGATION 3.

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 arrays, 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 arrays.

PART 1 - Navigating Items in Lists

Perform the Following Steps
  1. Start the ipython3 shell:
    ipython3
    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.

  2. Issue the following from the ipython shell:
    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 ]
    List are constructed similar to arrays. The best way to get individual elements from a list is using the list index.
    The index is a number starting from 0 to (number_of_items - 1), the list index starts counting at 0.

  3. Issue the following in the ipython shell to obtain stored data:
    list1[0] # First element in list1
    list2[1] # Second element in list2
    list3[-1] # Last element in list3
  4. Issue the following to provide ranges of items in a list:
    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
    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.

  5. Issue of the following to create a list that contains lists:
    list4 = [ [1, 2, 3, 4], ['a', 'b', 'c', 'd'], [ 5, 6, 'e', 'f' ] ]
    The list just created only has 3 index locations. Each index points the individual list stored as the list element.
    list4[0]
    list4[1]
    list4[2]
  6. 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 element in first list
    list4[0][-1]  # Last element in first list
    list4[2][0:2] # First two elements in third list
  7. You can use different elements from existing lists to create new lists. To demonstrate, issue the following:
    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 a Python script called: ~/ops435/lab3/lab3e.py
    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.

  2. The template function names and the special if statement:
    #!/usr/bin/env python3
    
    # Create the list called "my_list" below this command (not within any function defined below)
    
    
    
    def give_list():
        # Does not accept any arguments
        # Returns the entire list unchanged
    
    def give_first_item():
        # Does not accept any arguments
        # Returns a single string that is the first item in the list
    
    def give_first_and_last_item():
        # Does not accept any arguments
        # Returns a list that includes the first and last items in the list
    
    def give_second_and_third_item():
        # Does not accept any arguments
        # Returns a list that includes the second and third items in the list
    
    if __name__ == '__main__':   # This section also referred to as a "boiler plate"
        print(give_list())
        print(give_first_item())
        print(give_first_and_last_item())
        print(give_second_and_third_item())
Additional Requirements
  • The script should have a list called my_list created BEFORE any function definition
  • The list called 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


Sample Run 1:
run  lab3e.py
[100, 200, 300, 'six hundred']
100
[100, 'six hundred']
[200, 300]
Sample Run 2 (with import):
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]
3. 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
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.
Make notes for all of your observations in your lab log book, and proceed to Part 2.

PART 2 - Manipulating Items in Lists

There are a number of ways to obtain information about lists as well as change the data that is contained within a list. In this section, you will learn how to manipulate lists.
Perform the Following Steps:
  1. Start the ipython3 shell:
    ipython3
  2. Let's perform a simple change to a list element. Issue the following in the ipython shell:
    courses = [ 'uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635' ]
    courses[0]
    courses[0] = 'eac150'
    courses[0]
    courses
    It might be useful to change a list element-by-element, but there are other more efficient methods of changing a list (for example: using functions). You will now use the dir() and help() functions to see what functions and attributes are available for manipulating lists. The help() function will also give us tips on how to use those functions. For example, you can use issue help(list-name) in order to see what functions are available to use for that list specific list.

  3. Issue the following:
    dir(courses)
    help(courses)
    Below are some examples of using functions to manipulate lists. Take time to see how each function can be a useful tool for making changes to existing lists.

  4. Issue the following:
    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_courses = courses.copy() # Create a copy of the courses list
    sorted_list.sort()           # Sort the new list
    courses
    sorted_courses
    In addition to using functions 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.

  5. Issue the following:
    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
    In addition to manipulating and obtaining characteristics of a list, it is also useful to be able to perform searches for values within lists and obtain the location of values for elements contained within 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
    One common annoyance that can occur when performing searches are error messages when performing a search for an element that happens NOT to exist in the list. A good way to prevent those type of errors is to use a logic statement to check to see if the value for an element is in a list, then the appropriate search can be performed for that existing element value.

  6. Issue the following:
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    number = 7
    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)
        print('index is: ' + str(number_index))
    else:                                           # If the statement is False, the else will run
        print(str(number) + ' is not in list_of_numbers')
Make notes for all of your observations in your lab log book, and proceed to Part 3.

PART 3 - Iterating Over Lists

This last section demonstrates an extremely useful for 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 (a "determinant" loop) will execute all indented code for each item (element) in the list. Using loops with list allow for efficient processing of stored data.
Perform the Following Steps
Let's take a moment to understand how the for loop works. The for loop will store data within a temporary variable (in the demonstration below, the variable called: item) and run code that are indented below the for loop. This operation will repeat until ALL contained items within the list have been used.

  1. Issue the following in the ipython shell:
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    for item in list_of_numbers:
        print(item)
  2. Instead of re-calling functions over and over, we can use them in a loop. When combined with using lists, this is a very efficient method of processing large volumes of data.

  3. Issue the following:
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    def square(num):
        return num * num
    
    for value in list_of_numbers:
        print(square(value))
    You should see from the previous demonstration the advantage of using lists with functions. Although useful, the demonstration above only prints the squares as opposed to saving them for future use. The next demonstration uses a function that loops through lists, squares the values, but saves the squares in a new list.

  4. Issue the following:
    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
    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 as an argument, 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.

  5. To demonstrate, issue the following:
    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.
    #!/usr/bin/env python3
    
    # Place my_list below this comment (before the function definitions)
    
    
    
    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)

Additional Requirements

  • 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)
    This function takes a single argument which is a list name itself. It will then look at the value of the last existing item in the list, it will then append a new value that is one unit bigger (i.e. +1 and modifying that same list without returning any value).
  • The script should have a function called remove_items_from_list(my_list, items_to_remove)
    This function takes two arguments: a list, and a list of numbers to remove from the list. This function will then check if those items exist within that list, and if they exist, then they will be removed. This function will modify the list without returning any value.
Sample Run 1:
run lab3f.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8]
[2, 3, 4, 7, 8]
Sample Run 2 (with import):
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]
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 lab3f
3. Before proceeding, make certain that you identify any and all errors in lab3f.py. When the checking script tells you everything is OK before proceeding to the next step.
Make notes for all of your observations in your lab log book, and proceed to INSTRUCTOR SIGN-OFF.

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