Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

Similar documents
\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Object Model Comparisons

Introduction to Python

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Table of Contents EVALUATION COPY

At full speed with Python

CIS192: Python Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Introduction to Python. Didzis Gosko

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

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

CS 11 python track: lecture 4

The current topic: Python. Announcements. Python. Python

python 01 September 16, 2016

Babu Madhav Institute of Information Technology, UTU 2015

A Brief Introduction to Python for those who know Java. (Last extensive revision: Daniel Moroz, fall 2015)

The Pyth Language. Administrivia

Senthil Kumaran S

Some material adapted from Upenn cmpe391 slides and other sources

And Parallelism. Parallelism in Prolog. OR Parallelism

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Python Intro GIS Week 1. Jake K. Carr

Positional, keyword and default arguments

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

Introduction to Python

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Ruby: Introduction, Basics

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

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

PYTHON CONTENT NOTE: Almost every task is explained with an example

Python Development with PyDev and Eclipse -

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

Rapid Application Development with

Jython. secondary. memory

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

CSE : Python Programming

Scala : an LLVM-targeted Scala compiler

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

15.1 Origins and Uses of Ruby

Python Evaluation Rules

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming

Programming with Python

Introduction to Python! Lecture 2

Artificial Intelligence A Primer for the Labs

Introduction to Python programming, II

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Advanced Algorithms and Computational Models (module A)

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

Introduction to Python

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

CS61A Lecture 36. Soumya Basu UC Berkeley April 15, 2013

Python Basics. Lecture and Lab 5 Day Course. Python Basics

Java Bytecode (binary file)

Python INTRODUCTION: Understanding the Open source Installation of python in Linux/windows. Understanding Interpreters * ipython.

Ruby: Introduction, Basics

ENGR 102 Engineering Lab I - Computation

Programming to Python

Advanced Python. Executive Summary, Session 1

Introduction to Python, Cplex and Gurobi

Unit E Step-by-Step: Programming with Python

(CC)A-NC 2.5 by Randall Munroe Python

A Tutorial Introduction. CS 3270 Chapter 1

Python Tutorial. Day 1

Table of Contents. Preface... xxi

[CHAPTER] 1 INTRODUCTION 1

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Iterators & Generators

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

F21SC Industrial Programming: Python: Classes and Exceptions

Python in 10 (50) minutes

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

A Brief Introduction to Python for those who know Java Last extensive revision: Jie Gao, Fall 2018 Previous revisions: Daniel Moroz, Fall 2015

CIS192 Python Programming

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

Introduction to Python programming, II

SPARK-PL: Introduction

CIS192 Python Programming

CIS192 Python Programming

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Coral Programming Language Reference Manual

DSC 201: Data Analysis & Visualization

CIS192 Python Programming. Robert Rand. August 27, 2015

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

OBJECT ORIENTED PROGRAMMING

Python. Executive Summary

High Level Scripting. Gino Tosti University & INFN Perugia. 06/09/2010 SciNeGhe Data Analysis Tutorial

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

STSCI Python Introduction. Class URL

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

This is an introductory tutorial, which covers the basics of Jython and explains how to handle its various modules and sub-modules.

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

Beyond Blocks: Python Session #1

Variable and Data Type I

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Transcription:

Python A Technical Introduction James Heliotis Rochester Institute of Technology December, 2009

Background & Overview

Beginnings Developed by Guido Van Rossum, BDFL, in 1990 (Guido is a Monty Python fan.) Cornerstones: simple, general-purpose, high level, object-oriented 3

Qualities Dynamically typed Compilation step rarely fails Considered by many to be a scripting language Interpreted Interactive Object-optional 4

Style Useful where Perl is useful, but simpler syntax & semantics Class style typical of dynamic languages dynamic creation of slots classes exist at run time Ruby, JavaScript 5

Runs Everywhere Python available for Linux MacOS Solaris Windows Compiled binaries are cross-platform compatible 6

Runs Everywhere Python available for Linux MacOS Solaris Windows But watch out for different releases Compiled binaries are cross-platform compatible 6

Development Environments IDLE comes with Python (from python.org) Eclipse plug-in available 7

Syntax At the very basic statement level, Python can look a lot like C++ or Java name = "James" sys.stdout.write( "Hey, " + name + \ ", pi = " + str(math.pi) ) 8

Less Infrastructure But, very importantly, that code snippet can (almost) work all by itself % python >>> import sys, math >>> name = "James" >>> sys.stdout.write( "Hey, " + name + ", pi = " + str(math.pi) + "\n" ) Hey, James, pi = 3.14159265359 % 9

