Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 4

484 bytes added, 14:26, 5 February 2020
m
PART 2 - Searching and Validating
= Under Construction and Review, DO NOT USE!=
= OBJECTIVES =
:The '''first investigation''' in this lab will focus on Python's '''Built-in Data Structures''' objects. Each built-in data structure object has its own advantages and limitations. This lab will emphasize the most important differences between them.
:The '''second investigation''' will focus on string objects. You have been using and storing string in objects since our first class, however in this lab we will dive into the more complex nature of string object manipulation. Finally, this lab will cover how to use a variety of built-in functions and string object methods for searching and performing input validation.
=== PYTHON REFERENCE ===
:As you develop your Python scripting language 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 learn to use online references effectively in order to obtain information regarding techniques and tools for manipulating various Python built-in objects in our Python scripts.
:Below is a table with links to useful online Python reference sites (by category). You may find these references useful when performing assignmentsfor your python coding tasks, etc.
{| class="wikitable" | style="margin-left:20px; border: 2px solid black;"
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget matrix.senecachttps://ict.onsenecacollege.ca/~acoatley-willisraymond.chan/ops435/labs/LabCheckScripts/CheckLab4.py
python3 ./CheckLab4.py -f -v lab4b
</source>
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab4.py
python3 ./CheckLab4.py -f -v lab4c
</source>
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
ls CheckLab4.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab4.py
python3 ./CheckLab4.py -f -v lab4d
</source>
==PART 2 - Searching and Validating==
:This second part will study how to look for specific character(s) in a string object and also how to verify that a given string object contains only certain type of characters.
:'''Perform the following steps:'''
offset = 0
length = len(text)
while offset < length:
print(offset, text[offset])
offset = offset + 1
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/>
'''Create a Python script to validate user input'''
:'''Perform the following instructions'''
:# Create the '''~/ops435/lab4/lab4e.py ''' script. The purpose of this script is to define a function called is_digits(), which takes a string object as its argument and return True if all the characters in the string are all digits, i.e 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9; return False if any one of the characters is not a digit.
:# Use the following template to get started:<source lang="python">
#!/usr/bin/env python3
if __name__ == '__main__':
test_list = ['x1234x3058','12x343058','12348503x','1234x8503']
for item in test_list:
if is_digits(item):
:::'''Sample run 1:'''<source lang="python">
./lab4e.py
x1234 x3058 is not an integer.12x34 3058 is not an integer.1234 8503x is an not integer.1234x 8503 is not an integer.
</source>
:::'''Sample run 2 (with import):'''<source lang="python">
import lab4e
print(is_digits('5665'))
# will output True
print(is_digits('1F'))
<li style="margin-left:25px;">Before proceeding, make certain that you identify all errors in lab4e.py. When the checking script tells you everything is OK - proceed to the next step.</li></ol>
= LAB 4 SIGN-OFF (SHOW INSTRUCTOR) === Show to Instructor in class ==
[[Image:lab1_signoff.png|thumb|right|450px|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 the following Ready to Show Your Instructor:'''
::<span style="color:green;font-size:1.5em;">&#x2713;</span> Output of: <code>./CheckLab4.py -f -v</code>
::<span style="color:green;font-size:1.5em;">&#x2713;</span> Output of: <code>cat lab4a.py lab4b.py lab4c.py lab4d.py lab4e.py</code>
 
== Upload to Blackboard by the due date ==
:: Capture and name the output of <code>python3 ./CheckLab4.py -f -v </code> as lab4_[seneca_id].txt.
:: Upload the file lab4_[seneca_id].txt and your python scripts: lab4a.py, lab4b.py, lab4c.py, lab4d.py, and lab4e.py to blackboard individually.
= LAB REVIEW =
# 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()''', '''len()''', '''append()''', and '''map()''' functions for a dictionary?
# List and briefly explain the following functions (string 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>
# Create a string object '''text = "Seneca"''' in the Python Interactive shell and study the string methods: '''text.find()''', '''text.count()''' using the built-in help() function.
[[Category:OPS435-Python]][[Category:rchan]]
14
edits