COMP 204: Sets, Commenting & Exceptions

Similar documents
COMP 204: Sets, Commenting & Exceptions

COMP 204: Dictionaries Recap & Sets

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Jarek Szlichta

COMP 364: Conditional Statements Control Flow

Getting Started with Python

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

Fundamentals of Programming (Python) Getting Started with Programming

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

Lecture 9: July 14, How to Think About Debugging

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

61A Lecture 25. Friday, October 28

Exceptions & a Taste of Declarative Programming in SQL

Programming Fundamentals and Python

Pairs and Lists. (cons 1 2) 1 2. (cons 2 nil) 2 nil. Not a well-formed list! 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) ( ) (Demo)

Python for Non-programmers

Python I. Some material adapted from Upenn cmpe391 slides and other sources

CS61A Lecture 32. Amir Kamil UC Berkeley April 5, 2013

Lecture 4. Defining Functions

Exception Handling. Genome 559

CS1 Lecture 3 Jan. 18, 2019

Lecture #12: Quick: Exceptions and SQL

ENGR 101 Engineering Design Workshop

MUTABLE LISTS AND DICTIONARIES 4

CSC Software I: Utilities and Internals. Programming in Python

Introduction to Python! Lecture 2

Getting Started Values, Expressions, and Statements CS GMU

Introduction to python

Not-So-Mini-Lecture 6. Modules & Scripts

Data Structures. Lists, Tuples, Sets, Dictionaries

MEIN 50010: Python Flow Control