Read-Evaluate-Print But it can be even simpler. % python >>> "Hello, world!" 'Hello, world!' >>> name = "James" >>> name 'James' >>> n = 22.0 / 7 >>> n 3.1428571428571428 >>> if n < 3: print( "smaller" )... >>> 10

Utility The large Python standard library contains almost any common utility one might want. Python interfaces well with other languages, due largely to its C base. Variations: CPython (fast), Jython (Java), Python.NET 11

Syntax and a bit of semantics

Be Like C? As seen, expressions, including function calls and assignment statements look a lot like C. Python programs are a list of definitions: variable assignments(=), functions(def), classes(class) Different semantics Definitions are executed. A definition creates an object 13

Operators Arithmetic + - * / // % ** Relational ==!= < > <= >= Logical and or not Bitwise & ~ 14

Operators Arithmetic Equality, by default, is reference-based, but most classes redefine it to be value-based. + - * / // % ** Relational ==!= < > <= >= Logical and or not Bitwise & ~ 14

Operators Arithmetic Equality, by default, is reference-based, but most classes redefine it to be value-based. + - * / // % ** Relational ==!= < > <= >= Logical Bitwise and or not Newsflash: "a < b < c" works! & ~ 14

Arrays Arrays, called lists (type list), are accessed with brackets. Negative indices work right (-1) to left (-length) >>> a = [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 33 ] >>> a[ 6 ] 8 >>> a[ 9 ] = 34 >>> a[ 9 ] 34 >>> a[ -2 ] 21 15

Lists versus Tuples Tuples (type tuple) are built just as lists are, but with parentheses. Tuples are immutable. >>> a = ( 0, 1, 1, 2, 3, 5, 8, 13, 21, 33 ) >>> a[ 6 ] 8 >>> a[ 9 ] = 34 TypeError: 'tuple' object does not support item assignment 16

Dictionaries Dictionaries (type dict) are hash tables. >>> d1 = {} >>> d1[ "a" ] = 4 >>> d1[ 4.3 ] = 'four point 3' >>> d1 {'a': 4, 4.3: 'four point 3'} >>> d2 = {'a': 4, 4.3: 'four point 3'} >>> d1 == d2 True >>> d3 = dict( ( ( 'a', 4 ), ( 4.3, 'four point 3' ) ) ) >>> d3 {'a': 4, 4.3: 'four point 3'} >>> d3 == d1 True 17

Dictionaries Dictionaries (type dict) are hash tables. >>> d1 = {} >>> d1[ "a" ] = 4 >>> d1[ 4.3 ] = 'four point 3' >>> d1 {'a': 4, 4.3: 'four point 3'} >>> d2 = {'a': 4, 4.3: 'four point 3'} >>> d1 == d2 True WOW--- this is a constructor! >>> d3 = dict( ( ( 'a', 4 ), ( 4.3, 'four point 3' ) ) ) >>> d3 {'a': 4, 4.3: 'four point 3'} >>> d3 == d1 True 17

String Syntax String literals: single or double quotes no character type Strings are indexable. Strings are immutable. >>> a="0112358abd" >>> a '0112358ABD' >>> a[6] '8' >>> a[9]='c' TypeError: 'str' object does not support item assignment 18

Flow Control To control program flow: if*/elif*/else while* for -- like a foreach *Parentheses not required for conditions No case statement 19

Function Definitions Because Python is dynamically typed, Function parameters have no declared types. Functions do not declare a return type. There is a return statement. If no return statement is executed by a called function, it returns None. 20

Programmers Must Indent! 21

Programmers Must Indent! if height < MINIMUM: print( "Sorry, not allowed." ) 21

Programmers Must Indent! if height < MINIMUM: print( "Sorry, not allowed." ) if height < MINIMUM: print( "Sorry, not allowed." ) else: print( "Go ahead!" ) 21

Programmers Must Indent! if height < MINIMUM: print( "Sorry, not allowed." ) if height < MINIMUM: print( "Sorry, not allowed." ) else: print( "Go ahead!" ) height = float( input( "Height? " ) ) while height > 0: if height < MINIMUM: print( "Sorry, not allowed." ) else: print( "Go ahead!" ) height = float( input( "Height? " ) ) 21

