Download presentation
Presentation is loading. Please wait.
1
Winter 2019 CISC101 5/17/2019 CISC101 Reminders Assignment 4 due next Friday. Use lists, sets, dictionaries and design and write functions to solve some data mining exercises. Quiz 3 this week. Topics listed in last Wednesday’s lecture. Keyword and Default arguments are fair game for the quiz. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today A last resort for functions that cannot complete their task: Raising Exceptions. Start Strings. Winter 2019 CISC101 - Prof. McLeod
3
Last Resort for Functions
Our input function was able to handle any combination of messed up arguments and still do something sensible. But suppose a function encounters a difficulty that it cannot repair. What does it do? Raise an Exception! Winter 2019 CISC101 - Prof. McLeod
4
Raising Exceptions Sometimes a function just has to fail.
(Like the .index() method when the string cannot be found.) What we have been doing until now (supposing if aParam is “fail” then this is an illegal argument): def aFunction(aParam) : if aParam == "fail" : print("I am sorry, but I cannot continue!") return else : return len(aParam) Winter 2019 CISC101 - Prof. McLeod
5
Raising Exceptions, Cont.
Assuming that the function cannot repair the problem, it just has to exit. This is kinda lame: What happens if the invoking function is expecting a return value? It is not wise and often not even possible to simply return some sort of magic error code that can be used by the invoking function to detect a problem. Do different functions have different error codes? How else can the invoking function know the function that it just called could not do its job? Winter 2019 CISC101 - Prof. McLeod
6
Raising Exceptions, Cont.
The exception mechanism is a much better way of allowing a function to “abort”: No worries about the function returning something. In fact the whole return thing is irrelevant – the function never gets to return anything and the statement expecting the return value in the invoking function never has a chance to finish. The existence of an error condition is made very clear to the invoking function. The invoking function can catch the exception – maybe it will be able to fix the problem? This mechanism is an expected part of most modern programming languages. Winter 2019 CISC101 - Prof. McLeod
7
Raising Exceptions, Cont.
Do this instead: def aFunction(aParam) : if aParam == "fail" : raise ValueError("The fail argument!") else : return len(aParam) Winter 2019 CISC101 - Prof. McLeod
8
Raising Exceptions, Cont.
raise ValueError("The fail argument!") The raise keyword “raises” or “throws” the exception. The function immediately stops after the raise command is executed. You must raise an exception object – typically you would use one already defined in Python. ValueError is a good general purpose exception. And, supply a relevant message to the exception as a string literal. Winter 2019 CISC101 - Prof. McLeod
9
Raising Exceptions, Cont.
Of course if the exception is not caught, we see: Traceback (most recent call last): File "C:/Users/Alan/Teaching/CISC101/2016Winter/Code/SecondHalf/FailingFunction.py", line 10, in <module> main() File "C:/Users/Alan/Teaching/CISC101/2016Winter/Code/SecondHalf/FailingFunction.py", line 8, in main print(aFunction("fail")) File "C:/Users/Alan/Teaching/CISC101/2016Winter/Code/SecondHalf/FailingFunction.py", line 3, in aFunction raise ValueError("The fail argument!") ValueError: The fail argument! Winter 2019 CISC101 - Prof. McLeod
10
Raising Exceptions, Cont.
If we don’t want to see all that, then the function needs to be invoked in a try/except construct. Which we know how to do… Even if you cannot fix the problem at least the try/except structure gives you a chance to exit your program gracefully, without the user seeing all the red stuff! Winter 2019 CISC101 - Prof. McLeod
11
Aside - Getting the Message Out
You can extract the message from an exception with a slightly modified try/except construct: try : print(aFunction("fail")) except ValueError as message : print("Exception message:", message) Would show this to the console: Exception message: The fail argument! Winter 2019 CISC101 - Prof. McLeod
12
Raising Exceptions, Cont.
You can have as many raise statements inside a function as you need – use the same exception, but provide different messages, so the user of your function has a better idea of what the specific error is. In assignment 4, you raise a ValueError, but you also need to be prepared to catch this one and the OSError exception raised by the open() BIF. Winter 2019 CISC101 - Prof. McLeod
13
CISC101 Strings String manipulation is a frequent activity in any programming language: Web site construction / analysis. Speech analysis. Text searching and indexing. Spell and grammar checkers. Program (code) interpretation. Scanning s for SPAM. NSA, FBI, CIA, CSIS, RCMP, … A string is a kind of data structure – like a tuple, but they have lots of methods (tuples don’t)! Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
14
Help With String Methods
Look for “Text Sequence Types – str” in “Built-in Types” in the Python Standard Library. Or look up “string” then “methods” in the index. What do we already know about strings and characters?: Winter 2019 CISC101 - Prof. McLeod
15
Strings, so Far… What do we know, so far? String literals:
"Hello there! ", or 'Hey man!', or """Multiline string""" You can store a string in a variable, just like anything else. They are of type str. A conversion BIF to create strings: str( ). Winter 2019 CISC101 - Prof. McLeod
16
Strings, so Far, Cont. A BIF to input a string from the keyboard:
You can put escape sequences like: \n, \”, \’, \\, \t in a string to control how a string is displayed. The string format() method is used to help format numeric output for display. Or you can use “formatted strings” that have the f at the beginning. Winter 2019 CISC101 - Prof. McLeod
17
Strings, so Far, Cont. You can concatenate strings using +.
You can generate repeating strings using *. You can compare strings using ==, >, <, >=, <= and != (just like comparing numbers, but you must have a string on both sides of the operator). Note that == and != will work with mixed types (but so what?). Strings are compared on the basis of the ASCII code values for their individual characters. Winter 2019 CISC101 - Prof. McLeod
18
Characters, so Far The Unicode character value takes two bytes of memory for a value between 0x0000 and 0xFFFF. Python uses Unicode already. The ASCII system only uses one byte. Keyboard characters form the lower half of the ASCII character set. “Extended ASCII” characters occupy the upper half. Strings are immutable collections of characters. Winter 2019 CISC101 - Prof. McLeod
19
Strings, so Far, Cont. Like other collections, we have found that the following also work with strings: The slice operator [ : ] in and not in work with strings as long as a string is placed on both sides. for loops work with strings. len() works with strings. list() and tuple() will create a list or a tuple consisting of the individual characters from the string. sorted() returns a sorted list of individual characters. reversed() and enumerate() also work (along with a for loop). See StringDemo.py Winter 2019 CISC101 - Prof. McLeod
20
Strings vs Tuples Strings are immutable like Tuples:
Cannot put the slice operator on the left side of the assignment operator. del does not work. But, Tuples only have two methods – count() and index(). Strings have many methods – 44 of them! Winter 2019 CISC101 - Prof. McLeod
21
Characters What strings are made of!
CISC101 Characters What strings are made of! In Python, a character literal is just a string of length one. A Character has to be stored as a numeric value in RAM (how else?). So, we need to know which numeric values stand for which characters. Thus the Unicode system which needs a two-byte number to store the character’s numeric code. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
22
Character BIFs chr() takes an integer argument, which is the Unicode number and returns the corresponding character. This number can range from 0x0000 to 0xFFFF ( , 2 bytes, compared to ASCII’s 1 byte). ord() does the reverse of chr() returning the character code value, given a single character as a string. Winter 2019 CISC101 - Prof. McLeod
23
CISC101 String Methods Just like lists, a string object owns many methods (at least 44). Lots of them (35 – a subset – are listed here)! As Douglas Adams would say: String method signatures will be provided with any question needing these methods. Or a Python Aid Sheet will be provided with the exam. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
24
string_variable.method_name()
String Methods, Cont. How can you see a list of all the string methods? First: 3 slides going through the methods in alphabetical order without any other description. Note the use of default arguments. Remember that they are invoked as in: string_variable.method_name() Winter 2019 CISC101 - Prof. McLeod
25
string.count(str, beg=0, end=len(string))
string.capitalize() string.center(width) string.count(str, beg=0, end=len(string)) string.endswith(str, beg=0, end=len(string)) string.expandtabs(tabsize=8) string.find(str, beg=0, end=len(string)) string.format(args) string.index(str, beg=0, end=len(string)) string.isalnum() string.isalpha() string.isdigit() string.islower() string.isspace() string.istitle() Winter 2019 CISC101 - Prof. McLeod
26
string.partition(str)
string.isupper() string.join(seq) string.ljust(width) string.lower() string.lstrip() string.partition(str) string.replace(str1, str2, num=string.count(str1)) string.rfind(str, beg=0, end=len(string)) string.rindex(str, beg=0, end=len(string)) string.rjust(width) string.rpartition(str) string.rstrip() string.split(str=“ “, num=string.count(str)) Winter 2019 CISC101 - Prof. McLeod
27
string.splitlines(num=string.count(‘\n’))
string.startswith(obj, beg=0, end=len(string)) string.strip() string.swapcase() string.title() string.translate(str, del=““) string.upper() string.zfill(width) Winter 2019 CISC101 - Prof. McLeod
28
String Methods, Cont. Each method returns something.
None of them alter the string object (strings are immutable!). Next slides: Categorize by return value type: boolean (True or False) integer another string a list or tuple of strings Winter 2019 CISC101 - Prof. McLeod
29
Boolean Returns string.endswith(str, beg=0, end=len(string))
Returns True if string has str at the end of the string or False otherwise. str is usually a string, but can be a tuple of strings. If it is a tuple, True will be returned if any one of the strings match. You have the option of limiting the search to a portion of string. string.startswith(str, beg=0, end=len(string)) Just like endswith(), but looks at the start of string instead. Winter 2019 CISC101 - Prof. McLeod
30
Boolean Returns - the “is…” Ones
string.isalnum() Returns True if all of the characters in string are alphanumeric (letters and numbers, only), False otherwise. string.isalpha() True if all alphabetic (letters only) string.isdigit() True if all digits (numbers only) string.islower() True if all letters are lower case string.isspace() True if only whitespace: tabs, spaces… string.istitle() True if “titlecased” string.isupper() True if letters are all upper case Winter 2019 CISC101 - Prof. McLeod
31
Aside - “Titlecase” Is When All Words In The String Start With A Capital Letter And All Other Letters Are Lower Case. string.title() Will return a version of string that is in Titlecase. Winter 2019 CISC101 - Prof. McLeod
32
string.count(str, beg=0, end=len(string))
Integer Returns string.count(str, beg=0, end=len(string)) Returns a count of how many times str occurs in string, or a substring of string as specified by beg and end. Winter 2019 CISC101 - Prof. McLeod
33
Integer Returns, Cont. string.find(str, beg=0, end=len(string))
string.index(str, beg=0, end=len(string)) Returns the location of the first occurrence of str in string starting the search from the beginning of the string, or searches a substring specified by beg and end. find() returns -1 if not found, index() raises an exception if not found. string.rfind(str, beg=0, end=len(string)) string.rindex(str, beg=0, end=len(string)) Same as above but searches string from the end. Winter 2019 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.