1 / 15

Loops

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

karl
Download Presentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops

  2. 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.

  3. Flow Diagram for While Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS NEXT SCRIPT STATEMENT

  4. 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

  5. Flow Diagram for While Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS else STATEMENT NEXT SCRIPT STATEMENT

  6. 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

  7. continue Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS1 CONTINUE STATEMENTS2 NEXT SCRIPT STATEMENT

  8. break Statement PREVIOUS SCRIPT STATEMENT EXPRESSION STATEMENTS1 BREAK STATEMENTS2 NEXT SCRIPT STATEMENT

  9. 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()

  10. 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()

  11. 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())

  12. TEMPLATE deffn():initialize values while test values: use values change values # repeat return result

  13. 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

  14. 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)

  15. 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

More Related