while Loops Repetition Hans-Joachim Böckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming Functions and return values Autumn 2019 – October 24, 2019 while Loops while Loops while condition : while condition : statement statement Indentation condition is evaluated statement: True : iteration starts arbitrary statement statement is executed body of the while loop False : while loop ends condition: Boolean expression Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 1 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 2 / 31
while Loops Incrementation of Variables s = 0 Use simplified syntax for changing values of variables i = 1 while i <= 2: n = n + 1 is written as n += 1 s = s + i n = n + i is written as n += i i = i + 1 n = n - 15 is written as n -= 15 n = n * j is written as n *= j i condition s n = n ** 4 is written as n **= 4 i = 1 true s = 1 . . . i = 2 true s = 3 i = 3 false s = 3 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 3 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 4 / 31 The Jump Statements break break Immediately leave the enclosing loop Useful in order to be able to break a loop “in the middle” Control Structures Termination s = 0 while True: x = int(input("Enter a positive number, abort with 0: ")) if x == 0: break s += x print(s) Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 5 / 31
Termination Infinite Loops Infinite loops are easy to generate i = 1 while True: while i <= n: print("0") s += i i += 1 while not False: print("1") Here and commonly while 2 > 1: statement changes its value that appears in condition print("2") After a finite number of iterations condition becomes false ➯ Termination . . . but can in general not be automatically detected Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 6 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 7 / 31 Halting Problem The Collatz Sequence Undecidability of the Halting Problem [Alan Turing, 1936] Sequence of natural numbers n 0 , n 1 , n 2 , n 3 , n 4 , n 5 , . . . There is no Python program that can determine, for n 0 = n each Python program P and each input I , whether P n i − 1 / 2 if n i − 1 even terminates with the input I for every i ≥ 1 , n i = 3 · n i − 1 + 1 if n i − 1 odd This means that the termination of programs can in general not be automatically checked Alan Turing [Wikimedia] Example for n = 5 Theoretical questions of this kind were the main motivation for Turing to design 5, 16, 8, 4, 2, 1, 4, 2, 1, . . . (repetition at 1) his computing machine Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 8 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 9 / 31
Exercise – The Collatz Sequence Exercise – The Collatz Sequence Write a program that n = int(input("Compute the Collatz sequence for n = ")) takes an integer n as input while n > 1: # stop when 1 is reached outputs the Collatz sequence using if n % 2 == 0: # n is even n 0 = n and n //= 2 n i − 1 / 2 if n i − 1 even else: # n is odd n i = 3 · n i − 1 + 1 if n i − 1 odd n = 3 * n + 1 print(n, end=" ") Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 10 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 11 / 31 The Collatz Sequence The Collatz Sequence Example for n = 27 The Collatz Concecture [Lothar Collatz, 1937] 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 For every n ≥ 1 , 1 will occur in the sequence 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 Nobody could prove the conjecture so far 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 If it is wrong, then the while loop for computing the 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 Collatz sequence can be an endless loop for some n 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 as input 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 Lothar Collatz [Wikimedia] Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 12 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 13 / 31
Control Flow – if Order of the (repeated) execution of statements generally from top to bottom. . . . . . except in selection and iteration statements Control Structures condition Control Flow true false if condition : statement statement Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 14 / 31 Control Flow – if-else Control Flow – while condition true false statement1 condition if condition : true statement1 while condition : false else: statement statement2 statement statement2 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 15 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 16 / 31
Kontrollfluss break in while -Schleife condition Functions statement break Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 17 / 31 Functions Analogy to Natural Languages So far. . . Python “understands” some specific words One algorithm per file These are called keywords : def , if , while , etc. Statements are processed sequentially Basic stock of functions : print() , range() , input() , etc. Usage of loops and control structures def f(): ⇐ ⇒ Python “learns” new word f Group related code as function def welcome(): From Merriam-Webster dictionary date = "October 24, 2019" print("Hello", username, "!") re · frig · er · a · tor Optional list of parameters print("Welcome to the lecture on", date) A room or appliance for keeping food or other items cool welcome() Definition of a function Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 18 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 19 / 31
Analogy to Natural Languages Analogy to Mathematical Functions def welcome(): f ( x ) = 2 · x + 1 date = "October 24, 2019" print("Hello", username, "!") print("Welcome to the lecture on", date) Functions in Python Parameter x is passed to function username = input("Enter username:") Value is passed back using return if username == "hjb" or username == "dkomm": welcome() date = "October 24, 2019" def f(x): print("Hello", username, "!") y = 2 * x + 1 print("Welcome to the lecture on", date) return y ... else: def f(x): print("Username not found.") return 2 * x + 1 ... return without argument is used to simply end the function call Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 20 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 21 / 31 Analogy to Mathematical Functions Functions with Parameters def checkuser(givenname): def f(x): validnames = [ "ffrei", "jkaufmann", "lfritschi", "skamp", "spiasko" ] return 2 * x + 1 if givenname in validnames: return True else: By using return , the function call represents the corresponding value return False print(f(5)) results in output 11 username = input("Enter username:") z = f(6) assigns z the value 13 if checkuser(username) == True: print("Welcome", username) z = 3 * f(2) + f(4) assigns z the value 24 password = input("Enter your password:") b = (f(10) > 20) assigns b the Boolean value True ... else: print("Username not found.") Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 22 / 31 Digital Medicine I: Introduction to Programming – Functions Autumn 2019 Böckenhauer, Komm 23 / 31
Recommend
More recommend