Programmers Must Indent! if height < MINIMUM: print( "Sorry, not allowed." ) if height < MINIMUM: print( "Sorry, not allowed." ) else: print( "Go ahead!" ) height = float( input( "Height? " ) ) while height > 0: if height < MINIMUM: print( "Sorry, not allowed." ) else: print( "Go ahead!" ) height = float( input( "Height? " ) ) def admit( numriders ): count = 0 height = float( input( "Height? " ) ) while count < numriders: if height < MINIMUM: print( "Sorry, not allowed." ) else: print( "Go ahead!" ) count += 1 height = float( input( "Height? " ) ) 21

Rule: Compound Statements All compound statements have at least one header line and one body, a sequence of statements. The header must end in a colon (:). The body must be uniformly indented. A one-statement body may be appended to the header. 22

Modules Three ways to execute Python: (a) (b) Type statements directly into the interpreter. Provide a module (Python code file) name on the command line to execute that module. (c) Use (a) or (b) and import other modules. 23

import module Module name: file name, without.py,.pyc,.pyo Reads and executes the named module, but keeps it in a separate namespace. idempotent! from module import list Merges module def. s into importer s namespace. 24

import module source compiled byte code optimized byte code Module name: file name, without.py,.pyc,.pyo Reads and executes the named module, but keeps it in a separate namespace. idempotent! from module import list Merges module def. s into importer s namespace. 24

Using Modules demo.py # Demo # James Heliotis, November 2009 doc = "Demonstration module" var = "I am a variable" def what(): "Prints this module's name." print( "My name is", name ) what() 25

Using Modules demo.py # Demo # James Heliotis, November 2009 $ python3 demo.py My name is main doc = "Demonstration module" var = "I am a variable" def what(): "Prints this module's name." print( "My name is", name ) what() 25

Using Modules demo.py # Demo # James Heliotis, November 2009 doc = "Demonstration module" var = "I am a variable" def what(): "Prints this module's name." print( "My name is", name ) what() $ python3 demo.py My name is main $ python3 >>> import demo My name is demo >>> demo.var 'I am a variable' >>> demo.what() My name is demo >>> help(demo) Help on module demo: NAME demo - Demonstration module FILE /Users/jeh/Source/Python/demo.py FUNCTIONS what() Prints this module's name. DATA var = 'I am a variable' 25 >>>

Using Modules demo.py # Demo # James Heliotis, November 2009 doc = "Demonstration module" var = "I am a variable" def what(): "Prints this module's name." print( "My name is", name ) what() $ python3 >>> from demo import * My name is demo >>> var 'I am a variable' >>> what() My name is demo >>> what. doc "Prints this module's name." >>> $ python3 demo.py My name is main $ python3 >>> import demo My name is demo >>> demo.var 'I am a variable' >>> demo.what() My name is demo >>> help(demo) Help on module demo: NAME demo - Demonstration module FILE /Users/jeh/Source/Python/demo.py FUNCTIONS what() Prints this module's name. DATA var = 'I am a variable' 25 >>>

Using Modules demo.py # Demo # James Heliotis, November 2009 doc = "Demonstration module" var = "I am a variable" def what(): "Prints this module's name." print( "My name is", name ) what() $ pydoc3 demo Help on module demo: NAME demo - Demonstration module FILE /Users/jeh/Source/Python/demo.py FUNCTIONS what() Prints this module's name. DATA var = 'I am a variable' 25

Using Modules demo.py # Demo # James Heliotis, November 2009 doc = "Demonstration module" var = "I am a variable" def what(): "Prints this module's name." print( "My name is", name ) what() $ pydoc3 -w demo My name is demo wrote demo.html 25

Using Modules demo.py # Demo # James Heliotis, November 2009 doc = "Demonstration module" var = "I am a variable" def what(): "Prints this module's name." print( "My name is", name ) what() $ pydoc3 -w demo My name is demo wrote demo.html 25

Packages As in Java, a Python package is just a directory containing modules and other packages. If directory a contains directory b, and directory b contains file stuff.py, That file s code is referenced via a.b.stuff. 26

import: Java vs. Python J A V A [no import] Must use fully qualified name p1.p2.foo. import p1.p2.foo foo works on its own. import p1.p2.* Anything in p1.p2 can be mentioned unqualified. P Y T H O N [no import] Item cannot be used. import p1.p2 Must use fully qualified name p1.p2.foo. from p1.p2 import foo foo works on its own. from p1.p2 import * Anything in p1.p2 can be mentioned unqualified. 27

Special Variables: Python defines many variables, functions (methods) and object attributes whose names begin and end in two underscores. These items have special connections to the compiler. 28

