lilian blot core elements selection & functions lecture 3 autumn 2014 tpop 1

39
Lilian Blot Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Upload: annika-northrop

Post on 01-Apr-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian BlotLilian Blot

CORE ELEMENTSSELECTION

&FUNCTIONS

Lecture 3

Autumn 2014TPOP

1

Page 2: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Last Week

What a program is

What a statement is

What an expression is

How to do repetitions/iterations

Introduced the notion of Functions

Autumn 2014TPOP

2

Page 3: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Overview

Branching with Selection Structure nested if-elif-else

Functions and Parameters

Void and Non-Void Function

Autumn 2014TPOP

3

Page 4: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Today’s Problem

We would like to write a program taking a measurement in meters (resp. Feet, inches) as input and convert it to Feet, inches (resp. meters). We need to find a way for the user to tell us which

conversion he/she want to do Depending on user SELECTION we need to do one

operation or the other. We haven’t seen such a structure yet

Autumn 2014TPOP

4

Page 5: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian BlotLilian Blot

IF-ELIF-ELSE

A Selection Structure

Autumn 2014TPOP

5

Page 6: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

6

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 7: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

7

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 8: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

8

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 9: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

9

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 10: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

10

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 11: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

11

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 12: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

12

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 13: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

13

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 14: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

14

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 15: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

15

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 16: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

16

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 17: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

17

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 18: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

18

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 19: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Motivation

Straight line programs: sequence of commands executed one after the other one, and only once. Cannot do many interesting things if

any.

Branching: A program can decide which statements to execute based on a condition. If condition is True, execute A1 to An

If condition is False, execute B1 to Bk

Autumn 2014TPOP

19

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 20: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Python Code

Schema Python Code

……if condition : Statement A1 … Statement Anelse : Statement B1 … Statement Bk…

Autumn 2014TPOP

20

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 21: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Python Code

Schema Python Code

……if condition : Statement A1 … Statement Anelse : Statement B1 … Statement Bk…

Autumn 2014TPOP

21

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 22: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Python Code

Schema Python Code

……if condition : Statement A1 … Statement Anelse : Statement B1 … Statement Bk…

Autumn 2014TPOP

22

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 23: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Python Code

Schema Python Code

……if condition : Statement A1 … Statement Anelse : Statement B1 … Statement Bk…

Autumn 2014TPOP

23

Statement B1…

Statement Bk

Statement A1…

Statement An

True FalseCondition

Page 24: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Simple if-statement

Schema Python Code

……if condition : Statement A1 … Statement An

statement Xstatement Y…

Autumn 2014TPOP

24

Statement A1…

Statement An

True

False

Condition

Statement XStatement Y…

Note the change of indentation

Page 25: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

if-elif-else statement

Schema Python Code

……if cond_1 : Statement Aelif cond_2 : Statement B…elif cond_n : Statement Celse : Statement D…

Autumn 2014TPOP

25

Statement A

True Falsecond_1

cond_2

cond_n

Statement B

Statement C

Statement D

True

True

False

False

Page 26: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Nested if-else statements

Schema Python Code

……if cond_1 : Statement Aelse : Statement B1 Statement B2 if cond_2 : Statement C else : Statement D…

Autumn 2014TPOP

26

Statement A

True Falsecond_1

cond_2

Statement B1

Statement B2

Statement C

Statement D

True False

Page 27: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Why using Functions?

Advantages code reuse facilitate team work modularisation maintainability

monolithic code huge collection of statement

no modularisation no code reuse (cut & paste is not code reuse!) no parallel implementation

Autumn 2014TPOP

27

Page 28: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Implementation

See code snippet 1

Autumn 2014TPOP

28

Page 29: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

What is a Function?

math function f(x) = 2 * x +1

kind of a sub-program function definition when use, we say that the definition is ‘called’ or

‘invoked’

may or may not have argument

may or may not return a value/object

Autumn 2014TPOP

29

Page 30: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Void and non-void function

Void function doesn’t return anything (meaningful) help(…) function None keyword <type ‘NoneType’>, value to represent

the “nothingness”.

non-void function returns something other than None input(…) value raw_input(…) string len(…) int

Autumn 2014TPOP

30

Page 31: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Declaring a Function

template:

def <function_name> (<formal_parameters>): <body>

<function_name>: an identifier <formal_parameters>: comma-separated identifiers <body>: any number of indented statements A non-void function must have a return statement as

the last statement of the <body>.

Autumn 2014TPOP

31

Page 32: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Implementation

See code snippet 2

Autumn 2014TPOP

32

• Key point 1: Separation of concerns

• Key point 2: A function should do one and only one thing (most of the time)

• Key point 3: Decide which information is needed by the function to do its computation… (parameters)

• Key point 4: Does the function need to return zero, one or more values.

Page 33: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

non-void functions

Must have a return statement

Function with no parameter (see head_tail())

Function with one or more parameters (see feet_to_meters(feet, inches) in snippet 3)

Function returning multiple values (see meters_to_feet(number_meters) in snippet 3)

Autumn 2014TPOP

33

Page 34: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Be Careful

print x, y never executed

In some language this would generate a compile error

Autumn 2014TPOP

34

def f(x): y = 3 * x + 7 return y print x, y

Code

Page 35: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Void function

Aimed at changing a state or display information

No return statement

Technically, all function return something

A void function will return the None value even if there is no return statement in the body

Autumn 2014TPOP

35

Page 36: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Function Call

When Python comes to a function call, it initiate a four-step process:

1. the calling program suspends execution at the point of call

2. the formal parameter of the function get assigned the value supplied by the actual parameters in the call

3. the body of the function is executed

4. control returns to the point just after where the function was called

Autumn 2014TPOP

36

Page 37: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Function Call

Autumn 2014TPOP

37

def head_tail():rand = random.random()if(rand < 0.5):

return 'head'else :

return 'tail'

Code

>>> heads = 0>>> tails = 0>>> for i in range(1, 10000):

if (head_tail() == 'head'):heads += 1

else :tails += 1

>>>

Interpreter

Page 38: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Todays’ New Keywords

Selection If, if-else, if-elif-else,

Functions def, return, None.

Autumn 2014TPOP

38

Page 39: Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1

Lilian Blot

Summary

Sometime we need to execute some statements while ignoring others depending on some condition Use the selection structure: if-elif-else

When testing we must ensure that all branches are traversed by the test suit (difficult).

When designing a function, we define what information is needed (parameters) and what should be returned after computation.

Separation of concerns is key for reuse of a function

Autumn 2014TPOP

39