Introduction to Computing Using Python for loop defining

  • Slides: 12
Download presentation
Introduction to Computing Using Python for loop / def-ining a new function § §

Introduction to Computing Using Python for loop / def-ining a new function § § § Execution control structures (if, for, function call) for loop def-ining a new function Passing and using function parameters return-ing a value from a function

Introduction to Computing Using Python Execution control structures • Execution control (flow control) structures

Introduction to Computing Using Python Execution control structures • Execution control (flow control) structures are statements that control which statements in a program are executed and in what order • The if/else statement is a conditional structure that specifies whether blocks of code are executed, depending on the value of one or more boolean expressions • The for loop statement is an repetition (iteration) structure that executes a block of code for every item of a sequence • A function call temporarily ‘detours’ the sequence of execution of statements to the named function. When the function exits, execution returns to the point where the function call occurred. • We are going to visualize the sequence of execution using pythontutor. org

Introduction to Computing Using Python for loop Executes a block of code for every

Introduction to Computing Using Python for loop Executes a block of code for every item of a sequence • If the sequence is a string, the items are its characters (single-character strings) >>> name = 'Apple' >>> for char in name: print(char) name = char = char = 'A p p l e' A p p l e 'A' 'p' 'l' 'e'

Introduction to Computing Using Python for loop Executes a code block for every item

Introduction to Computing Using Python for loop Executes a code block for every item of a sequence • The sequence may be a range, a string, a list, … • Block of code must be indented word = 'stop' for <variable> in <sequence>: <indented code block > <non-indented code block> for word in ['stop', 'desktop', 'post', 'top']: if 'top' in word: print(word) print('Done. ') 'desktop' 'post' 'top' >>> stop desktop Done.

Introduction to Computing Using Python Built-in function range() The built-in function range() generates a

Introduction to Computing Using Python Built-in function range() The built-in function range() generates a sequence of integers in a specified range. A common use is to execute a block of code a specified number of times. • To iterate over the n numbers 0, 1, 2, …, n-1 for i in range(n): • To iterate over the n numbers i, i+1, i+2, …, n-1 for i in range(i, n): • To iterate over the n numbers i, i+c, i+2 c, i+3 c, …, n-1 for i in range(i, n, c): >>> for i in range(2, range(4): 16, range(0): range(1): range(0, 3): 10): 2): 6): 4): print(i) >>> 2 0 1 >>> 3 6 4 12 2 4 10 8 >>> 3 5 14 12 >>>

Introduction to Computing Using Python Exercise Write for loops that will print the following

Introduction to Computing Using Python Exercise Write for loops that will print the following sequences: 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10 b) 1, 2, 3, 4, 5, 6, 7, 8, 9 c) 0, 2, 4, 6, 8 d) 1, 3, 5, 7, 9 e) 20, 30, 40, 50, 60 a)

Introduction to Computing Using Python Defining a new function We have seen a few

Introduction to Computing Using Python Defining a new function We have seen a few built-in functions: • abs(), max(), len(), sum(), print() You can define a new function using def: function definition keyword i. Squared. Plus 10: name of function x: variable name for input argument def i. Squared. Plus 10(x): result = x**2 + 10 return result >>> 9 >>> 4 >>> 14 >>> abs(-9) max(2, 4) lst = [2, 3, 4, 5] len(lst) sum(lst) print() >>> def i. Squared. Plus 10(x): result = x**2 + 10 return result >>> f(1) 11 >>> f(3) 19 >>> f(0) 10 return: specifies the value that the function returns (default: None)

Introduction to Computing Using Python print() versus return def i. Squared. Plus 10(x): result

Introduction to Computing Using Python print() versus return def i. Squared. Plus 10(x): result = x**2 + 10 return res def i. Squared. Plus 10 (x): result = x**2 + 10 print(res) >>> f(2) i. Squared. Plus 10(2) 14 >>> 2*i. Squared. Plus 10(2) 28 >>> f(2) i. Squared. Plus 10(2) 14 >>> 2*i. Squared. Plus 10(2) 14 Traceback (most recent call last): File "<pyshell#56>", line 1, in <module> 2*i. Squared. Plus 10(2) Type. Error: unsupported operand type(s) for *: 'int' and 'None. Type' Function returns value of res which can then be used in an expression Function prints value of res but does not return anything

Introduction to Computing Using Python Defining a new function The general format of a

Introduction to Computing Using Python Defining a new function The general format of a function definition is def <function name> (<0 or more variables>): <indented function body> c a b Let’s develop function hyp() that: • Takes two numbers as input (side lengths a and b of above right triangle ) • Returns the length of the hypotenuse c >>> hyp(3, 4) 5. 0 >>> import math def hyp(a, b): res = math. sqrt(a**2 result = math. sqrt(a**2 + b**2) return result

Introduction to Computing Using Python Exercise Write function hello() that: • takes a name

Introduction to Computing Using Python Exercise Write function hello() that: • takes a name (i. e. , a string) as input • prints a personalized welcome message Note that the function does not return anything >>> hello('Julie') Welcome, Julie, to the world of Python. >>> def hello(name): line = 'Welcome, ' + name + ', to the world of Python. ' Python. ’ print(line)

Introduction to Computing Using Python Exercise Write function odd. Count() that: • takes a

Introduction to Computing Using Python Exercise Write function odd. Count() that: • takes a list of numbers as input • returns the number of odd numbers in the list >>> odd. Count([4, 0, 1, -2]) 1 >>> def odd. Count(lst): count = 0 def rng(lst): for i in lst: res = max(lst) - min(lst) if i%2 == 1: count += 1 # same as count = count+1 return count

Introduction to Computing Using Python Comments and docstrings Python programs should be documented •

Introduction to Computing Using Python Comments and docstrings Python programs should be documented • So the program’s ‘mission’ is well defined • So the developer who writes/maintains the code understands it • So the user knows what the program does Comments def i. Squared. Plus 10(x): result = x**2 + 10 return res >>> help(i. Squared. Plus 10) Help on function i. Squared. Plus 10 in module __main__: i. Squared. Plus 10(x) # compute result # and return it Docstring def f(x): 'returns x**2 + 10' res = x**2 + 10 # compute result return res # and return it >>> def i. Squared. Plus 10 (x): 'returns x**2 + 10' result = x**2 + 10 return result >>> help(i. Squared. Plus 10) Help on function i. Squared. Plus 10 in module __main__: i. Squared. Plus 10(x) returns x**2 + 10