OPS445 Lab 4: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
Line 554: Line 554:
</syntaxhighlight>
</syntaxhighlight>
* Before proceeding, make certain that you identify all errors in lab4c.py. When the checking script tells you everything is OK proceed to the next step.
* Before proceeding, make certain that you identify all errors in lab4c.py. When the checking script tells you everything is OK proceed to the next step.
= INVESTIGATION 2: STRINGS =
:Strings are basically a list of characters (bits of text). This section will investigate strings in more detail such as '''cutting strings into sub-strings''', '''joining strings''', '''formatting strings''', '''searching through strings''', and '''matching strings against patterns'''.<br><br>Strings are '''immutable''' data objects - this means that once a string is created, it <u>cannot</u> be modified. In order to make a change inside a string, you would first make a copy of the part of the string (i.e. sub-string) for manipulation.
== PART 1 - Strings and Substrings ==
:This first part will explain basic concepts of using strings, printing strings, and manipulating sub-strings.
:'''Perform the Following Steps:'''
:#Create some strings in a temporary Python file:<source lang="python">
course_name = 'Open System Automation'
course_code = 'OPS435'
course_number = 435
</source>Strings can contain any '''characters''' inside them, whether they are '''letters''', '''numbers''', or '''symbols'''.
:#Strings can also be '''concatenated''' (i.e. "combined together") by using the '''+''' sign, just make sure string are only concatenating strings with strings (no lists, no numbers, no dictionaries, etc.):<source lang="python">
print(course_name)
print(course_code)
print(str(course_number))
print(course_name + ' ' + course_code + ' ' + str(course_number))
</source>When using the '''print()''' function, you can display '''special characters'''. One such special character is the  is the newline character (denoted by the symbol:  '''\n'''). This allows you to separate content between new lines or empty lines:<source lang="python">
print('Line 1\nLine 2\nLine 3\n')
</source>
:#Strings have many built-in functions that we can use to manipulate text. [https://docs.python.org/3/library/stdtypes.html#string-methods Here's a list].
:#Lets try out several different functions:<source lang="python">
print(course_name.lower())        # Returns a string in lower-case letters
print(course_name.upper())        # Returns a string in upper-case letters
print(course_name.swapcase())      # Returns a string with upper-case and lower-case letters swapped
print(course_name.title())        # Returns a string with upper-case first letter of each word, lowercase for remaining text
print(course_name.capitalize())    # Returns a string with upper-case first letter only, lowercase for remaining text
</source>
:#These values can be saved inside new strings and then reused:<source lang="python">
lower_name = course_name.lower()    # Save returned string lower-case string inside new string variable
print(lower_name)
</source>
:#If a string contains many values separated by a single character, such as a space, the string can be split on those values and create a list of values<source>
lower_name.split(' ')      # Provide the split() function with a character to split on
</source>The above example will return a list of strings, which we can access just like all of lists. <br><br>
:#Let's practice more string manipulation:<source lang="python">
list_of_strings = lower_name.split(' ')    # Split string on spaces and store the list in a variable
print(list_of_strings)                      # Display list
print(list_of_strings[0])                  # Display first item in list
</source>Since lists are actually a list of '''strings''', you should be able to use any function that works with a string on a list:<source lang="python">
list_of_strings[0].upper()          # Use the function after the index to affect a single string within a list
first_word = list_of_strings[0]
print(first_word)
</source>The '''index''' that is used to access <u>items</u> within a list, can also be used to access <u>characters</u> within a string. For practice, let's  create a new string, and start accessing the strings index:<source>
course_name = 'Open System Automation'
course_code = 'OPS435'
course_number = 435
print(course_code[0])                          # Print the first character in course_code
print(course_code[2])                          # Print the third character in course_code
print(course_code[-1])                        # Print the last character in course_code
print(str(course_number)[0])                  # Turn the integer into a string, return first character in that string, and print it
print(course_code[0] + course_code[1] + course_code[2])
</source>
:#You can use a technique that uses index numbers of a string to '''cut-out''' or '''"parse"''' smaller portions of text within a string. This term is referred to as a '''substring'''. We can use this to create a new string or display only a small portion of it:<source lang="python">
print(course_name[0:4])                # Print the first four characters (values of index numbers 0,1,2, and 3)
first_word = course_name[0:4]          # Save this substring for later use
print(course_code[0:3])                # Print the first three characters (values of index numbers 0,1,and 2)
</source>
:# The index allows a few '''extra functions''' using the same parsing technique:<source lang="python">
course_name = 'Open System Automation'
print(course_name[12:])                        # Print the substring '12' index until end of string
print(course_name[5:])                        # Print the substring '5' index until end of string
print(course_name[-1])                        # Print the last character
</source>With '''negative indices''', '''-1''' would represent the '''last''' character, '''-2''' index would represent the '''second last''' character, etc.:<source lang="python">
course_name = 'Open System Automation'
print(course_name[-1])
print(course_name[-2])
</source>
:# Practice some of the skills that you have learned in this section:<source>
course_name = 'Open System Automation'
print(course_name[-10:])                            # Return the last ten characters
print(course_name[-10:-6])                          # Try and figure out what this is returning
print(course_name[0:4] + course_name[-10:-6])      # Combine substrings together
substring = course_name[0:4] + course_name[-10:-6]  # Save the combined substring as a new string for later
print(substring)
</source>
:# The real power found in substrings goes beyond just manually writing index values and getting back words. The next part of this investigation will cover how to search through a string for a specific word, letter, number, and return the index to that search result.
'''Create a Python Script Demostrating Substrings'''
:'''Perform the Following Instructions'''
:#Create the '''~/ops435/lab4/lab4d.py''' script. The purpose of this script is to demonstrate creating and manipulating strings. There will be four functions each will return a single string.
:#Use the following template to get started:<source>
#!/usr/bin/env python3
# Strings 1
str1 = 'Hello World!!'
str2 = 'Seneca College'
num1 = 1500
num2 = 1.50
def first_five():
    # Place code here - refer to function specifics in section below
