Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1."— Presentation transcript:

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

2 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 2014 TPOP 2

3 Lilian Blot Overview Branching with Selection Structure  nested if-elif-else Functions and Parameters Void and Non-Void Function Autumn 2014 TPOP 3

4 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 2014 TPOP 4

5 Lilian Blot IF-ELIF-ELSE A Selection Structure Autumn 2014 TPOP 5

6 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 6 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

7 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 7 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

8 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 8 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

9 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 9 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

10 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 10 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

11 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 11 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

12 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 12 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

13 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 13 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

14 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 14 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

15 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 15 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

16 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 16 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

17 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 17 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

18 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 18 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

19 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 A 1 to A n  If condition is False, execute B 1 to B k Autumn 2014 TPOP 19 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

20 Lilian Blot Python Code SchemaPython Code … if condition : Statement A1 … Statement An else : Statement B1 … Statement Bk … Autumn 2014 TPOP 20 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

21 Lilian Blot Python Code SchemaPython Code … if condition : Statement A1 … Statement An else : Statement B1 … Statement Bk … Autumn 2014 TPOP 21 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

22 Lilian Blot Python Code SchemaPython Code … if condition : Statement A1 … Statement An else : Statement B1 … Statement Bk … Autumn 2014 TPOP 22 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

23 Lilian Blot Python Code SchemaPython Code … if condition : Statement A1 … Statement An else : Statement B1 … Statement Bk … Autumn 2014 TPOP 23 Statement B1 … Statement B k Statement A1 … Statement An True False Condition

24 Lilian Blot Simple if-statement SchemaPython Code … if condition : Statement A1 … Statement An statement X statement Y … Autumn 2014 TPOP 24 Statement A1 … Statement An True False Condition Statement X Statement Y … Note the change of indentation

25 Lilian Blot if-elif-else statement SchemaPython Code … if cond_1 : Statement A elif cond_2 : Statement B … elif cond_n : Statement C else : Statement D … Autumn 2014 TPOP 25 Statement A TrueFalse cond_1 cond_2 cond_n Statement B Statement CStatement D True False

26 Lilian Blot Nested if-else statements SchemaPython Code … if cond_1 : Statement A else : Statement B1 Statement B2 if cond_2 : Statement C else : Statement D … Autumn 2014 TPOP 26 Statement A TrueFalse cond_1 cond_2 Statement B1 Statement B2 Statement C Statement D True False

27 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 2014 TPOP 27

28 Lilian Blot Implementation See code snippet 1 Autumn 2014 TPOP 28

29 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 2014 TPOP 29

30 Lilian Blot Void and non-void function Void function doesn’t return anything (meaningful)  help(…) function  None keyword, value to represent the “nothingness”. non-void function returns something other than None  input(…)  value  raw_input(…)  string  len(…)  int Autumn 2014 TPOP 30

31 Lilian Blot Declaring a Function template: def ( ):  : an identifier  : comma-separated identifiers  : any number of indented statements  A non-void function must have a return statement as the last statement of the. Autumn 2014 TPOP 31

32 Lilian Blot Implementation See code snippet 2 Autumn 2014 TPOP 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.

33 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 2014 TPOP 33

34 Lilian Blot Be Careful print x, y never executed In some language this would generate a compile error Autumn 2014 TPOP 34 def f(x): y = 3 * x + 7 return y print x, y Code

35 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 2014 TPOP 35

36 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 2014 TPOP 36

37 Lilian Blot Function Call Autumn 2014 TPOP 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

38 Lilian Blot Todays’ New Keywords Selection  If,  if-else,  if-elif-else, Functions  def,  return,  None. Autumn 2014 TPOP 38

39 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 2014 TPOP 39


Download ppt "Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1."

Similar presentations


Ads by Google