Special Variable Examples slots is a class variable holding list of attribute names so that no other ones can be created in that class s objects. If x+y is seen in source code, the compiler generates a call to x. add (y). name is a global variable naming the "current" module. doc is an attribute of a module containing its documentation string (see earlier demo example). 29

Special Function Typical Pattern len( x ) invokes x. len (). str( x ) invokes x. str (). iter( x ) invokes x. iter (). x[ i ] invokes x. getitem (). x.y invokes x. getattribute ( "y" ). 30

Object Orientation

Classes class Point( object ): slots = [ "x", "y" ] def init ( self, xcoord, ycoord ): self.x = xcoord self.y = ycoord def getx( self ): return self.x def gety( self ): return self.y def distfromorigin( self ): return ( self.x**2 + self.y**2 ) 0.5 def str ( self ): return "Point( " + str( self.x ) + \ ", " + str( self.y ) + " )" p = Point( 3, 4 ) print( p.getx() ) print( str( p ) ) print( p.distfromorigin() ) 32

Classes in Python Parent class(es) Multiple inheritance allowed Attributes via slots list (optional) Methods: explicit this/self first argument All features are public. 33

Construction 1. Client calls class like a function. p = Pet( "cat", "Chester", 5 ) 2. Object gets created, sans slots. 3. Object reference is prepended to the arg. list, and the method init is called. def init (self,kind,name,age):.... 34

Classes are Objects, Too. >>> x = [ 1, 2, 3 ] >>> x. class <class 'list'> >>> x. class. name 'list' >>> x. class. class <class 'type'> >>> help( type ) class type(object) type(object) -> the object's type type(name, bases, dict) -> a new type Methods defined here: : mro(...) mro() -> list return a type's method resolution order : new = <built-in method new of type object at 0x32e600> T. new (S,...) -> a new object with type S, a subtype of T prepare = <built-in method prepare of type object at 0x32e600> prepare () -> dict used to create the namespace for the class statement 35

Inheritance Because Python allows multiple inheritance, it is usually best to refer to superclasses by name, à la C++. 36

Inheritance Example from point import Point class ColoredPoint( Point ): slots = [ "c" ] def init ( self, xcoord, ycoord, color ): Point. init ( self, xcoord, ycoord ) self.c = color def str ( self ): return Point. str ( self ) + \ '[' + self.c + ']' def getcolor( self ): return self.c cp = ColoredPoint( 8, 10, "red" ) print( cp.getx() ) print( str( cp ) ) print( cp.distfromorigin() ) print( cp.getcolor() ) 37

Exceptions

In the C++/Java/C# Tradition Python exceptions work in the model of a stack-upward propagation. Started by executing a raise statement. Only instances of descendants of BaseException may be raised. Caught in an except block below a try block. except has a single argument type (variable optional) 39

Mechanics of Exceptions class MyError( Exception ): def init ( self, value ): self.value = value def str ( self ): return str( self.value ) try: raise MyError( 2 * 2 ) except MyError as e: print( 'My exception occurred, value:', e ) raise MyError( 'oops!' ) My exception occurred, value: 4 Traceback (most recent call last): File "exception1.py", line 12, in <module> raise MyError( oops! ) main.myerror: 'oops!' 40

Exceptions and Inheritance class MyError( Exception ): def init (self, value): self.value = value def str (self): return str(self.value) class MyOtherError( Exception ): def init (self, value): self.value = value def str (self): return str(self.value) exceptions = ( MyError, MyOtherError, Exception ) for i in range( len( exceptions ) ): try: raise exceptions[ i ]( i ) except MyError as e: print( "(A) A", e. class. name, 'occurred, value:', e ) except Exception as e: print( "(B) A", e. class. name, 'occurred, value:', e ) (A) A MyError occurred, value: 0 (B) A MyOtherError occurred, value: 1 (B) A Exception occurred, value: 2 41

Operations on Sequences

Slicing Once can make sub-lists and sub-tuples using slices. a = [ 0, 2, 4, 6, 8 ] a2 = a[ 2 ] a468 = a[ 2 : 5 ] # or a[ 2 : ] a048 = a[ 0 : 5 : 2 ] 43

Other Operations Concatenation [ 1, 3 ] + [ 2, 4 ] ---> [1,3,2,4] Duplication ( 0, 1 ) * 4 ---> (0,1,0,1,0,1,0,1) [ None ] * SIZE creates an array of known size 44