def last_seven():
    # Place code here - refer to function specifics in section below
def middle_number():
    # Place code here - refer to function specifics in section below
def first_three_last_three():
    # Place code here - refer to function specifics in section below
if __name__ == '__main__':
    print(first_five(str1))
    print(first_five(str2))
    print(last_seven(str1))
    print(last_seven(str2))
    print(middle_number(num1))
    print(middle_number(num2))
    print(first_three_last_three(str1, str2))
    print(first_three_last_three(str2, str1))
</source>
::*The script should contain '''four''' functions (use your own argument names):
::::'''first_five()''':<ol><li>Accepts a single string argument</li><li>Returns a string that contains the first five characters of the argument given</li></ol>
::::'''last_seven()''':<ol><li>Accepts a single string argument</li><li>Returns a string that contains the last seven characters of the argument given</li></ol>
::::'''middle_number()''':<ol><li>Accepts a integer as a argument</li><li>Returns a string containing the second and third characters in the number</li></ol>
::::'''first_three_last_three()''':<ol><li>Accepts two string arguments</li><li>Returns a single string that starts with the first three characters of argument1 and ends with the last three characters of argument2</li></ol>
::*Example: first_three_last_three('abcdefg', '1234567') returns single string 'abc567'
:::'''Sample Run 1'''<source>
./lab4d.py
Hello
Senec
World!!
College
50
.5
Helege
Send!!
</source>
:::'''Sample Run 2 (with import)'''<source>
import lab4d
str1 = 'Hello World!!'
str2 = 'Seneca College'
num1 = 1500
num2 = 1.50
print(lab4d.first_five(str1))
# Will output 'Hello'
print(lab4d.first_five(str2))
# Will output 'Senec'
print(lab4d.last_seven(str1))
# Will output 'World!!'
print(lab4d.last_seven(str2))
# Will output 'College'
print(lab4d.middle_number(num1))
# Will output '50'
print(lab4d.middle_number(num2))
# Will output '.5'
print(lab4d.first_three_last_three(str1, str2))
# Will output 'Helege'
print(lab4d.first_three_last_three(str2, str1))
# Will output 'Send!!'
</source>
::3. Download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab4.py
python3 ./CheckLab4.py -f -v lab4d
</source>
::4. Before proceeding, make certain that you identify all errors in lab4d.py. When the checking script tells you everything is OK - proceed to the next step.


= LAB 4 SIGN-OFF (SHOW INSTRUCTOR) =
= LAB 4 SIGN-OFF (SHOW INSTRUCTOR) =

