Open main menu

CDOT Wiki β

Changes

OPS445 Online Lab4

323 bytes added, 15:46, 20 January 2023
wget addr switched to github
:There are many advantages to using tuples when creating Python scripts:
::*'''Data protection''' (eg. values are are NOT allowed to change 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
=== Create a Python Script Demonstrating Comparing Sets ===
<blockquote style="margin-left:35px;">{{Admon/note|style="padding-left:25px"|NOTE|If your professor is asking you to submit labs on GitHub, first follow their instructions on Blackboard to clone the lab4 repository.}}</blockquote>
 
:'''Perform the Following Instructions'''
:#Create the '''~/ops445/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.
cd ~/ops445/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops435lab4-template/labsblob/LabCheckScriptsmaster/CheckLab4.py?raw=true' -O CheckLab4.py
python3 ./CheckLab4.py -f -v lab4a
</source></li>
cd ~/ops445/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops435lab4-template/labsblob/LabCheckScriptsmaster/CheckLab4.py?raw=true' -O CheckLab4.py
python3 ./CheckLab4.py -f -v lab4b
</source>
::'''Perform the Following Steps:'''
::#Let's begin by creating a new dictionary in a temporary Python file:<source lang="python">
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
</source>You should note that the syntax to define a dictionary is similar to defining sets (i.e. using '''{}'''), but unlike sets dictionaries use '''<code>key:value</code>''' pairs within the dictionary, each ''key:value'' pair is separated by commas.
::#All the values in a dictionary can be retrieved by using the '''dictionary.values()''' function. This particular function provides a '''list''' containing all values:<source lang="python">
cd ~/ops445/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops435lab4-template/labsblob/LabCheckScriptsmaster/CheckLab4.py?raw=true' -O CheckLab4.py
python3 ./CheckLab4.py -f -v lab4c
</source>
cd ~/ops445/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops435lab4-template/labsblob/LabCheckScriptsmaster/CheckLab4.py?raw=true' -O CheckLab4.py
python3 ./CheckLab4.py -f -v lab4d
</source>
if __name__ == '__main__':
s1 = 'Seneca'
print(s1,'contains letter s? ->',find(s1,'s')) print(s1,'contains letter S? ->',find(s1,'S'))
</source> The find() function defined above, is not needed, as it can be replaced by the python keyword '''in''', which can be used as a boolean operator. This boolean operator takes two strings, and return '''True''' if the first appears as a substring in the second.<br/>
cd ~/ops445/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops435lab4-template/labsblob/LabCheckScriptsmaster/CheckLab4.py?raw=true' -O CheckLab4.py
python3 ./CheckLab4.py -f -v lab4e
</source></li>