Sequences and Iterators An iterable is any object that responds to iter () by returning an iterator. An iterator is any object that responds to next () and throws a StopIteration exception if nothing is left. A sequence is an iterable that supports integer indexing using getitem (i) and len () 45

Sequences and Iterators An iterable is any object that responds to iter () by returning an iterator. built-in next(iterator) function An iterator is any object that responds to next () and throws a StopIteration exception if nothing is left. A sequence is an iterable that supports integer indexing using getitem (i) and len () 45

How for loops work >>> nums = [1,2,3] >>> i = nums. iter () # Get the iterator from the iterable. >>> try:... while True:... print( i. next () )... except StopIteration:... pass... 1 2 3 46

Making One s Own Iterator class range2( object ): def init ( self, n, max=none, step=none ): if step == None: self.step = 1 else: self.step = step if max == None: self.stop = n self.current = 0 else: self.stop = max self.current = n def iter ( self ): return self def next ( self ): result = self.current if result >= self.stop: raise StopIteration() else: self.current += self.step return result >>> r = range2( 1, 7, 2 ) >>> i = iter( r ) >>> try:... while True:... print( next( i ) )... except StopIteration:... pass... 1 3 5 >>> r = range2( 1, 7, 2 ) >>> for n in r: print( n )... 1 3 5 >>> r = range2( 1, 7, 2 ) >>> list( r ) [1, 3, 5] >>> 47

Generators A short-cut for building an iterator. Write the loop yourself! def range3( n, max=none, step=none ): if step == None: step = 1 if max == None: stop = n current = 0 else: stop = max current = n while current < stop: yield current current += step for i in range3( 3 ): print( i ) 48

Map A built-in function to apply a function to an iterable >>> nums = [ 1, 2, 3 ] >>> def double( x ): return 2 * x... >>> num2 = map( double, nums ) >>> num2 <map object at 0x82b758c> >>> list( num2 ) [2, 4, 6] 49

Filter A built-in function that uses a predicate to prune an iterable >>> def cutdf( grade ): return grade!= "D" and grade!= "F"... >>> f = filter( cutdf, [ "B", "A", "B", "B", "F", "A", "C", "B", "D" ] ) >>> type( f ) <class 'filter'> >>> list( f ) ['B', 'A', 'B', 'B', 'A', 'C', 'B'] >>> list( f ) [] >>> 50

Reduce A formerly built-in function to apply a function to an iterable >>> from functools import reduce >>> reduce( max, ( 1, 3, 5, 4, 2 ) ) 5 >>> reduce( min, ( 1, 3, 5, 4, 2 ), 0 ) 0 >>> def subtract(x,y): return x-y... >>> reduce( subtract, [ 4, 2, 1 ] ) 1 >>> 51

List Comprehensions Shorthand notation for generating lists Mapping section is required. Filtering section is optional. >>> [ x**2 for x in range(11) ] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>> [ ( x, x**2 ) for x in range( 5 ) ] [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)] >>> [ x**2 for x in range(11) if x % 2 == 0 ] [0, 4, 16, 36, 64, 100] >>> 52

Exercise What does this do??? def mystery( lst ): if lst == []: return [] else: return \ mystery( [ x for x in lst[ 1: ] if x < lst[ 0 ] ] ) + \ lst[ 0:1 ] + \ mystery( [ x for x in lst[ 1: ] if x >= lst[ 0 ] ] ) 53

Exercise What does this do??? def mystery( lst ): if lst == []: return [] else: return \ mystery( [ x for x in lst[ 1: ] if x < lst[ 0 ] ] ) + \ lst[ 0:1 ] + \ mystery( [ x for x in lst[ 1: ] if x >= lst[ 0 ] ] ) >>> mystery( [1,3,5,7,9,10,8,6,4,3,2,1] ) [1, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10] >>> 53

Exercise Write a function that takes two iterables (for-each-able) and creates a single list of bituples: combine( [1,2,3],(5,6) ) --> [(1,5),(2,6),(3,None)] 54

Other Important Features

Tuples as Parameter Lists A single asterisk before the last parameter says that it may be a tuple of argument values, like varargs or Java's "..". def product( *nums ): result = 1 for x in nums: result *= x return result print( product( 5, 6, 7 ) ) print( product( 8, 9 ) ) print( product( 10 ) ) print( product() ) numlist = ( 2, 3, 4 ) print( product( *numlist ) ) print( product( numlist ) ) 56 210 72 10 1 24 (2, 3, 4)