Revision as of 18:58, 30 January 2025

!!!THIS LAB IS NOT READY YET!!!

Common Data Structures

In this lab you will learn about some non-trivial data structures commonly used in Python programming: lists, tuples, sets, and dictionaries.

Lists

A List is a non-trivial data-type in Python. You define a list using a series of comma separated values found between square brackets. Values in a list can be anything: strings, integers, objects, even other lists.

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.

Navigating items in lists

  • Create a new Python file for testing things in this section.
  • Create three lists with different values: list1 contains only integers, list2 contains only strings, list3 contains a combination of both integers and strings
    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]
    

The simplest way to access individual elements in a list is using the list index.

The index is a number starting from 0 to (number_of_items - 1). The index of the first element is 0.

  • Inspect specified elements in your lists:
    print(list1[0])  # First element in list1
    print(list2[1])  # Second element in list2
    print(list3[-1]) # Last element in list3
    
  • You can also retrieve ranges of items from a list (these are called slices):
    print(list1[0:5]) # Starting with index 0 and stopping before index 5
    print(list2[2:4]) # Starting with index 2 and stopping before index 4
    print(list3[3:])  # Starting with index 3 and going to the end
    
  • Create a Python program called: ~/ops435/lab4/lab3e.py

The program will have a number of functions which output various parts 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.

  • Start with this template:
    #!/usr/bin/env python3
    
    # Create the list called "my_list" here, not within any function defined below.
    # That makes it a global variable. We'll talk about that in another lab.
    
    
    def give_list():
        # Does not accept any arguments
        # Returns all of the global variable my_list unchanged
    
    def give_first_item():
        # Does not accept any arguments
        # Returns a single string that is the first item in the global my_list
    
    def give_first_and_last_item():
        # Does not accept any arguments
        # Returns a list that includes the first and last items in the global 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 my_list
    
    if __name__ == '__main__':   # This section also referred to as a "main code"
        print(give_list())
        print(give_first_item())
        print(give_first_and_last_item())
        print(give_second_and_third_item())
    
  • Here's what you need to do:
  1. The script should declare a list called my_list created BEFORE any function definition
  2. The list called my_list should have the values: 100, 200, 300, and "six hundred"
  3. The script should implement the empty functions - i.e. you have to fill in the bodies for these functions

Sample Run 1:

./ lab3e.py
[100, 200, 300, 'six hundred']
100
[100, 'six hundred']
[200, 300]

Sample Run 2 (with import from another script):

import lab3e
lab3e.give_list()
# Will print [100, 200, 300, 'six hundred']
lab3e.give_first_item()
# Will print 100
lab3e.give_first_and_last_item()
# Will print [100, 'six hundred']
lab3e.give_second_and_third_item()
# Will print [200, 300]
  • Download the checking script and check your work:
    cd ~/ops435/lab4/
    pwd #confirm that you are in the right directory
    ls CheckLab4.py || wget http://ops345.ca/ops445/CheckLab4.py
    python3 ./CheckLab4.py -f -v lab3e
    
  • Before proceeding, make certain that you identify any and all errors in lab3e.py. When the checking script tells you everything is OK - proceed to the next step.

Manipulating items in lists

There is 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.

  • Let's perform a simple change to a list element. Try the following code in a temporary python file:
    courses = ['uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635']
    print(courses[0])
    courses[0] = 'eac150'
    print(courses[0])
    print(courses)
    
  • Below are some examples of using built-in functions to manipulate lists. Take your time to see how each function can be a useful tool for making changes to existing lists:
    courses.append('ops235')    # Add a new item to the end of the list
    print(courses)
    
    courses.insert(0, 'hwd101') # Add a new item to the specified index location
    print(courses)
    
    courses.remove('ops335')    # Remove first occurrence of value
    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)
    
  • 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:
    list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
    length_of_list = len(list_of_numbers)    # Returns the length of the list
    smallest_in_list = min(list_of_numbers)  # Returns the smallest value in the list
    largest_in_list = max(list_of_numbers)   # Returns the largest value in the list
    
    # Notice how the long line below is wrapped to fit on one editor screen, so you don't need to scroll to read it:
    print("List length is " + str(length_of_list) + 
          ", smallest element in the list is " + str(smallest_in_list) +
          ", largest element in the list is " + str(largest_in_list))
    