7. (2 pts) str( str( b ) ) str '4' will not compile (single, double, or triple quotes

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Collections. Lists, Tuples, Sets, Dictionaries

LECTURE 4 Python Basics Part 3

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CS1 Lecture 3 Jan. 22, 2018

Class extension and. Exception handling. Genome 559

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CSE : Python Programming

Python Intro GIS Week 1. Jake K. Carr

An Introduction to Python

COMP 364: Functions II

Python. Tutorial Lecture for EE562 Artificial Intelligence for Engineers

Chapter 9: Dealing with Errors

What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

>>> * *(25**0.16) *10*(25**0.16)

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Programming in Python

Python: common syntax

Here n is a variable name. The value of that variable is 176.

Advanced Python. Executive Summary, Session 1

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Python. Chapter 4. Sets

Python for Informatics

Cosmology with python: Beginner to Advanced in one week. Tiago Batalha de Castro

Basic Syntax - First Program 1

CS Programming Languages: Python

2. Explain the difference between read(), readline(), and readlines(). Give an example of when you might use each.

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Some material adapted from Upenn cmpe391 slides and other sources

Variables, expressions and statements

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Try and Error. Python debugging and beautification

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

CMSC201 Computer Science I for Majors

Lecture 21. Programming with Subclasses

Exceptions CS GMU

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

A Problem. Loop-Body Returns. While-Loop Solution with a Loop-Body Return. 12. Logical Maneuvers. Typical While-Loop Solution 3/8/2016

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Algorithms and Programming I. Lecture#12 Spring 2015

Class extension and. Exception handling. Genome 559

ALICE: An introduction to progamming

Abstract Data Types Chapter 1

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Python. Executive Summary

CSCA08 Winter Week 12: Exceptions & Testing. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Chapter 5 Errors. Bjarne Stroustrup

1 Lecture 6: Conditionals and Exceptions

Exceptions & error handling in Python 2 and Python 3

CSE 111 Bio: Program Design I Lecture 4: Variables, Functions, Strings, Genbank

Lecture 17A: Finite and Infinite Iterators

Intro. Scheme Basics. scm> 5 5. scm>

Slide Set 15 (Complete)

Debugging. BBM Introduc/on to Programming I. Hace7epe University Fall Fuat Akal, Aykut Erdem, Erkut Erdem, Vahid Garousi

Chapter 2.6: Testing and running a solution

University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

Strings and Testing string methods, formatting testing approaches CS GMU

CSE 140 Winter 2014: Midterm Exam

Lecture 21. Programming with Subclasses

Transcription:

COMP 204: Sets, Commenting & Exceptions Material from Carlos G. Oliver, Christopher J.F. Cameron October 12, 2018 1/31

Reminder CSUS is holding a midterm review session on Monday, October 15th, from 6-9pm. The Facebook group is here: https://www.facebook.com/events/1721064144671158/ 2/31

Sets: the unordered container for unique things Syntax: myset = {1, 2, 3} or myset = set([1, 2, 3]) (careful, myset = {} is an empty dictionary) Sets never contain duplicates. Python checks this using the == operator. 1 >>> myset = set([1, 1, 2, 3]) 2 set([1,2, 3]) #only keep unique values 3 >>> myset.add(4) 4 set([1, 2, 3, 4]) 5 >>> myset.add(1) 6 set([1, 2, 3, 4]) 7 #get unique characters of string 8 >>> charset = set("aaaccggga") 9 {A, C, G} Sets can only contain immutable objects (like dictionary keys) Elements in sets do not preserve their order. 3/31

Useful set methods and operations Membership testing 1 >>> 4 in myset 2 False Set intersection (elements common to A and B, if A and B are sets) 1 >>> A = {"a", "b", "c"} 2 >>> B = {"a", "b", "d"} 3 >>> A & B # equivalent to: A.intersection(B) 4 set(["a", "b"]) Click here for a full list of set functionality. 4/31

Useful set methods and operations Set difference (elements in A that are not in B) 1 >>> A - B 2 set(["c"]) #same as: A.difference(B) Set union (Elements found in A or B) 1 >>> A B # equivalent to: A.union(B) 2 set(["a", "b", "c", "d"]) These can be applied to multiple sets 1 >>> C = {"a", "c", "d", "e"} 2 >>> A & B & C # A.intersection(B, C) 3 set(["a"]) #elements common to A and all others 5/31

Practice problems 1. Write a program that counts the number of unique letters in a given string. E.g. "bob" should give 2. 2. Write a program that checks whether a list of strings contains any duplicates. ['att', 'gga', 'att'] should return True 6/31

1 # 1. long way 2 uniques = 0 3 for c in "bob": 4 if c not in bob: 5 uniques += 1 6 #1. short way 7 len(set("bob")) 8 #2. long way 9 uniques = [] 10 mylist = ['att', 'gga', 'att'] 11 for item in mylist: 12 if item not in uniques: 13 uniques.append('att') 14 if len(uniques)! = len(mylist): 15 print("found duplicates") 16 #3. short way 17 if len(set(mylist))!= len(mylist): 18 print("found duplicates") 7/31

Practice problem: putting it all together You re going to create your own dating app. Each user s profile is a dictionary with the following keys: 'movies' set of strings. 'foods' set of strings. 'genome' set of DNA strings. The user database will also be a dictionary where each key is a person s name and the value is its profile dictionary. E.g. database[ bob ] maps to 1 { 2 'movies':{'legally blonde', 'mission impossible'}, 'foods': {'mexican', 'vegetarian'}, 3 'genes': {'AAC', 'AAT', "GGT", "GGA"} 4 } 8/31

Your app will support 3 functions: 1. add_user(name, profile, database) creates a key for the user with its profile info and returns the updated database. (assume all names given are unique) 2. compatibility_score(user_1, user_2, database) Returns the compatibility score between two user profiles. Given as: similarity(u1, u2) = # of movies in common + # of foods in common + genome diversity i.e. number of genes in u1 or u2 but not in both. 3. most_compatible(user, database) returns user with the highest compatibility score to user. 9/31

Commenting: rules of thumb Comments should be informative but not overly detailed. Comments should be indented with the block they address Which is better? 1 #this line binds an empty list to the name 'students' 2 students = [] 3 for s in students: 4 #loop over list and print 5 print(s) 1 #keep track of students in a list 2 students = [] 3 #display student list 4 for s in students: 5 print(s) 10/31

Commenting: Docstrings A triple quoted string directly under a function header is stored as function documentation. 1 def my_max(lili): 2 """ Input: an iterable 3 return: max of list 4 """ 5 return max(lili) 1 >>> help(my_max) 2 Help on function my_max in module main : 3 4 my_max(lili) 5 Input: an iterable 6 return: max of list 11/31

Tips on coding style Be critical of your code. is this the best it can be? Avoid hard-coding for i in range(len(mylist)) is better than for i in range(5) Give objects meaningful names. Avoid names like string, list, number, result, x, y When lines get too long you are either doing something wrong or you should break the line 1 for mylistitem in [innerlistitem in 2 originallist if innerlistitem / 2 + 4 > 9]: 3 print("hi") A complete description of Python s coding style guidelines is here 12/31

Bugs: when things break You will probably have noticed by now that things don t always go as expected when you try to run your code. We call this kind of occurrence a bug. One of the first uses of the term was in 1946 when Grace Hopper s software wasn t working due to an actual moth being stuck in her computer. 1 1 Wikipedia 13/31

Types of bugs There are three major ways your code can go wrong. 1. Syntax errors 2. Exceptions (runtime) 3. Logical errors 14/31

Syntax Errors: Furiously sleep ideas green colorless. 2 When you get a syntax error it means you violated a writing rule and the interpreter doesn t know how to run your code. Your program will crash without running any other commands and produce the message SyntaxError with the offending line and a ^ pointing to the part in the line with the error. Game: spot the syntax errors! 1 print("hello) 2 x = 0 3 while True 4 x = x + 1 5 mylist = ["bob" 2, False] 6 if x < 1: 7 print("x less than 1") 2 Noam Chomsky (1955) 15/31

Exceptions: Colorless green ideas sleep furiously 3 If you follow all the syntax rules, the interpreter will try to execute your code. However, the interpreter may run into code it doesn t know how to handle so it raises an Exception The program has to deal with this Exception if it is not handled, execution aborts. Note: unlike with syntax errors, all the instructions before the interpreter reaches an exception do execute. Here is a list of all the built-in exceptions and some info on them. 3 Noam Chomsky (1955) 16/31

Exceptions: ZeroDivisionError There are many types of exceptions, and eventually you will also be able to define your own exceptions. I ll show you some examples of common Exceptions. ZeroDivisionError 1 x = 6 2 y = x / (x - 6) #syntax is OK, executing fails 3 4 File "test.py", line 2, in <module> 5 y = x / (x - 6) 6 ZeroDivisionError: integer division or modulo by zero 17/31

Exceptions: NameError Raised when the interpreter cannot find a name-binding you are requesting. Usually happens when you forget to bind a name, or you are trying to access a name outside your namespace. 1 def foo(): 2 x = "hello" 3 foo() 4 print(x) 5 Traceback (most recent call last): 6 File "exceptions.py", line 4, in <module> 7 print(x) 8 NameError: name 'x' is not defined 18/31

Exceptions: IndexError Raised when the interpreter tries to access a list index that does not exist 1 mylist = ["bob", "alice", "nick"] 2 print(mylist[len(mylist)]) 3 4 Traceback (most recent call last): 5 File "exceptions.py", line 2, in <module> 6 print(mylist[len(mylist)]) 7 IndexError: list index out of range 19/31

Exceptions: TypeError Raised when the interpreter tries to do an operation on a non-compatible type. 1 >>> mylist = ["bob", "alice", "nick"] 2 >>> mylist + "mary" 3 4 Traceback (most recent call last): 5 File "<stdin>", line 1, in <module> 6 TypeError: can only concatenate list (not "int") to list 7 8 # this is okay 9 >>> mylist * 2 10 ["bob", "alice", "nick", "bob", "alice", "nick"] 20/31

Traceback When an exception is raised, you get a traceback message which tells you where the error was raised. 1 def foo(): 2 return 5 / 0 3 def fee(): 4 return foo() 5 fee() 6 7 Traceback (most recent call last): 8 File "exception.py", line 5, in <module> 9 fee() 10 File "exception.py", line 4, in fee 11 return foo() 12 File "exception.py", line 2, in foo 13 return 5 / 0 14 ZeroDivisionError: division by zero 21/31

Where do exceptions come from? Exceptions come from raise statements. Syntax: raise [exception object] You can choose to raise any exception object. Obviously a descriptive exception is preferred. You can even define your own exceptions but we leave this for a later lecture. 1 def my_divide(a, b): 2 if b == 0: 3 raise ZeroDivisionError 4 else: 5 return a / b 6 def my_divide(a, b): 7 if b == 0: 8 raise TypeError 9 else: 10 return a / b 22/31

Handling Exceptions When an exception is raised, the exception is passed to the calling block. If the calling block does not handle the exception, the program terminates. 1 #unhandled exception 2 def list_divide(numerators, denominators): 3 ratio = [] 4 for a, b in zip(numerators, denominators): 5 ratio.append(my_divide(a, b)) 6 return ratio 7 list_divide([1, 2, 1, 0], [1, 1, 0, 2]) Life Hack 1 The zip(*args) function lets you iterate over lists simultaneously. Yields tuple at each iteration with (a[i], b[i]). 23/31

try and except Python executes the try block. If the code inside the try raises an exception, python executes the except block. 1 #exception handled by caller 2 def list_divide(numerators, denominators): 3 ratio = [] 4 for a, b in zip(numerators, denominators): 5 try: 6 ratio.append(my_divide(a, b)) 7 except ZeroDivisionError: 8 print("division by zero, skipping") 9 continue 10 return ratio 11 list_divide([1, 2, 1, 0], [1, 1, 0, 2]) 24/31

Try/except: a more realistic example Often exceptions are caused by external users giving the program data it is not expecting. 1 #not handling exceptions 2 while True: 3 #if user gives invalid input program crashes 4 x = int(input("give me a number: ")) 5 #handling exceptions 6 while True: 7 try: 8 x = int(input("give me a number: ")) 9 break 10 except TypeError: 11 print("not a number! Try again.") 25/31

Try/except/else: when no exception occurs An else block after a try/catch executes only if the try does not cause an exception. 1 while True: 2 try: 3 a = int(input("give me a numerator: ")) 4 b = int(input("give me a denominator: ")) 5 except: 6 print("not a number! Try again.") 7 else: 8 print(f"{a} divided by {b} is {my_divide(a, b)}") 9 break 26/31

Why not just do this? 1 while True: 2 try: 3 a = int(input("give me a numerator: ")) 4 b = int(input("give me a denominator: ")) 5 print(f"{a} divided by {b} is {my_divide(a, b)}") 6 break 7 except: 8 print("not a number! Try again.") 27/31

And finally, the finally statement The finally block always executes after the try-except-else blocks. Useful when: 1. The except or else block itself throws an exception. 2. The try trows an unexpected exception. 3. A control flow statement in the except skips the rest. Why is it useful? Often there are statements you need to perform before your program closes. If there is an exception you forgot to handle, the finally will still execute. 28/31

finally example 1 while True: 2 try: 3 a = int(input("give me a numerator: ")) 4 b = int(input("give me a denominator: ")) 5 except: 6 print("not a number! Try again.") 7 break 8 else: 9 result = my_divide(a, b) 10 finally: 11 print("hello from finally!") 12 print("hello from the other siiiiide") 29/31

Okay one last thing: assert The assert statement is a shortcut to raising exceptions. Sometimes you don t want to execute the rest of your code unless some condition is true. 1 def divide(a, b): 2 assert b!= 0 3 return a / b If the assert evaluates to True then an AssertionError exception is raised. Pro: quick and easy to write Con: exception error may not be so informative. Used mostly for debugging and internal checks than for user friendliness. 30/31

Logical errors When according to Python your code is fine and runs without errors but it does not do what you intended. Example: spot the logical error 1 #1 2 def my_max(mylist): 3 for bla in mylist: 4 my_max = 0 5 if bla > my_max: 6 my_max = bla 7 return my_max There s nothing to do to avoid logical errors other than testing your code thoroughly and having a good algorithm. Logical errors are often silent but deadly. 31/31