Keyword-based Parameter Lists Python allows naming arguments in a call, as long as the unnamed ones appear first. def draw( obj=none, color="black", background="white", shadow=false ): print( "Drawing " + str(obj) + ", color=" + color + \ ", bkgd=" + background + ", ", end = "" ) if not shadow: print( "no ", end = "" ) print( "shadow." ) draw() draw( "OBJ1" ) draw( "OBJ2", color = "purple" ) draw( "OBJ1", shadow = True ) draw( "OBJ1", background = "green", color = "red" ) Drawing None, color=black, bkgd=white, no shadow. Drawing OBJ1, color=black, bkgd=white, no shadow. Drawing OBJ2, color=purple, bkgd=white, no shadow. Drawing OBJ1, color=black, bkgd=white, shadow. Drawing OBJ1, color=red, bkgd=green, no shadow. 57

Keyword-based Parameter Lists Python allows naming arguments in a call, as long as the unnamed ones appear first. def draw( obj=none, color="black", background="white", shadow=false ): print( "Drawing " + str(obj) + ", color=" + color + \ ", bkgd=" + background + ", ", end = "" ) if not shadow: print( "no ", end = "" ) print( "shadow." ) draw() draw( "OBJ1" ) draw( "OBJ2", color = "purple" ) draw( "OBJ1", shadow = True ) draw( "OBJ1", background = "green", color = "red" ) Drawing None, color=black, bkgd=white, no shadow. Drawing OBJ1, color=black, bkgd=white, no shadow. Drawing OBJ2, color=purple, bkgd=white, no shadow. Drawing OBJ1, color=black, bkgd=white, shadow. Drawing OBJ1, color=red, bkgd=green, no shadow. 57

Dictionary-based Parameter Argument names can also be made to correspond to the keys in a single dictionary parameter. def demo( **params ): print( "first =", params[ 'first' ] ) print( "second =", params[ 'second' ] ) demo( first="#1", second="#2" ) args = { "second": 2, "first": 1 } demo( **args ) first = #1 second = #2 first = 1 second = 2 58

Closures Python supports the functional programming closure concept. def getnumberprinter( start ): def printparameter(): print( start ) return printparameter a = getnumberprinter( 1 ) z = getnumberprinter( 10 ) a() a() z() 1 1 10 59

Lambda Functions Anonymous functions In Python, the body is limited to a single expression. >>> ( lambda x, y: ( x + y ) / 2 )( 3, 10 ) 6.5 >>> tuple( map( lambda n: n**3, range(5) ) ) (0, 1, 8, 27, 64) >>> reduce( lambda x, y: x * y, [ 3, 2, 8, 20 ] ) 960 >>> 60

Variable Binding in Python Remember that all variable assignments, including function definitions, are executed at run time. All names in Python are variables: module names, function names, class names,... No variable s value is hard-wired during compilation. 61

Wrapping Functions def log( f ): def logandrun( *args ): print( f. name, "called with args", args ) return f( *args ) return logandrun >>> def hi( name ): print( "Hello, " + name + '!' )... >>> hi( Jim ) Hello, Jim! >>> hi = log( hi ) >>> hi( Jim ) hi called with args ('Jim',) Hello, Jim! 62

Decorators def log( f ): def logandrun( *args ): print( f. name, "called with args", args ) return f( *args ) return logandrun @log def hi( name ): print( "Hello, " + name + '!' ) >>> hi( Jim ) hi called with args ('Jim',) Hello, Jim! 63

Another Example def ensurenonnegative( f ): def runandcheck( *args ): result = f( *args ) if result < 0: raise Exception( "Negative result?" ) return result return runandcheck @ensurenonnegative def locate( lst, val ): for i in range( len( lst ) ): if lst[ i ] == val: return i return -1 64 >>> nums = range( 0, 11, 2 ) >>> locate( nums, 2 ) 1 >>> locate( nums, 6 ) 3 >>> locate( nums, 7 ) Exception: Negative result? >>>

(Not Necessarily Useful ) def checkresult( expected ): def applytofn( f ): def runandcheck( *args ): result = f( *args ) if result!= expected: raise Exception( "Test failed!" ) return result return runandcheck return applytofn >>> @checkresult( 8 )... def halve( n ):... return n // 2 >>> halve( 16 ) 8 >>> halve( 14 ) Exception: Test failed! >>> 65

Closing Remarks

Want More Information? python.org 67

Contact Information James Heliotis jeh@cs.rit.edu http://www.cs.rit.edu/~jeh 68

69