Winter 2019 CISC101 5/17/2019 CISC101 Reminders

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Built-in Data Structures in Python An Introduction.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
String and Lists Dr. José M. Reyes Álamo.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Why exception handling in C++?
CMPT 120 Topic: Python strings.
Exceptions and files Taken from notes by Dr. Neil Moore
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Assn 3 due Friday, this week.
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Exceptions and files Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Python - Strings.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
String and Lists Dr. José M. Reyes Álamo.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
Fundamentals of Python: First Programs
CS190/295 Programming in Python for Life Sciences: Lecture 3
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Computer Science
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Topics Basic String Operations String Slicing
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Topics Basic String Operations String Slicing
Presentation transcript:

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

Today A last resort for functions that cannot complete their task: Raising Exceptions. Start Strings. Winter 2019 CISC101 - Prof. McLeod

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

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

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

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

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

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

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

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

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

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

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 Emails 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

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

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

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

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

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

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

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

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

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 (6553510, 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

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

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

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

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

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

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

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

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

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

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

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