Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Built-in Data Structures in Python An Introduction.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - 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.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
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.
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:
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
Functions CIS 40 – Introduction to Programming in Python
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Assn 3 due Friday, this week.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
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
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
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.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CMPE212 – Reminders Course Web Site:
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops – still! Conditionals – still! File I/O, catching Exceptions. Exercises 1 to 5. Sets not on quiz. Winter 2016CISC101 - Prof. McLeod1

Today… Sets – for the Spell Checker assignment. Functions, Cont.: –Look at bit more closely at using functions. –Keyword and Default arguments. (if we have time) Winter 2016CISC101 - Prof. McLeod2

Sets One of the Python collection types. Makes your work in assn 3 much easier! See section 5.4 in the Python Tutorial and section 4.9 in the Python Library Reference. Winter 2016CISC101 - Prof. McLeod3

Mathematical Set Operations Set Union operation: Set Intersection operation: Winter 2016CISC101 - Prof. McLeod4 All elements from both sets. Duplicates are eliminated. Just elements that are common to both sets.

Mathematical Set Operations, Cont. Set Difference operation: There are other operations, but these are the most common ones. The assignment makes the most use of Intersection, but Difference might also be useful. Winter 2016CISC101 - Prof. McLeod5 The elements from set “A” that are not in set “B”.

Sets, Cont. Suppose you have a set of candidate words and you have the dictionary of words as a set: In order to find out which of the candidate words are actually words, carry out an Intersection operation. If you have a set of the high probability words and a set of the low probability words carry out a difference to eliminate the words from the other set that are the same. Winter 2016CISC101 - Prof. McLeod6

Sets in Python A set is mutable (use frozenset if you want an immutable set). Use the set() BIF to create a set from a list. Duplicates will be eliminated. A set is hashed, not indexed. So the slice operator and other operations like sorting and indexing will not work. Things like in, len() and the for loop will work (but you don’t need any of these in the assignment). Winter 2016CISC101 - Prof. McLeod7

Sets in Python, Cont. To carry out an Intersection, invoke the.intersection method and supply the other set as an argument. For the Difference operation, invoke the.difference method. See some sample code on the next slide: Winter 2016CISC101 - Prof. McLeod8

Python Set Interpreter Demo Winter 2016CISC101 - Prof. McLeod9

10 Invoking Functions - Overview Name the function and then use round brackets. The brackets can be empty or have one or more arguments inside. For example: print() Displays a linefeed on the console. print("Hello") Displays the string Hello on the console. print("Hello", "Alan") Displays Hello and then Alan, separated by a space on the console. Winter 2016

CISC101 - Prof. McLeod11 Invoking Functions, Cont. Arguments are separated by commas. Arguments can be: –Literal values. –Variables. –Expressions. In the case of a variable or an expression, it is first evaluated to come up with the resulting value before it is fed into the function. Winter 2016

CISC101 - Prof. McLeod12 Keyword Arguments The previous print() examples used positional arguments. We have also done things like: print("Hello", "Alan", sep="\n") The two strings will be on different lines this time. The sep="\n" thing is called a keyword argument. We will learn more about keyword arguments and default arguments later! Winter 2016

CISC101 - Prof. McLeod13 Function Returns A function may return something. Functions that don’t return anything (like print(), for example) are sometimes called procedures or void functions. Can you think of some functions that return something? –input() –float() –str() –len() –sorted() –…–… Winter 2016

CISC101 - Prof. McLeod14 Function Returns This “thing” that is returned can be any Python type such as a string, a list, an int, a float, etc. It is also possible to return multiple things in Python: a, b, c = someFunction(arg1, arg2) But, is the a, b, c part three “things” or just one thing really? What type is it? Winter 2016

CISC101 - Prof. McLeod15 A Function with Parameters Here is a (useless) function that displays the higher of two numbers: def showHighest(num1, num2) : if num1 > num2 : print(num1, "is higher.") else : print(num2, "is higher.") Winter 2016

CISC101 - Prof. McLeod16 A Function with Parameters, Cont. So, when you invoke this (completely useless) function from within some other function (main() perhaps…), you need to supply two things for the parameters - you supply two arguments: showHighest(3.4, 6.7) The code in showHighest() runs and the larger number is displayed. Within showHighest(), num1 has the value 3.4 and num2 has the value 6.7 Winter 2016

CISC101 - Prof. McLeod17 A Function with Parameters, Cont. To put it another way: The positional arguments 3.4 and 6.7 have been mapped into the parameters num1 and num2. num1 and num2 are variables that have been created in the function’s parameter list and are local to the function. Winter 2016

CISC101 - Prof. McLeod18 Functions Returning a Value How can showHighest() be changed to return the highest number instead of printing it out? (It is kind of tacky to have functions print things instead of returning them - let main() do the printing!) def getHighest(num1, num2) : if num1 > num2 : return num1 else : return num2 Winter 2016

CISC101 - Prof. McLeod19 Functions Returning a Value, Cont. Or: def getHighest(num1, num2) : if num1 > num2 : return num1 return num2 Winter 2016

CISC101 - Prof. McLeod20 Returning Values So, here are a few things you need to know about returning things: –You can have as many return statements as you want in a function. –If you don’t have a return statement, then your function does not return anything. It is invoked without expecting any value to come out of the function (no assignment required when invoking). –Execution of a function stops as soon as you execute the return statement, even if there is code after the return statement. Winter 2016

Jazzing Up the Parameter List When using a function, you might not always need or wish to supply all possible parameters. Or, you might not want to have to worry about the order of the arguments supplied. The use of keyword (when invoking) and default (when defining) arguments allows you to enhance the flexibility of how your function is used. CISC101 - Prof. McLeod21Winter 2016

CISC101 - Prof. McLeod22 Keyword Arguments Suppose you have a function with several parameters, but you don’t want to worry about supplying values in the matching order. You can use keyword arguments to supply the arguments in any order with the syntax: parameterName=argument in the parameter list. See KeywordArgumentsDemo.py Winter 2016

CISC101 - Prof. McLeod23 Keyword Arguments, Cont. All positional arguments must come before keyword arguments. But, the keyword arguments can be in any order. Unless the function has default arguments, you must still supply arguments for each parameter. Style note: No spaces on either side of the equals sign. Winter 2016

CISC101 - Prof. McLeod24 Default Arguments You can make a function a great deal more flexible by making it optional for the user to supply all the arguments. You do this by creating default arguments in your function definition statement. Default arguments must come after all positional parameters. The same syntax as for Keyword Arguments, but this time it is used in the def line instead of the invoking line. See DefaultArgumentsDemo.py Winter 2016

CISC101 - Prof. McLeod25 Default Arguments, Cont. You must decide which parameters are optional, if any. Then you must make assumptions to come up with values for those optional parameters. Supplying an argument value for a default argument replaces the default value. We will see quite a bit of this stuff when using Tkinter! Reduces the need for multiple function versions. Winter 2016

Example: The print() BIF We know that print() can have any number of positional arguments. It also has sep= and end= default arguments. They have been defaulted as sep=" " and end="\n" In other words the default separator between printed items is a space and a linefeed will be added to the output after the last item has been printed. You can only change these values using the keyword arguments. CISC101 - Prof. McLeod26Winter 2016