Iterating over a list using the for loop

This last section demonstrates a quick and easy way to loop through every value in the list. For loops have a set number of times they loop. The for loop will execute all indented code for each item (element) in the list.

Other programming languages that have a loop such as this: it is usually called a "foreach" loop.

The following for loop's body will execute as many times as there are elements in the list_of_numbers. For each element: it will store its value in a variable named item; and run code indented below.

  • Create a temporary Python file with these contents and run it:
    list_of_numbers = [1, 5, 2, 6, 8, 5, 10, 2]
    for item in list_of_numbers:
        print(item)
    
  • As you can see: instead of writing eight function calls for each element of the list, we can call the print() function in a loop. And we won't have to rewrite code if the length of the list changes.
  • Run the following code:
    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))
    

The code above only prints the squares and does not save them for future use. The next example uses a function that loops through list, squares the values, and also saves the squares in a new list.

  • Run the following code:
    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)
    print(list_of_numbers)
    print(new_list_of_numbers)
    

The above is just one example of a quick use of 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 change the list without making a new list. While you do have to be careful, this can also be useful. A function can modify any given list without having to return it.

  • To demonstrate, run the following code:
    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)
    print(list_of_numbers)
    

Tuples

Many often confuse a tuple with a list (which you learned about in a previous lab). A tuple is a type of list whose values cannot be changed. Nothing in a tuple can be changed after it's created.

