+ All Categories
Home > Documents > CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family!...

CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family!...

Date post: 06-Aug-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
63
CS 7 : Introduction to Programming and Computer Science Python is TIOBE's programming language of the year 2018! www.tiobe.com/tiobe-index “The Python programming language has won the title "programming language of the year"! Python has received this title because it has gained most ranking points in 2018 if compared to all other languages. The Python language has won 3.62%, followed by Visual Basic .NET and Java. Python has now definitely become part of the big programming languages. For almost 20 years, C, C++ and Java are consistently in the top 3, far ahead of the rest of the pack. Python is joining these 3 languages now. It is the most frequently taught first language at universities nowadays, it is number one in the statistical domain, number one in AI programming, number one in scripting and number one in writing system tests. Besides this, Python is also leading in web programming and scientific computing (just to name some other domains).” In summary, Python is everywhere. Computing in the news
Transcript
Page 1: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

CS 7 : Introduction to Programming and Computer Science

Python is TIOBE's programming language of the year 2018! www.tiobe.com/tiobe-index“The Python programming language has won the title "programming language of the year"! Python has received this title because it has gained most ranking points in 2018 if compared to all other languages. The Python language has won 3.62%, followed by Visual Basic .NET and Java. Python has now definitely become part of the big programming languages. For almost 20 years, C, C++ and Java are consistently in the top 3, far ahead of the rest of the pack. Python is joining these 3 languages now. It is the most frequently taught first language at universities nowadays, it is number one in the statistical domain, number one in AI programming, number one in scripting and number one in writing system tests. Besides this, Python is also leading in web programming and scientific computing (just to name some other domains).” In summary, Python is everywhere.

Computingin the news

Page 2: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

This material is an adaptation from CS61A material at UC Berkeley.

Credits to Professor John DeNero and the entire CS61A staff.

Acknowledgements

Page 3: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Parts of the Course

Lecture: Lecture is on Mon and Tues

Lab section: The most important part of this course

Staff office hours: The most important part of this course

Online textbook: http://composingprograms.com

Optional Discussion section: The most important part of this course

Weekly lab, homework assignments, three programming projects (hopefully)

Lots of optional special events to help you complete all this work

3

Everything is posted to erickhumalo.com/cs7

Page 4: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

An Introduction to Programming & Computer Science

Page 5: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

What is Computer Science?

Systems

Artificial Intelligence

Graphics

Security

Networking

Programming Languages

Theory

Scientific Computing

...

5

Decision Making

Robotics

Machine Learning

...

What problems can be solved using computation,How to solve those problems, andWhat techniques lead to effective solutions

The study of

Training Models

Classification

...

Creativity!

Page 6: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

What is This Course About?

A course about managing complexity

Mastering abstraction

Programming paradigms

An introduction to programming

Full understanding of Python fundamentals

Combining multiple ideas in large projects

How computers interpret programming languages

A challenging course that will demand a lot of you

6

Page 7: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Course Policies

Page 8: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Course Policies

8

Learning

Details...

http://erickhumalo.com/cs7/about.html

Community

• You don’t know that?Sheesh! (rolls eyes)

• Elitism

• “Me first” attitude

• Making students feelunwelcome

• You having trouble?Here, let me help!

• Supporting each other

• “We together” attitude

• Making students feelwelcome. We are a CS7 family!

Page 9: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Collaboration

• Discuss everything with each other; learn from your fellow students!

• Some projects can be completed with a partner

• Choose a partner from your discussion section

9

• One simple rule: Don’t share your code, except with your project partner

• Copying project solutions causes people to fail the course

The limits of collaboration

Asking questions is highly encouraged

Build good habits now

Page 10: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Announcements

• “Optional” Discussion this week

• Lab this week for setting up your workspace

• Visit the course website and browse through

10

Page 11: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Expressions

Page 12: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Types of expressions

12

An expression describes a computation and evaluates to a value

Page 13: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Call Expressions in Python

All expressions can use function call notation

(Demo 1)

13

Page 14: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Anatomy of a Call Expression

14

Evaluation procedure for call expressions:

add ( 2 , 3 )

Operator Operand Operand

Operators and operands are also expressions

1. Evaluate the operator and then the operand subexpressions

2. Apply the function that is the value of the operator

to the arguments that are the values of the operands

