150 likes | 359 Views
Loops. Looping Constructs. A loop is a block of statements that gets executed as long as some condition is satisfied Python loops are expressed as while statements Basic form: while expression: statements
E N D
Looping Constructs • A loop is a block of statements that gets executed as long as some condition is satisfied • Python loops are expressed as whilestatements • Basic form: while expression: statements • If the expression is true, the statements are executed and the expression is evaluated again • As long as the expression is true, the statements are executed repeatedly • Once the expression is false, the statements are skipped, completing the while statement • At that point, execution continues with the statement after the while statement. If the while was the last statement, the script terminates.
Flow Diagram for While Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS NEXT SCRIPT STATEMENT
Looping Constructs • If the expression is false the first time it is evaluated, the statements will never be executed • Python provides for the execution of a second block of statements when the initial expression evaluation is false while expression: statements1 else:statements2
Flow Diagram for While Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS else STATEMENT NEXT SCRIPT STATEMENT
Looping Constructs • There are two statements that are occasionally used in loops • break terminates execution of the while statement and continues at the statement that follows the while • continue interrupts while statement execution and continues back at the expression evaluation; basically skips the rest of the loop body and starts the while again
continue Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS1 CONTINUE STATEMENTS2 NEXT SCRIPT STATEMENT
break Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS1 BREAK STATEMENTS2 NEXT SCRIPT STATEMENT
Simple Loop Examples # file chap04_01.py def echo1(): """Prompt the user for a string, "echo" it, and return it""" line = input('Say something: ') print('You said "', line, '"', sep='') return line def echo():"""Echo the user's input until an empty line is entered"""while echo1(): pass def test():echo()
Simple Loop Examples from chap04_01 import echo1 defpolite_echo():"""Echo the user's input until it equals 'bye'""" while echo1() != 'bye':pass def test():polite_echo()
Simple Loop Examples ### Example 4-3: Recording echo defrecording_echo(): """Echo the user's input until it equals 'bye', then return a list of all the inputs received""" lst = [] entry = echo1() while entry != 'bye': lst.append(entry) entry = echo1() return lst def test(): print(recording_echo())
TEMPLATE deffn():initialize values while test values: use values change values # repeat return result
Commented Recording Echo Function defrecording_echo(): # initialize entry and lstlst = [] # get the first inputentry = echo1() # test entrywhile entry != 'bye': # use entrylst.append(entry) # change entryentry = echo1() # repeat # return resultreturn lst
TEMPLATE: Loop Forever initialize values while True: change values if test values: return use values # repeat return result defrecording_echo_with_conditional(): seq= [] while True: entry = echo1() if entry == 'bye': return seq seq.append(entry)
Example: Loop Forever Using a Generator def translate(rnaseq): """Translate rnaseq into amino acid symbols""" gen = aa_generator(rnaseq) seq = '' aa = next(gen, None) while aa: seq += aa aa = next(gen, None) return seq