Changes

Jump to: navigation, search

OPS435 Python Lab 4

4,390 bytes added, 09:26, 21 January 2020
no edit summary
<font color='red'>
'''** DO NOT USE - TO BE UPDATED FOR CENTOS 8.0 **'''
</font>
= OBJECTIVES =
:The '''first investigation''' in this lab will focus on '''Data Structures'''. Each data structure has its own advantages and limitations. This lab will provide you will additional scripting tools to help us write even more effective Python scripts to be applied to practical application involving VM management and deployment in future labsemphasize the most important differences between them.
:The '''first investigation''' in this lab will focus on '''Data Structures'''. In Wikipedia (http://searchsqlserver.techtarget.com/definition/data-structure)<br>''"A data structure is defined as a specialized format for organizing and storing data. Any data structure is designed to organize data to suit a specific purpose so that it can be accessed and worked with in appropriate ways."'' :Each data structure has its own advantages and limitations. This lab will emphasize the most important differences as they relate to Python scripting.  :The '''second investigation''' will focus closely on strings. You have been using and storing strings since our first class, however in this lab we will dive into the more complex nature of string manipulation. Finally, this lab will cover how to use a variety of different regular expression functions, for searching and input validation.
=== PYTHON REFERENCE ===
:As you develop your Python scripting skills, you may start to be "overwhelmed" with the volume of information that you have absorbed over these labs. One way to help, is to write what you have learned in your labs into your lab logbook. Also, in programming, it is important learn to use online references effectively in order to obtain information regarding Python scripting techniques and tools.
:Below is a table with links to useful online Python reference sites (by category). You may find these references useful when performing assignments, etc.
|- style="background-color:white;border:none;"
| style="border: 2px solid black;" valign="top"| :*[https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences Tuples]<br> :*[https://docs.python.org/3/tutorial/datastructures.html#sets Sets]| style="border: 2px solid black;" | :*[https://docs.python.org/3/tutorial/introduction.html#lists Lists]<br>:*[https://docs.python.org/3/tutorial/datastructures.html#more-on-lists More on Lists]<br>:*[https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions List Comprehensions]| style="border: 2px solid black;" valign="top"| :*[https://docs.python.org/3/tutorial/introduction.html#strings Strings]<br>:*[https://docs.python.org/3/library/string.html String Comparisons]| style="border: 2px solid black;" valign="top"| :*[https://docs.python.org/3/library/re.html Regular Expression Operations]<br>:*[https://docs.python.org/3/howto/regex.html Regular Expressions (HOWTO)]| style="border: 2px solid black;" valign="top"| :*[https://docs.python.org/3/tutorial/datastructures.html#dictionaries Dictionaries]
|}
 
<br><br>
= INVESTIGATION 1: DATA STRUCTURES =
:In this investigation, you will learn several tools when using data structures commonly used in Python scripting.:These tools include '''tupleslists''', '''setstuples''', '''dictionariessets''', and more '''advanced list functionsdictionaries'''.
== PART 1 - 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 changeschanged. In fact, the structure of nothing in a tuple cannot can be changed after it's created (like adding, removing list elements).
:There are many advantages to using tuples when creating Python scripts:
::*'''Data protection''' (eg. values are are NOT allowed to change like income tax rate, social insurance number, etc)::*The data structure in a tuple cannot be changed (eg. '''structure cannot be corrupted''so you won't modify them accidentally)
::*Tuples can be used as '''keys in data dictionaries''' (which are NOT allowed to change)
::*Tuples allow for '''faster access''' than lists
:Term The term to indicate that a data structure cannot be changed is called '''immutable''' (as opposed to ''"mutable"'' which means the data structure can be changed).
:'''Perform the Following Steps:'''
:#Launch your ipython3 shell:<source>ipython3</source>Let's create two tuplesin a temporary Python file, so we can learn how to use them and learn how they differ from lists.<br><br>Note: '''tuples are defined by using parenthesis ( )''' as opposed to '''lists which are defined by using square brackets [ ]'''<br><br>:#Issue the following:<sourcelang="python">
t1 = ('Prime', 'Ix', 'Secundus', 'Caladan')
t2 = (1, 2, 3, 4, 5, 6)
</source>
:#Values from a tuple can be retrieved in the same way as a list. For example, issue the following:<sourcelang="python">print(t1[0])print(t2[2:4])
</source>
:#You can also check to see whether a value exists inside a tuple or not. To demonstrate, issue the followingtry:<sourcelang="python">print('Ix' in t1)print('Geidi' in t1)</source>Let's now see how a tuple differs from a list. We will now create a list and note the difference between them.<br><br>:#Issue the following to create a list:<sourcelang="python">
list2 = [ 'uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635' ]
</source>
:#See if you can change the value of your list by issuing the following:<sourcelang="python">
list2[0]= 'ica100'
print(list2[0])
print(list2)
</source>.You should have been successful in changing the value of your list.<br><br>:#Now, try changing a the value of your previously-created tuple value by issuing the following:<sourcelang="python">
t2[1] = 10
</source>Did it work? Once created the tuple values will not be able to change.<br><br>If you would like a tuple with different values than the tuple you currently have, then you must create a new one.<br><br>
:#To create The following creates a new tuple, issue (t3) with a contents from a slice of the t2 tuple. Slicing works the followingsame way for tuples as for lists:<sourcelang="python">
t3 = t2[2:3]
</source>
:#You can use most of the basic operations with tuples as you did with lists.<br><br>:#To demonstrate, issue the following:<source>len(t1) # list the length of the tuplet1 * 3 # repetitiont1 + t2 # concatenation, remember this is creating a new tuple, not modifying</source>:#Also, as with lists, you can use for loops with to iterate the values of tuples. Issue the following to demonstrate:<sourcelang="python">
for item in t1:
print('item: ' + item)
== PART 2 - Sets ==
:Sets are another very similar structure to lists, they can also be modified and changed, unlike the tuple. But sets have two unique characteristics, they are unordered, and they cannot have duplicate values. The unordered part provides a added performance from hashing the values, but also means we cannot pull out a specific value at a spefici position. Any duplicate entries will immediately be deleted. Sets however are great tools for doing comparisons, finding differences in multiple sets, or finding similarities. The best part about sets are, they are fast!
:So far, you have been exposed to two structures that are used to contain data:''lists'Perform ' and ''tuples''. You can modify the values within a list as well as modify the Following Stepsstructure of a list (i.e. add and remove elements), whereby you cannot with a tuple. :In this section, you will learn about '''sets'''. A set has similar characteristics as a list, but there are two major differing characteristics: ::*Sets are '''un-ordered''':#Start the ipython3 shell:<source>*Sets '''cannot contain duplicate values'''ipython3</source>:Since new duplicate entries will be automatically removed when using sets, they are very useful for performing tasks such as '''comparisons''': '''finding similarities or differences in multiple sets'''. :#Create a couple some sets to work with.in a temporary Python file:<sourcelang="python">
s1 = {'Prime', 'Ix', 'Secundus', 'Caladan'}
s2 = {1, 2, 3, 4, 5}
s3 = {4, 5, 6, 7, 8}
</source>Note: '''Sets are defined by using braces { }''' as opposed to tuples which use parenthesis ( ), or lists which use square brackets [ ]'''<br /><br />
:#Try to access a set through the index:<source lang="python">
print(s1[0])
</source>This should have caused 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:<source lang="python">
print('Ix' in s1)
print('Geidi' in s1)
</source><br>'''Sets can be combined''', but it is important to note that any '''duplicate values (shared among sets) will be deleted'''.<br><br>
:#Print the contents of the sets and note the values that are common:<source lang="python">
print(s2)
print(s3)
</source>
:#First, access This is how you get a set through the index.containing only UNIQUE values (no duplicates) from both sets:<source>s1[0]print(s2 | s3) # returns a set containing all values from both setsprint(s2.union(s3)) # same as s2 | s3</source>:#This should Notice that both methods above have created an errorthe same result, this is not how to access data inside a set because they are unorderedwhich one you choose depends purely on your style. <br><br>Instead check of combining sets, we can display '''values that are common to see if a value both sets'''. This is inside.known in mathematical terms as an '''intersection''' between the lists:<sourcelang="python">'Ix' in s1print(s2 & s3) # returns a set containing all values that s2 and s3 share'Geidi' in s1print(s2.intersection(s3)) # same as s2 & s3
</source>
:#Sets can be combined together, any duplicates that the 2 also have their values compared against other sets share, will be deleted. Take a close look at which First find out what items are shared between the setsin '''s2''' but not in '''s3'''.This is also called a '''difference''':<sourcelang="python">print(s2)print(s3)print(s2 | - s3 ) # returns a set containing all values from both setsin s2 that are not found in s3print(s2.uniondifference(s3) ) # same as s2 | - s3
</source>
:#Instead of combining sets, we can find out what values are in both sets. This is a intersection between the lists.In order to see <sourceu>s2s3s2 & s3 # returns a set containing all values that s2 and s3 shares2.intersection(s3) # same as s2 & s3every</sourceu>:#Sets can have their values compared against other difference between both sets. First , you need to find out what items are in the '''s2''' but not in '''s3'''. This is also called a symmetric difference. But notice that it only shows values that '''s2''' contains, specifically values that '''s3''' doesn't have. So this isn't really the true difference between the sets.<source>s2s3s2 - s3 # returns a set containing all values in s2 that are not found s3s2.difference(s3) # same as s2 - s3</source>:#In order to see every difference between both sets, find the symmetric difference. This will return a set that shows all numbers that both sets do not share together.:<sourcelang="python">s2s3print(s2 ^ s3 ) # returns a set containing all values that both sets DO NOT shareprint(s2.symmetric_difference(s3)) # same as s2 ^ s3</source>Note:#These powerful features the '''set()''' function can be useful and efficient, try applying them to lists. Lists cannot perform these operations on them, instead we have to convert the lists into sets. Perform , and the comparision then '''list()''' function can convert the list back to a setsets into lists. There are two problems with doing The operations in this: Firstsection can only be applied to sets, sets are unordered so if the list order is important this will cause problems and remove orderyou need to perform a union, secondintersection, sets cannot contain duplicate valuesor difference between lists, if the list contains any duplicate values they will be deleted. However, if the list does not have any of the above requirements this is a great solution you need to convert them to some problemssets first.For example:<sourcelang="python">
l2 = [1, 2, 3, 4, 5]
l3 = [4, 5, 6, 7, 8]
new_list temporary_set = list(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)
</source>
'''=== Create a Python Script Demonstrating Comparing Sets'''===
:'''Perform the Following Instructions'''
::#Create the '''~/ops435/lab4/lab4a.py''' script. The purpose of this script will be to demonstrate the different way of comparing sets. There will be three functions, each returning a different set comparison. ::#Use this the following template to get started:<sourcelang="python">
#!/usr/bin/env python3
def join_sets(set1s1, set2s2): # join_sets will return a set that has contains every value from both set1 s1 and set2 inside its2
def match_sets(set1s1, set2s2): # match_sets will return a set that contains all values found in both set1 s1 and set2s2
def diff_sets(set1s1, set2s2):
# diff_sets will return a set that contains all different values which are not shared between the sets
</source>
:::*The match_setsjoin_sets() function should return a set that contains all values found in from both sets:::*The diff_setsmatch_sets() function should return a set that contains all values which are not shared between found in both sets:::*The join_setsdiff_sets() function should return a set that contains all values from which are not shared between both sets:::*All three functions should accept '''two arguments''' both are sets:::*The script should show the exact output as the samples:::*The script should contain no errors::::'''Sample Run 1:'''<source>run ./lab4a.py
set1: {1, 2, 3, 4, 5, 6, 7, 8, 9}
set2: {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
diff: {1, 2, 3, 4, 10, 11, 12, 13, 14}
</source>
::::'''Sample Run 2(with import):'''<source>
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}
</source>
<ol><li value='3' style="margin-left:::3. Exit the ipython3 shell, download 25px;">Download the checking script and check your work. Enter the following commands from the bash shell.:<sourcelang="bash">
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab4.py
python3 ./CheckLab4.py -f -v lab4a
</source></li><li style="margin-left:::4. 25px;">Before proceeding, make certain that you identify any and all errors in lab4a.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.</li></ol>
'''=== Create a Python Script Demonstrating Comparing Lists'''===
:'''Perform the Following Instructions'''
::#Create the '''~/ops435/lab4/lab4b.py''' script. The purpose of this script will be to improve the previous script to perform the same joins, matches, and diffs, but this time on lists. ::#Use the following as a template:<sourcelang="python">
#!/usr/bin/env python3
def join_lists(list1l1, list2l2): # join_lists will return a list that contains every value from both list1 l1 and list2 inside itl2
def match_lists(list1l1, list2l2): # match_lists will return a list that contains all values found in both list1 l1 and list2l2
def diff_lists(list1l1, list2l2):
# diff_lists will return a list that contains all different values, which are not shared between the lists
print('diff: ', diff_lists(list1, list2))
</source>
:::*The match_lists() function should return a list that contains all values found in both lists:::*The diff_lists() function should return a list that contains all values which are not shared between both lists:::*The join_lists() function should return a list that contains all values from both setslists:::*All three functions should accept '''two arguments''' both are lists:::*The script should show the exact output as the samples:::*The script should contain no errors::::'''Sample Run 1:'''<source>run ./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: [8, 9, 5, 6, 7, 8, 9]
diff: [1, 2, 3, 4, 10, 11, 12, 13, 14]
</source>
::::'''Sample Run 2(with import)under interactive python shell:'''<source>
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 [81, 9, 5, 6, 72]print(lab4b.diff_lists(list1,list2) ) # Will output [1, 20, 3, 4, 105, 11-2, 12, 13, 14-1]
</source>
:::3. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<sourcelang="bash">
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
python3 ./CheckLab4.py -f -v lab4b
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab4b.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.
== PART 3 - 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, providing a new tool to store and manipulate data with.
::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. ::'''Perform the Following Steps:'''::#Start the ipython3 shell:<source>ipython3</source>::#Start Let's begin by creating a new dictionary to practice within a temporary Python file:<sourcelang="python">
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Postal Code': 'M3J3M6'}
</source>::#The You should note that the syntax here to define a dictionary is similar to use defining sets (i.e. using '''{}''' to create a ), but unlike sets dictionaries use '''<code>key:value</code>''' pairs within the dictionary and placing , each ''key:value pairs inside '' pair is separated by commas.::#Take All the values in a close look at all the available functions available to dictionary objects<source>dir(dict_york)help(dict_york)</source>::#All values can be viewed retrieved by using the '''dictionary.values() ''' function. This particular function provides a '''list''' containing all values:<sourcelang="python">helpprint(dict_york.values()dict_york.values()</source>::#All keys to access the ''key:pair'' values within a dictionary can be viewed by retrieved using the '''dictionary.keys() function. This function provides a '''list''' containing all keysfunction:<sourcelang="python">help(dict_york.keys)
dict_york.keys()
</source>We can retrieve <u>individual</u> values from a dictionary by providing the key associated with the value:<source lang="python">
print(dict_york['Address'])
print(dict_york['Postal Code'])
</source>
::#We Dictionary keys can retrieve individual values from a dictionary by provide the key associated with the value<source>dict_york[be any '''Addressimmutable']dict_york['Postal Code']</source>::#Dictionary keys can values (i.e. not permitted for value to be any immutable changed). Types of values, such asinclude: '''strings''', '''numbers''', and '''tuples'''. Trying ::#Try adding a couple new keys key and values value to the dictionary:<sourcelang="python">
dict_york['Country'] = 'Canada'
print(dict_york)print(dict_york.values())print(dict_york.keys())
</source>
::#Study Let's change the output and add another keyprovince value to BC:value pair<sourcelang="python">
dict_york['Province'] = 'BC'
print(dict_york)print(dict_york.values())print(dict_york.keys())</source>:'''WARNING:#Dictionary keys must be unique'''. Attempting to add a key that already exists in the dictionary will <u>overwrite </u> the existing value for that key! For example:<sourcelang="python">
dict_york['Province'] = 'ON'
print(dict_york)print(dict_york.values())print(dict_york.keys())</source>::#These You should notice that value for the 'Province' key has been changed back to 'ON'.<br><br>The lists that contain the values and keys of the dictionary are not <u>real </u> python lists, - they are view "views of the dictionary::#However we can " and therefore are <u>immutable</u>. You could change these from views into usable lists by using the '''list() ''' function, the index can be used to access individual values:<sourcelang="python">
list_of_keys = list(dict_york.keys())
print(list_of_keys[0]</source> ::#Lists can be changed into sets if we would like to perform comparisons with another set<source>set_of_keys = set(dict_york.keys())set_of_values = set(dict_york.values())set_of_keys | set_of_values
</source>
::#The lists Lists can be iterated over in a used with '''for looploops''':<sourcelang="python">
list_of_keys = list(dict_york.keys())
for key in list_of_keys:
print(key)
for value in dict_york.values():
print(value)
</source>
::#The values and keys can be looped over using the index as well::#The range() function provides a list of numbers in a range. ::#The len() provides a the number of items in a list. Used together len() and range() create a list of usable indexes for a specific list<source>list_of_keys = list(dict_york.keys())list_of_values = list(dict_york.values())list_of_indexes = range(0, len(dict_york.keys()))list_of_indexeslist_of_keys[0]list_of_values[0]</source>::#Using this this list of indexes we are able to pair the keys and values of two separate lists<source>list_of_keys = list(dict_york.keys())list_of_values = list(dict_york.values())for index in range(0, len(list_of_keys)): print(list_of_keys[index] + '--->' + list_of_values[index])</source>::#Looping using indexes is not the best way to loop through a dictionary. A new dictionary could be created using this method, but this is '''not good'''<source>list_of_keys = list(dict_york.keys())list_of_values = list(dict_york.values())new_dictionary = {}for index in range(0, len(list_of_keys)): new_dictionary[list_of_keys[index]] = list_of_values[index]</source>::#The above method uses a lot of memory and loops. The best method to create a dictionary from two lists is to use the zip() function<source>list_of_keys = list(dict_york.keys())list_of_values = list(dict_york.values())new_dictionary = dict(zip(list_of_keys, list_of_values))</source>::#Loop through the keys in a dictionary also provides a easy way to get the value for each key at the same time<source>for key in dict_york.keys(): print(key + '--->' + dict_york[key])</source>::#Even better than the above, both key and value can be extracted in a single for loop using a special object<source>for key, value in dict_york.items(): print(key + ' | ' + value)</source>'''Create a Python Script for Managing Dictionaries'''===
:'''Perform the Following Instructions'''
::#Create the '''~/ops435/lab4/lab4c.py''' script. The purpose of this script will be to create dictionaries, extract data from dictionaries, and to make comparisons between dictionaries.
::#Use the following as a template:<sourcelang="python">
#!/usr/bin/env python3
def create_dictionary(keys, values):
# Place code here- refer to function specifics in section below
def split_dictionary(dictionary):
# Place code here
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)
keys, values = split_dictionary(dict_newnham)
print('Newnham Keys: ', keys)
print('Newnham Values: ', values)
keys, values = split_dictionary(york)
print('York Keys: ', keys)
print('York Values: ', values)
common = shared_values(dict_york, dict_newnham)
print('Shared Values', common)
</source>
:::*The script should contain '''threetwo''' functions::::*::'''create_dictionary() '''<ol><li>'''accepts ''' two lists as arguments keys and values, '''combines ''' these lists together to '''create ''' a dictionary<br>('''Tip:::*create_dictionary(''' use a while loop to access elements in both the keys and values lists at the same time) </li><li>'''returns a dictionary''' that has the keys and associated values from the lists</li></ol>:::*split_dictionary() accepts a single dictionary as a argument and splits the dictionary into two lists, keys and values:::*split_dictionary'''shared_values() '''returns two lists<ol><li>''': return keys, values:::*shared_values() accepts ''' two dictionaries as arguments and '''finds ''' all values that are shared between the two dictionaries<br>('''Tip:::*shared_values(''' 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 <u>both</u> lists) </li><li>'''returns a set''' containing '''ONLY values ''' found in '''BOTH dictionaries'''</li></ol>
:::*make sure the functions have the correct number of arguments required
:::*The script should show the exact output as the samples
:::*The script should contain no errors
::::'''Sample Run 1:'''<source>
run ./lab4c.py
York: {'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Address': '70 The Pond Rd', 'Province': 'ON', 'City': 'Toronto'}
Newnham Keys: ['Country', 'Postal Code', 'Address', 'Province', 'City']
Newnham Values: ['Canada', 'M2J2X5', '1750 Finch Ave E', 'ON', 'Toronto']
York Keys: ['Country', 'Postal Code', 'Address', 'Province', 'City']
York Values: ['Canada', 'M3J3M6', '70 The Pond Rd', 'ON', 'Toronto']
Shared Values {'Canada', 'ON', 'Toronto'}
</source>
::::'''Sample Run 2(with import):'''<source>
import lab4c
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
list_values = ['70 The Pond Rd', 'Toronto', 'Canada', 'M3J3M6', 'ON']
york = lab4c.create_dictionary(list_keys, list_values) york{'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'} keys, values = split_dictionary(dict_newnham) keys['Country', 'Postal Code', 'Address', 'Province', 'City'] values['Canada', 'M2J2X5', '1750 Finch Ave E', 'ON', 'Toronto'] keys, values = split_dictionary(york) keys['Country', 'Postal Code', 'Address', 'Province', 'City']
valuesprint(york)[# Will print: {'CanadaAddress': '70 The Pond Rd', 'M3J3M6City': 'Toronto', 'Country': '70 The Pond RdCanada', 'Postal Code'ON: 'M3J3M6', 'TorontoProvince': 'ON']}
common = lab4c.shared_values(dict_york, dict_newnham)
print(common)# Will print: {'Canada', 'ON', 'Toronto'}
</source>
:::3. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<sourcelang="bash">
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab4.py
python3 ./CheckLab4.py -f -v lab4c
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab4c.py. When the checking script tells you everything is OK before proceeding 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 4 1 - List Comprehension Strings and Substrings ==:We've already covered lists to a degree. Lets move into more advanced functions to use and generate lists. This is a very common practice in Pythonfirst part will explain basic concepts of using strings, understanding how to generate, manipulateprinting strings, and apply functions to items inside a list can be incredibly useful. List comprehension is a way to build new lists from existing list and to do it faster than simply looping over listsmanipulating 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 start try out several different functions:<source lang="python">print(course_name.lower()) # Returns a string in lower-case lettersprint(course_name.upper()) # Returns a string in upper-case lettersprint(course_name.swapcase()) # Returns a string with upper-case and lower-case letters swappedprint(course_name.title()) # Returns a string with creating upper-case first letter of each word, lowercase for remaining textprint(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 variableprint(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 and applying some of values<source>lower_name.split(' ') # Provide the split() function with a character to each 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 variableprint(list_of_strings) # Display listprint(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 listfirst_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. The below will 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 = 435print(course_code[0]) # Print the first character in course_codeprint(course_code[2]) # Print the third character in course_codeprint(course_code[-1]) # Print the last character in course_codeprint(str(course_number)[0]) # Turn the integer into a string, return first character in that string, and print itprint (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 the square ''' or '''"parse"''' smaller portions of each itemtext 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:<sourcelang="python">l1 = 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 useprint(course_code[0:3]) # Print the first three characters (values of index numbers 0, 41, 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 stringprint(course_name[5:]) # Print the substring '5' index until end of stringprint(course_name[-1]) # Print the last characterfor item in l1</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(item ** course_name[-2])
</source>
:#To store these squares for later use, create a new list and append Practice some of the squares to it. This will generate a new list skills that contains squared values you have learned in the same positions of the first list. This is using an existing list to create a new list.this section:<source>l1 course_name = 'Open System Automation'print(course_name[1, 2, 3, -10:]) # Return the last ten charactersprint(course_name[-10:-6]) # Try and figure out what this is returning print(course_name[0:4, 5]+ course_name[-10:-6]) # Combine substrings togetherl2 substring = course_name[0:4]+ course_name[-10:-6] # Save the combined substring as a new string for item in l1:later l2.appendprint(item ** 2substring)l1l2
</source>
:#Move 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 squaring of numbers out into it's own separate function''~/ops435/lab4/lab4d.py''' script. While the squaring example The purpose of this script is to demonstrate creating and manipulating strings. There will be four functions each will return a simple function, this example could include a more complex function that does more processing on each item in single string.:#Use the list.following template to get started:<source>def square(number):#!/usr/bin/env python3 return number ** 2# Strings 1
l1 str1 = [1, 2, 3, 4, 5]'Hello World!!'l2 str2 = []for item in l1: l2.append(square(item))'Seneca College'
l1num1 = 1500l2num2 = 1.50</source>def first_five(): #The map Place code here - refer to function can be used specifics in section below def last_seven(): # Place code here - refer to apply a function on each item specifics in a list. This is exactly what happened above, however it gives much better syntax, removes the loop, including the variable that was created inside the loop. This will make the script a little more efficient while performing the same task.<source>section below def squaremiddle_number(number): return number ** 2# Place code here - refer to function specifics in section below
l1 = [1,2,3,4,5]l2 = list(mapdef first_three_last_three(square, l1)): # Place code here - refer to function specifics in section below
l1
l2
</source>
:#The above map function requires a function, and a list. This meant that before map() could be used a function needed to be defined earlier in the script. This entire process can be avoided through the use of anonymous functions. This is the ability to create a simple function without defining it, and pass it off for use. Below we will use lambda, which will return a function, and we can use that function immediately. The function takes 1 argument x, and it will perform a single operation on x, square it.<source>
square = lambda x: x ** 2
l1 = [1,2,3,4,5]
l2 = list(map(square, l1))
l1if __name__ == '__main__':l2 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 above code is actually not particularly good, the whole purpose of using lambda here is we were avoiding the function definition and just quickly returning a function. However this does break down exactly what lambda does, it returns a function for use. Fix this by removing the square function and just use the return function from lambda. Now remember what map requires? map's first argument is a function, and map's second argument is a list. Here lambda will return a function and provide it as the first argument.<source>
l1 = [1,2,3,4,5]
l2 = list(map(lambda x: x ** 2, l1))
l1::*The script should contain '''four''' functions (use your own argument names):l2::::'''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 HelloSenecWorld!!College50.5HelegeSend!!
</source>
:#Using the list comprehensions above our code will be faster and more efficient than using multiple variables and loops.::'''Sample Run 2 (with import)'''<source>import lab4d
str1 = INVESTIGATION 2: STRINGS 'Hello World!!'str2 = 'Seneca College'num1 =1500:Strings are in their most basic form a list of characters, or a bit of text. Strings store text so that we can use them later. In this section we will cover more than just displaying that text to the screen. Here, we will go over cutting strings into sub-strings, joining strings together, searching through strings, and matching strings against patternsnum2 = 1.50
== PART 1 - String Basics ==print(lab4d.first_five(str1))<!--# Will output 'Hello':We can concatenate strings using the plus signprint(lab4d. Combining strings together to create a brand new string, strings are immutable just like tuplesfirst_five(str2))# Will output 'Senec'print(lab4d. This means everytime you change a string, you are actually creating a new stringlast_seven(str1))# Will output 'World!!'print(lab4d.last_seven(str2))<source># Will output 'College'str1 = print(lab4d.middle_number(num1))# Will output 'Paul50'str2 = print(lab4d.middle_number(num2))# Will output 'Atreides.5'str3 = print(lab4d.first_three_last_three(str1 + , str2))# Will output ' Helege' + print(lab4d.first_three_last_three(str2, str1))str3# Will output 'Send!!'
</source>
 Repetition is also a useful tool that can be used with strings::3. Repetition repeats Download the string over checking script and over a specific amount of timescheck your work. This is useful anytime you would manually be typing Enter the following commands from the same thing over againbash shell.<source>str1 = 'Paul'cd ~/ops435/lab4/str2 = 'Atreides'pwd #confirm that you are in the right directorystr3 = str1 + ' ' + str2 + ' ' + 'I'ls CheckLab4.py || wget https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab4.pystr3str3 = str1 + ' ' + str2 + ' ' + 'I'*3str3python3 ./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 -->== PART 1 - String Manipulation == == PART 1 - Regular Expressions ==proceed to the next step.
= LAB 4 SIGN-OFF (SHOW INSTRUCTOR) =
:'''Have Ready to Show Your Instructor:'''
::<span style="color:green;font-size:1.5em;">&#x2713;</span> xOutput of: <code>./CheckLab4.py -f -v</code>::<span style="color:green;font-size:1.5em;">&#x2713;</span> x:Output of:<span style="color:green;font-size:1code>cat lab4a.py lab4b.py lab4c.py lab4d.5em;">&#x2713;py</spancode> Lab4 logbook notes completed = LAB REVIEW =
# What is the purpose of a '''tuple'''? How does a tuple differ from a list?# How do you define elements within a tuple?# Write Python code to confirm if the string ''''OPS435'''' exists within the tuple called '''courses'''.# What is the purpose of a '''set'''? How do sets differ from lists or tuples?# How do you define elements within a set?# Assuming you have defined two sets called '''set1''' and '''set2'''. Write Python code to:<ol type= Practice For Quizzes"a"><li>Return a set containing all values of both sets</li><li>Returns a set containing all values in set1 that are not found in set2</li><li>Return a set containing all values that both sets DO NOT share</li></ol># What is the purpose of a dictionary?# How do you define elements within a dictionary?# Write Python commands to display for a dictionary called '''my_dictionary''' the dictionary key called '''my_key''' and a dictionary value for that key?# What is the purpose for the '''range()''', Tests'''len()''', Midterm &amp; Final Exam '''append()''', and '''map()''' functions for a dictionary?# List and briefly explain the following functions (methods) that can be used with strings:<br>'''lower()''' , '''upper()''' , '''swapcase()''' , '''title()''' , '''captilize()''' , '''split()'''# Assume you issued the following command in your ipython3 shell:<br>'''course_name = 'Programming with Python''''<br>What will be the output for each of the following Python commands?<ol type="a"><li>'''course_name[3:11]'''</li><li>'''course_name[10:]'''</li><li>'''course_name[-1]</li></ol>
# x# x# x[[Category:OPS435-Python]]
1,760
edits

Navigation menu