So they evaluate to values

Page 15: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

224mul(add(4, mul(4, 6)), add(3, 5))

add(4, mul(4, 6))

Evaluating Nested Expressions

15

28mul

add 4

mul(4, 6)

mul 4 6

24

add(3, 5)

add 3 5

8

Page 16: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

224mul(add(4, mul(4, 6)), add(3, 5))

add(4, mul(4, 6))28mul

add 4

mul(4, 6)

mul 4 6

24

add(3, 5)

add 3 5

8

Evaluating Nested Expressions

16

Expression tree

Operand subexpression

1st argument to mul

Value of the whole expression

Value of subexpression

Page 17: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Functions, Values, Objects, Interpreters, and Data

(Demo)

Page 18: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Names, Assignment, and User-Defined Functions(Goal: Get you to have a correct understanding of the Notational Machine of Python, the “set of

abstractions that define the structure and behavior of a computing device” –Guzdial)

(Demo 2)

computinged.wordpress.com/2016/03/07/notional-machines-and-misconceptions-in-cs-developing-a-research-agenda-at-dagstuhl/

Page 19: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Types of Expressions

Primitive expressions:

Call expressions:

2 add 'hello'

max ( 2 , 3 )

Operator Operand Operand

max(min(pow(3, 5), -4), min(1, -2))

Number or Numeral Name String

19

An operand can alsobe a call expression

Page 20: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Discussion Question 1

What is the value of the final expression in this sequence?

>>> f = min

>>> f = max

>>> g, h = min, max

>>> max = g

>>> max(f(2, g(h(1, 5), 3)), 4)

???

20

Page 21: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Environment Diagrams

Page 22: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Environment Diagrams

(Demo 3)

Name Value

Import statement

Each name is bound to a value

Within a frame, a name cannot be repeated

Statements and expressions

Arrows indicate evaluation order

Frames (right):Code (left):

Environment diagrams visualize the interpreter’s process.

22

Just executed

Next to execute Assignment statement

http://pythontutor.com/composingprograms.html#code=from%20math%20import%20pi%0Atau%20%3D%202%20*%20pi&cumulative=false&curInstr=1&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 23: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Assignment Statements

Execution rule for assignment statements:

1. Evaluate all expressions to the right of = from left to right.

2. Bind all names to the left of = to those resulting values in the current frame.

23

Just executed

Just executed

Next to execute

http://pythontutor.com/composingprograms.html#code=a%20%3D%201%0Ab%20%3D%202%0Ab,%20a%20%3D%20a%20%2B%20b,%20b&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 24: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Discussion Question 1 Solution

24

func min(...) 4

3

f(2, g(h(1, 5), 3))3

g(h(1, 5), 3)3func max(...) 2

3h(1, 5)

func min(...) 5

5func max(...) 13

(Demo 4)

http://pythontutor.com/composingprograms.html#code=f%20%3D%20min%0Af%20%3D%20max%0Ag,%20h%20%3D%20min,%20max%0Amax%20%3D%20g%0Amax%28f%282,%20g%28h%281,%205%29,%203%29%29,%204%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 25: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Defining Functions

Page 26: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Defining Functions

Assignment is a simple means of abstraction: binds names to values

Function definition is a more powerful means of abstraction: binds names to expressions

<name>(<formal parameters>):

return <return expression>

>>> def

Execution procedure for def statements:

1. Create a function with signature <name>(<formal parameters>)

2. Set the body of that function to be everything indented after the first line

3. Bind <name> to that function in the current frame

Function signature indicates how many arguments a function takes

Function body defines the computation performed when the function is applied

26

Page 27: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

1. Add a local frame, forming a new environment2. Bind the function's formal parameters to its arguments in that frame3. Execute the body of the function in that new environment

Local frame

Original name of function called

Formal parameter bound to argument Return value

(not a binding!)

Built-in function

User-defined function

27http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

(Demo 5)

Page 28: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Calling User-Defined Functions

A function’s signature has all the information needed to create a local frame

28

Procedure for calling/applying user-defined functions (version 1):

1. Add a local frame, forming a new environment2. Bind the function's formal parameters to its arguments in that frame3. Execute the body of the function in that new environment

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 29: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Looking Up Names In Environments

Every expression is evaluated in the context of an environment.

So far, the current environment is either:

