Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Chapter 4 Making Decisions
Introduction to Python
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
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.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
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.
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… 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.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
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 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
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.
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… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
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:
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 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Coding Concepts (Basics)
CSC1018F: Intermediate Python
CISC/CMPE320 - Prof. McLeod
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
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
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,
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 2 due today.
CMPE212 – Reminders Assignment 3 due next Friday.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
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
Winter 2019 CISC101 5/17/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

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 – still! Conditionals – still! File I/O, catching Exceptions. Exercises 1 to 5. Sets not on quiz. Winter 2016CISC101 - Prof. McLeod2

Today… Functions, Cont.: –Go over Keyword and Default arguments again. –Variable numbers of positional arguments. –Using None as a default argument. –Checking argument types. Next topic is Strings. Winter 2016CISC101 - Prof. McLeod3

4 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. McLeod5 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. McLeod6 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. McLeod7 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. McLeod8Winter 2016

Variable Length Parameter Lists How can print() accept any number of positional arguments? See ConcatenateDemo.py So, in the function, the *args parameter is an iterable containing all positional arguments. It will not include keyword arguments. Winter 2016CISC101 - Prof. McLeod9

Example: Our Robust Input Function So far, our input function has to have two limits defined for the legal low and legal high limits for the desired number. Suppose there is no upper or lower limit – how can you use this function? Set the two limits to some default value – but what should that value be? Your only choice is to set them to None. (This keyword can be used to create variables without actually assigning them a value.) See myInputAgain.py again and testMyInput.py. CISC101 - Prof. McLeod10Winter 2016

Our Robust Input Function, Cont. Note that we could not use a variable set to None in a comparison (or in an expression that carries out a calculation). So, we had to alter the code in the function. Is it worth it? Only two things left to fix: –What if the invoking code supplies a string for lowLimit or highLimit? –What if the invoking code supplies a lowLimit > highLimit? CISC101 - Prof. McLeod11Winter 2016

Checking Argument Types A disadvantage of loosely typed language like Python is that the interpreter cannot check to make sure that you are supplying the proper argument types to a function. Improper argument types will result in a run-time error unless the function itself checks. What should the function do if argument types are not correct? CISC101 - Prof. McLeod12Winter 2016

Checking Argument Types, Cont. A function that expects a number for an argument might get a string (or something else!) instead. Ouch! So, a *very* robust function will check argument types. For example if numArg is supposed to be a number, check to see if type(numArg) == type(1) or type(numArg) == type(1.1) is True. CISC101 - Prof. McLeod13Winter 2016

Most! Robust Input Function Or, you could just have used: type(numArg) == int or type(numArg) == float Other types: str, list, set, etc… See the mrInput module, and the program testMrInput.py. If the lowLimit and highLimit values are reversed then just switch them. What to do if they are the same? CISC101 - Prof. McLeod14Winter 2016

isinstance() Another BIF that you can use to check types. For example: >>> aVal = 1234 >>> isinstance(aVal, int) True >>> isinstance(aVal, str) False Winter 2016CISC101 - Prof. McLeod15

Summary Keyword arguments allow you some flexibility in how you supply arguments when invoking a function. If you have a variable number of parameters, then you have to use keyword args (like for print()). Default arguments are used in a function definition to assign default values to parameters that do not have to be mapped when the function is invoked. The *param usage allows you to have variable length paramter lists. CISC101 - Prof. McLeod16Winter 2016

Summary, Cont. Other languages (like Java, but not C++) have to use method overloading to provide the flexibility of default arguments. The keyword None can be used to assign a default value to arguments when you cannot decide on a sensible value but still want that parameter to be optional. You should check argument types in a Python function. Use the type() or isinstance() BIFs. CISC101 - Prof. McLeod17Winter 2016