There are some advantages to using tuples for certain purposes:

  1. Data protection (eg. values are are NOT allowed to change so you won't modify them accidentally)
  2. Tuples can be used as keys in data dictionaries (which are NOT allowed to change)
  3. Tuples allow for faster access than lists

The term to indicate that a data structure cannot be changed is immutable (as opposed to "mutable" which means the data structure can be changed).

  • Create two tuples in a temporary Python file, so we can learn how to use them and learn how they differ from lists. Note that tuples are defined by using parenthesis ( ) as opposed to lists which are defined by using square brackets [ ]
    t1 = ('Prime', 'Ix', 'Secundus', 'Caladan')
    t2 = (1, 2, 3, 4, 5, 6)
    
  • Values from a tuple can be retrieved in the same way as a list. For example:
    print(t1[0])
    print(t2[2:4])
    
  • You can also check to see whether a value exists inside a tuple or not. To demonstrate try:
    print('Ix' in t1)
    print('Geidi' in t1)
    

Let's now see how a tuple differs from a list.

  • Create a list:
    list2 = ['uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635']
    
  • See if you can change the value of an item in your list:
    list2[0]= 'ica100'
    print(list2[0])
    print(list2)
    

You should have been successful in changing the value of your list.

  • Now try changing a value in your previously-created tuple:
    t2[1] = 10
    

Did it work? Once created the tuple values will not be able to change.

If you would like a tuple with different values than the tuple you currently have, then you must create a new one.

  • The following creates a new tuple (t3) with a contents from a slice of the t2 tuple. Slicing works the same way for tuples as for lists:
    t3 = t2[2:3]
    
  • Also, as with lists, you can use for loops to iterate the values of tuples:
    for item in t1:
        print('Item: ' + item)
    

Sets

A set has similar characteristics as a list, but there are two major differces:

  1. Sets are un-ordered
  2. Sets cannot contain duplicate values

Since new duplicate entries will be automatically removed when using sets, they are useful for performing tasks such as comparisons: finding similarities or differences in multiple sets.

  • Create some sets to work with in a new temporary Python file:
    s1 = {'Prime', 'Ix', 'Secundus', 'Caladan'}
    s2 = {1, 2, 3, 4, 5}
    s3 = {4, 5, 6, 7, 8}
    

Note: Sets are defined by using curly brackets { } as opposed to tuples which use parenthesis ( ), or lists which use square brackets [ ]

  • Try to access a set using an index:
    print(s1[0])
    

This will cause an error. You cannot access data inside a set this way because the elements inside are unordered.

  • Instead, you should use the in method to check to see whether a value is contained in the set:
    print('Ix' in s1)
    print('Geidi' in s1)
    

Sets can be combined, but any duplicate values (shared among sets) will be deleted.

  • Print the contents of the sets and note the values that are common:
    print(s2)
    print(s3)
    
  • This is how you get a set containing only unique values (no duplicates) from both sets:
    print(s2 | s3)         # returns a set containing all values from both sets
    print(s2.union(s3))    # same as s2 | s3

Notice that both methods above have the same result, which one you choose depends purely on your style.

Instead of combining sets, we can display values that are common to both sets. This is known in mathematical terms as an intersection between the lists.

  • Try this:
    print(s2 & s3)             # returns a set containing all values that s2 and s3 share
    print(s2.intersection(s3)) # same as s2 & s3
    

Sets can also have their values compared against other sets. First find out what items are in s2 but not in s3. This is also called a difference.

  • Try this:
    print(s2)
    print(s3)
    print(s2 - s3)             # returns a set containing all values in s2 that are not found in s3
    print(s2.difference(s3))   # same as s2 - s3
    

In order to see every difference between both sets, you need to find the symmetric difference.

  • This will return a set that shows all numbers from both sets which are not shared:
    print(s2 ^ s3)                     # returns a set containing all values from both sets which are not shared
    print(s2.symmetric_difference(s3)) # same as s2 ^ s3
    

The set() function can convert lists into sets, and the list() function can convert sets into lists. The operations in this section can only be applied to sets, so if you need to perform a union, intersection, or difference between lists, you need to convert them to sets first.

  • Try this for example:
    l2 = [1, 2, 3, 4, 5]
    l3 = [4, 5, 6, 7, 8]
    temporary_set = set(l2).intersection(set(l3))
    new_list = list(temporary_set)  # '''set()''' can make lists into sets. '''list()''' can make sets into lists.
    print(new_list)
    

Comparing Sets

  • Create the ~/ops435/lab4/lab4a.py program. The purpose of this program will be to demonstrate different ways of comparing sets. There will be three functions, each returning a different set comparison.
  • Use the following template to get started:
    #!/usr/bin/env python3
    
    def join_sets(s1, s2):
        # join_sets will return a set that contains every value from s1 and from s2
    
    def match_sets(s1, s2):
        # match_sets will return a set that contains all values found in both s1 and s2
    
    def diff_sets(s1, s2):
        # diff_sets will return a set that contains all different values which are not shared between the sets
    
    if __name__ == '__main__':
        set1 = set(range(1,10))
        set2 = set(range(5,15))
        print('set1: ', set1)
        print('set2: ', set2)
        print('join: ', join_sets(set1, set2))
        print('match: ', match_sets(set1, set2))
        print('diff: ', diff_sets(set1, set2))
    
  • Modify the program so that:
  1. The join_sets() function returns a set that contains all values from both sets.
  2. The match_sets() function should return a set that contains all values found in both sets.
  3. The diff_sets() function should return a set that contains all values which are not shared between both sets.
  4. All three functions should accept two arguments both of which are sets.
  5. The program should show the exact output as the samples.

Sample Run 1:

./lab4a.py
set1:  {1, 2, 3, 4, 5, 6, 7, 8, 9}
set2:  {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
join:  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
match:  {8, 9, 5, 6, 7}
diff:  {1, 2, 3, 4, 10, 11, 12, 13, 14}

Sample Run 2 (with import):

import lab4a
set1 = {1,2,3,4,5}
set2 = {2,1,0,-1,-2}
print(lab4a.join_sets(set1,set2))
# Will output {-2, -1, 0, 1, 2, 3, 4, 5}
print(lab4a.match_sets(set1,set2))
# Will output {1, 2}
print(lab4a.diff_sets(set1,set2))
# Will output {-2, -1, 0, 3, 4, 5}
  • Download the checking script and check your work. Enter the following commands from the bash shell:
    cd ~/ops435/lab4/
    pwd #confirm that you are in the right directory
    ls CheckLab4.py || wget http://ops345.ca/ops445/CheckLab4.py
    python3 ./CheckLab4.py -f -v lab4a
    
  • Before proceeding, make certain that you identify all errors in lab4a.py. When the checking script tells you everything is OK - proceed to the next step.

Comparing Lists

  • Create the ~/ops435/lab4/lab4b.py program. This program will improve the previous program to perform the same joins, matches, and diffs, but this time on lists.
  • Use the following as a template:
    #!/usr/bin/env python3
    
    def join_lists(l1, l2):
        # join_lists will return a list that contains every unique value from l1 and from l2
    
    def match_lists(l1, l2):
        # match_lists will return a list that contains all values found in both l1 and l2
    
    def diff_lists(l1, l2):
        # diff_lists will return a list that contains all different values, which are not shared between the lists
    
    if __name__ == '__main__':
        list1 = list(range(1,10))
        list2 = list(range(5,15))
        print('list1: ', list1)
        print('list2: ', list2)
        print('join: ', join_lists(list1, list2))
        print('match: ', match_lists(list1, list2))
        print('diff: ', diff_lists(list1, list2))
    
  • Modify the template so that:
  1. The match_lists() function returns a list that contains all values found in both lists.
  2. The diff_lists() function returns a list that contains all values which are not shared between both lists.
  3. The join_lists() function returns a list that contains all values from both lists.
  4. All three functions should accept two arguments both of which are lists.
  5. The program should show the exact output as the samples.

Sample Run 1:

./lab4b.py
list1:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
list2:  [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
join:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
match:  [5, 6, 7, 8, 9]
diff:  [1, 2, 3, 4, 10, 11, 12, 13, 14]

Sample Run 2 (with import) under interactive python shell:

import lab4b
list1 = [1,2,3,4,5]
list2 = [2,1,0,-1,-2]
print(lab4b.join_lists(list1,list2)))
# Will output [0, 1, 2, 3, 4, 5, -2, -1]
print(lab4b.match_lists(list1,list2))                                                                                                                 
# Will output [1, 2]
print(lab4b.diff_lists(list1,list2))                                                                                                                  
# Will output [0, 3, 4, 5, -2, -1]
  • Download the checking script and check your work. Enter the following commands from the bash shell.
    cd ~/ops435/lab4/
    pwd #confirm that you are in the right directory
    ls CheckLab4.py || wget http://ops345.ca/ops445/CheckLab4.py
    python3 ./CheckLab4.py -f -v lab4b
    
  • Before proceeding, make certain that you identify all errors in lab4b.py. When the checking script tells you everything is OK - proceed to the next step.

Dictionaries

In Python, a dictionary is a set of key-value pairs. Dictionaries are unordered like sets, however any value can be retrieved from a dictionary if you know the key. This section will go over how to create, access, and change dictionaries, giving you a new powerful tool to store and manipulate data.

  • Let's begin by creating a new dictionary in a temporary Python file:
    dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Postal Code': 'M3J3M6'}
    

You should note that the syntax to define a dictionary is similar to defining sets (i.e. using {}), but unlike sets dictionaries use key:value pairs within the dictionary. Each key:value pair is separated by commas.

  • All the keys within a dictionary can be retrieved using the dictionary.keys() function:
    print(dict_york.keys())
    
  • All the values in a dictionary can be retrieved in one go by using the dictionary.values() function. This particular function provides a list containing all values:
    print(dict_york.values())
    
  • We can retrieve individual values from a dictionary by providing the key associated with the value:
    print(dict_york['Address'])
    print(dict_york['Postal Code'])
    
  • Dictionary keys can be any immutable values (i.e. not permitted for value to be changed). Types of values include: strings, numbers, and tuples.
  • Try adding a new key and value to the dictionary:
    dict_york['Country'] = 'Canada'
    print(dict_york)
    print(dict_york.keys())
    print(dict_york.values())
    
  • Let's set the province value to BC:
    dict_york['Province'] = 'BC'
    print(dict_york)
    print(dict_york.keys())
    print(dict_york.values())
    
Important.png
Dictionary keys must be unique
Attempting to add a key that already exists in the dictionary will overwrite the existing value for that key!
  • For example:
    dict_york['Province'] = 'ON'
    print(dict_york)
    print(dict_york.keys())
    print(dict_york.values())
    

You should notice that value for the 'Province' key has been changed back to 'ON'.

The lists that contain the values and keys of the dictionary are not real python lists - they are "views of the dictionary" and therefore are immutable.

  • You could change these views into usable lists by using the list() function:
    list_of_keys = list(dict_york.keys())
    print(list_of_keys[0])
    
  • Lists can be used with for loops:
    list_of_keys = list(dict_york.keys())
    for key in list_of_keys:
        print(key)
    for value in dict_york.values():
        print(value)
    

Practice working with dictionaries

  • Create the ~/ops435/lab4/lab4c.py program. The purpose of this program will be to create dictionaries, extract data from dictionaries, and to make comparisons between dictionaries.
  • Use the following as a template:
    #!/usr/bin/env python3
    
    # Dictionaries
    dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
    dict_newnham = {'Address': '1750 Finch Ave E', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M2J2X5', 'Province': 'ON'}
    # Lists
    list_keys = ['Address', 'City', 'Country', 'Postal Code', 'Province']
    list_values = ['70 The Pond Rd', 'Toronto', 'Canada', 'M3J3M6', 'ON']
    
    def create_dictionary(keys, values):
        # Place code here - refer to function specifics in section below
    
    def shared_values(dict1, dict2):
        # Place code here - refer to function specifics in section below
    
    
    if __name__ == '__main__':
        york = create_dictionary(list_keys, list_values)
        print('York: ', york)
        common = shared_values(dict_york, dict_newnham)
        print('Shared Values', common)
    
  • Your create_dictionary() function should:
  1. accept two lists as arguments keys and values, combining these lists together to create a dictionary
    (Tip: use a while loop to access elements in both the keys and values lists at the same time).
  2. return a dictionary that has the keys and associated values from the lists.
  • Your shared_values() function should:
  1. accept two dictionaries as arguments and find all values that are shared between the two dictionaries
    (Tip: generate sets containing only values for each dictionary, then use a function mentioned in a previous section to store the values that are common to both lists)
  2. return a set containing ONLY values found in BOTH dictionaries

Sample Run 1:

./lab4c.py
York:  {'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Address': '70 The Pond Rd', 'Province': 'ON', 'City': 'Toronto'}
Shared Values {'Canada', 'ON', 'Toronto'}

Sample Run 2 (with import):

import lab4c
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
dict_newnham = {'Address': '1750 Finch Ave E', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M2J2X5', 'Province': 'ON'}
list_keys = ['Address', 'City', 'Country', 'Postal Code', 'Province']
list_values = ['70 The Pond Rd', 'Toronto', 'Canada', 'M3J3M6', 'ON']

york = lab4c.create_dictionary(list_keys, list_values)

print(york)
# Will print: {'Address': '70 The Pond Rd',
               'City': 'Toronto',
               'Country': 'Canada',
               'Postal Code': 'M3J3M6',
               'Province': 'ON'}

common = lab4c.shared_values(dict_york, dict_newnham)

print(common)
# Will print: {'Canada', 'ON', 'Toronto'}
  • Download the checking script and check your work. Enter the following commands from the bash shell.
    cd ~/ops435/lab4/
    pwd #confirm that you are in the right directory
    ls CheckLab4.py || wget http://ops345.ca/ops445/CheckLab4.py
    python3 ./CheckLab4.py -f -v lab4c
    
  • Before proceeding, make certain that you identify all errors in lab4c.py. When the checking script tells you everything is OK proceed to the next step.

LAB 4 SIGN-OFF (SHOW INSTRUCTOR)

File:Lab1 signoff.png
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: ./CheckLab4.py -f -v
Output of: cat lab4a.py lab4b.py lab4c.py lab4d.py

LAB REVIEW

  1. What is the purpose of a tuple? How does a tuple differ from a list?
  2. How do you define elements within a tuple?
  3. Write Python code to confirm if the string 'OPS435' exists within the tuple called courses.
  4. What is the purpose of a set? How do sets differ from lists or tuples?
  5. How do you define elements within a set?
  6. Assuming you have defined two sets called set1 and set2. Write Python code to:
    1. Return a set containing all values of both sets
    2. Returns a set containing all values in set1 that are not found in set2
    3. Return a set containing all values that both sets DO NOT share
  7. What is the purpose of a dictionary?
  8. How do you define elements within a dictionary?
  9. Write Python commands to display for a dictionary called my_dictionary the dictionary key called my_key and a dictionary value for that key?
  10. What is the purpose for the range(), len(), append(), and map() functions for a dictionary?
  11. List and briefly explain the following functions (methods) that can be used with strings:
    lower() , upper() , swapcase() , title() , captilize() , split()
  12. Assume you issued the following command in your ipython3 shell:
    course_name = 'Programming with Python'
    What will be the output for each of the following Python commands?
    1. course_name[3:11]
    2. course_name[10:]
    3. course_name[-1]