• The global frame alone, or

• A local frame, followed by the global frame.

Most important two things I’ll say all day:

An environment is a sequence of frames.

A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found.

E.g., to look up some name in the body of the square function:

• Look for that name in the local frame.

• If not found, look for it in the global frame.(Built-in names like “max” are in the global frame too, but we don’t draw them in environment diagrams.)

(Demo5)29

Page 30: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Print and None

(Demo1)

Page 31: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

None Indicates that Nothing is Returned

The special value None represents nothing in Python

A function that does not explicitly return a value will return None

Careful: None is not displayed by the interpreter as the value of an expression

31

>>> def does_not_return_square(x):

... x * x

...

>>> does_not_return_square(4)

>>> sixteen = does_not_return_square(4)

>>> sixteen + 4

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

The name sixteenis now bound tothe value None

No return

None value is not displayed

Page 32: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

abs

Pure Functions & Non-Pure Functions

-22

-2None

print

Python displays the output “-2”

2, 1001267650600228229401496703205376

pow

Pure Functionsjust return values

Non-Pure Functionshave side effects

Argument

Return value

A side effect isn't a value; it's anything that happens as a consequence of

calling a function

Returns None!

32

2 Arguments

Page 33: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Nested Expressions with Print

Noneprint(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(...):2None

display “2”

print(...):None, NoneNone

display “None None”

print(1)

func print(...) 1

Noneprint(2)

2

None

33

Does not get displayed

func print(...)

Page 34: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Multiple Environments

Page 35: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: squarefunction: func square(x)

Signature

4

16

A new frame is created!

Parameters bound to arguments

Body is executed in that newenvironment

Argument

Return value

Name

35

Page 36: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Multiple Environments in One Diagram!

square(square(3))

square(3)

3

func square(x)

36

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 37: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Multiple Environments in One Diagram!

square(square(3))

square(3)9

3

func square(x)

37

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 38: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Multiple Environments in One Diagram!

An environment is a sequence of frames.

1

2

1

2

1

• The global frame alone

• A local, then the global frame38

square(square(3))

square(3)9

3

func square(x)

func square(x)

81

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 39: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Names Have No Meaning Without Environments

An environment is a sequence of frames.• The global frame alone

• A local, then the global frame39

Every expression is evaluated in the context of an environment.

A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found.

1

2

1

2

1

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 40: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Names Have Different Meanings in Different Environments

40

1

2

1

A call expression and the body of the function being calledare evaluated in different environments

Every expression is evaluated in the context of an environment.

A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found.

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28square%29%3A%0A%20%20%20%20return%20mul%28square,%20square%29%0Asquare%284%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 41: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Miscellaneous Python Features

DivisionMultiple Return Values

Source FilesDoctests

Default Arguments

(Demo2)

Page 42: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Conditional Statements

Page 43: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

<header>:<statement><statement>...

<separating header>:<statement><statement>...

...

Compound statements:

Statements

Statement

Suite

ClauseThe first header determines a statement’s type

The header of a clause “controls” the suite that follows

def statements are compound statements

43

A statement is executed by the interpreter to perform an action

Page 44: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Compound Statements

Compound statements:

<header>:<statement><statement>...

<separating header>:<statement><statement>...

...

Execution Rule for a sequence of statements:

• Execute the first statement

• Unless directed otherwise, execute the rest

Suite

A suite is a sequence of statements

To “execute” a suite means to execute its sequence of statements, in order

44

Page 45: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Conditional Statements

1 statement,3 clauses,3 headers,3 suites

Each clause is considered in order.

1. Evaluate the header's expression.

2. If it is a true value, execute the suite & skip the remaining clauses.

45

Syntax Tips:

1. Always starts with "if" clause.

2. Zero or more "elif" clauses.

3. Zero or one "else" clause,always at the end.

(Demo3)

def my_abs(x):"""Return the absolute value of x."""if x < 0:

return -xelif x == 0:

return 0else:

return x

Execution Rule for Conditional Statements:

Page 46: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

def my_abs(x):"""Return the absolute value of x."""if x < 0:

return -xelif x == 0:

return 0else:

return x

Boolean Contexts

False values in Python: False, 0, '', None

True values in Python: Anything else (True)

(more to come)

George Boole

Read Section 1.5.4!

46

Two boolean contexts

Reading: http://composingprograms.com/pages/15-control.html#conditional-statements

Page 47: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Iteration

Page 48: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

George Boole

Iteration: While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite,then return to step 1.

1 2 31 3 6

48

(Demo4)

Execution Rule for While Statements:

Page 49: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

fib

n

predcurr

k

5

def fib(n):"""Compute the nth Fibonacci number, for N >= 1."""pred, curr = 0, 1 # 0th and 1st Fibonacci numbersk = 1 # curr is the kth Fibonacci numberwhile k < n:

pred, curr = curr, pred + currk = k + 1

return curr

The Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

49

The next Fibonacci number is the sum of the current one and its predecessor

12345

Page 50: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Designing Functions

Page 51: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Describing Functions

A function's domain is the set of all inputs it might possibly take as arguments.

A function's range is the set of output values it might possibly return.

A pure function's behavior is the relationship it creates between input and output.

51

def square(x):"""Return X * X."""

x is a number

square returns a non-negative real number

square returns the square of x

Page 52: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

A Guide to Designing Function… Generalization!

Give each function exactly one job, but make it apply to many related situations

52

Don’t repeat yourself (DRY). Implement a process just once, but execute it many times.

>>> round(1.23, 1)1.2

>>> round(1.23, 0)1

>>> round(1.23, 5)1.23

>>> round(1.23)1

Page 53: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Generalization

Page 54: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

Area:

Finding common structure allows for shared implementation

54

(Demo1)

Page 55: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Higher-Order Functions

Page 56: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Generalizing Over Computational Processes

The common structure among functions may be a computational process, rather than a number.

56

(Demo2)

Page 57: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

hof.py Page 2

return total

def identity(k): return k

def cube(k): return pow(k, 3)

def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

def pi_term(k): return 8 / (k * 4 � 3) / (k * 4 � 1)

# Local function definitions; returning functions

def make_adder(n): """Return a function that takes one argument k and returns k + n.

>>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder

def compose1(f, g): """Return a function that composes f and g.

f, g �� functions of a single argument """ def h(x): return f(g(x)) return h

@maindef run(): interact()

Summation Example

Function of a single argument (not called "term")

A formal parameter that will be bound to a function

The function bound to term gets called here

The cube function is passed as an argument value

0 + 1 + 8 + 27 + 64 + 125

57

Page 58: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Functions as Return Values

(Demo3)

Page 59: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

hof.py Page 2

return total

def identity(k): return k

def cube(k): return pow(k, 3)

def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

def pi_term(k): return 8 / (k * 4 � 3) / (k * 4 � 1)

# Local function definitions; returning functions

def make_adder(n): """Return a function that takes one argument k and returns k + n.

>>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder

def compose1(f, g): """Return a function that composes f and g.

f, g �� functions of a single argument """ def h(x): return f(g(x)) return h

@maindef run(): interact()

Locally Defined Functions

A function that returns a function

A def statement within another def statement

The name add_three is bound to a function

Can refer to names in the enclosing function

Functions defined within other function bodies are bound to names in a local frame

59

Page 60: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

make_adder( n ):

Call Expressions as Operator Expressions

make_adder(1) ( 2 )

Operator Operand

An expression that evaluates to a function

An expression that evaluates to its argument

60

2

3

make_adder(1)func adder(k)

func make_adder(n) 1

func adder(k)

Page 61: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Lambda Expressions

(Demo4)l

Page 62: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Lambda Expressions

>>> x = 10

>>> square = x * x

>>> square = lambda x: x * x

>>> square(4)16

An expression: this one evaluates to a number

Also an expression: evaluates to a function

that returns the value of "x * x"with formal parameter x

A function

Lambda expressions are not common in Python, but important in general

Important: No "return" keyword!

Must be a single expression

62

Lambda expressions in Python cannot contain statements at all!

Page 63: CS 7 : Introduction to Programming and Computer Science ... · welcome. We are a CS7 family! Collaboration •Discuss everything with each other; learn from your fellow students!

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x):return x * xVS

• Both create a function with the same domain, range, and behavior.

• Both bind that function to the name square.

• Only the def statement gives the function an intrinsic name, which shows up in environment diagrams but doesn't affect execution (unless the function is printed).

The Greek letter lambda

63


Recommended