140 likes | 376 Views
Introduction to Computing Using Python. for loop / def - ining a new function. Execution control structures ( if , for , function call) for l oop def -ining a new f unction Passing and using function parameters return- ing a value from a function. Execution control structures.
E N D
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
Execution control structures Introduction to Computing Using Python • Execution control (flow control) structures are statements that control which statements in a program are executed and in what order • The if/elif/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
for loop Introduction to Computing Using Python 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) A p p >>> name = 'Apple' >>> for char in name: print(char) >>> name = 'Apple' >>> for char in name: print(char) >>> name = 'Apple' >>> for char in name: print(char) A p p l e >>> name = 'Apple' >>> for char in name: print(char) A p p l >>> name = 'Apple' >>> for char in name: print(char) A >>> name = 'Apple' >>> for char in name: print(char) A >>> name = 'Apple' >>> for char in name: print(char) A p >>> name = 'Apple' >>> for char in name: print(char) A p >>> name = 'Apple' >>> for char in name: print(char) A p p >>> name = 'Apple' >>> for char in name: print(char) A p p l e name = 'A pple' 'A' char = 'p' char = 'p' char = 'l' char = 'e' char =
for loop Introduction to Computing Using Python 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 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.') 'stop' word = 'desktop' word = >>> stop desktop top >>> stop desktop top Done. >>> stop desktop >>> stop desktop >>> >>> stop 'post' word = 'top' word =
Built-in function range() Introduction to Computing Using Python 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+2c, i+3c, …, n-1 • for i in range(i, n, c): >>> for i in range(2, 3): print(i) 2 >>> >>> for i in range(2, 2): print(i) >>> >>> for i in range(4): print(i) 0 1 2 3 >>> >>> for i in range(0): print(i) >>> >>> for i in range(1): print(i) 0 >>> >>> for i in range(2, 6): print(i) 2 3 4 5 >>> >>> for i in range(2, 16, 4): print(i) 2 6 10 14 >>> >>> for i in range(0, 16, 4): print(i) 0 4 8 12 >>> >>> for i in range(2, 16, 10): print(i) 2 12 >>>
Exercise Introduction to Computing Using Python Write for loops that will print the following sequences: 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9 0, 2, 4, 6, 8 1, 3, 5, 7, 9 20, 30, 40, 50, 60
Defining a new function Introduction to Computing Using Python >>> abs(-9) 9 >>> max(2, 4) 4 >>> lst = [2,3,4,5] >>> len(lst) 4 >>> sum(lst) 14 >>> print() >>> def iSquaredPlus10(x): result = x**2 + 10 return result >>> f(1) 11 >>> f(3) 19 >>> f(0) 10 We have seen a few built-in functions: • abs(), max(), len(), sum(), print() You can define a new function using def def: function definition keyword iSquaredPlus10: name of function x: variable name for input argument defiSquaredPlus10(x): result = x**2 + 10 return result return: specifies the value that the function returns (default: None)
print() versus return Introduction to Computing Using Python def iSquaredPlus10(x): result = x**2 + 10 return res defiSquaredPlus10 (x): result = x**2 + 10 print(res) >>> f(2) 14 >>> iSquaredPlus10(2) 14 >>> 2*iSquaredPlus10(2) 28 >>> f(2) 14 >>> iSquaredPlus10(2) 14 >>> 2*iSquaredPlus10(2) 14 Traceback (most recent call last): File "<pyshell#56>", line 1, in <module> 2*iSquaredPlus10(2) TypeError: unsupported operand type(s) for *: 'int' and 'NoneType' Function returns value of res which can then be used in an expression Function printsvalue of res but does not return anything
Defining a new function Introduction to Computing Using Python c The general format of a function definition is a def <function name> (<0 or more variables>): <indented function body> 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 >>> def hyp(a, b): import math def hyp(a, b): res = math.sqrt(a**2 + b**2) import math def hyp(a, b): result = math.sqrt(a**2 + b**2) return result
Exercise Introduction to Computing Using Python • 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): def hello(name): line = 'Welcome, ' + name + ', to the world of Python.’ def hello(name): line = 'Welcome, ' + name + ', to the world of Python.' print(line)
Exercise Introduction to Computing Using Python • Write function oddCount() that: • takes a list of numbers as input • returns the number of odd numbers in the list >>> oddCount([4, 0, 1, -2]) 1 >>> defoddCount(lst): count = 0 for i in lst: if i%2 == 1: count += 1 # same as count = count+1 return count def rng(lst): def rng(lst): res = max(lst) - min(lst)
Comments and docstrings Introduction to Computing Using Python • 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 >>> help(iSquaredPlus10) Help on function iSquaredPlus10 in module __main__: iSquaredPlus10(x) >>> defiSquaredPlus10 (x): 'returns x**2 + 10' result = x**2 + 10 return result >>> help(iSquaredPlus10) Help on function iSquaredPlus10 in module __main__: iSquaredPlus10(x) returns x**2 + 10 Comments def iSquaredPlus10(x): result = x**2 + 10 # compute result return res # and return it Docstring def f(x): 'returns x**2 + 10' res = x**2 + 10 # compute result return res # and return it