Python Programming/Loops

This lesson introduces loops.

Objectives and Skills
Objectives and skills for this lesson include:
 * Control Flow
 * The while statement
 * The for loop
 * The break and continue statement

Readings

 * 1)  Control flow
 * 2) Python for Everyone: Iteration
 * 3) Python Basics: Iteration, Iterables, Iterators, and Looping

Multimedia

 * 1) YouTube: Python for Informatics: Chapter 5 - Iterations
 * 2) YouTube: Python For
 * 3) YouTube: Python Range and While
 * 4) YouTube: Python Break
 * 5) YouTube: Python Continue
 * 6) YouTube: Python Nested Loops
 * 7) YouTube: Python Iteration Playlist
 * 8) Python Tutorial for Beginners 7: Loops and Iterations - For/While Loops

While Statement
The while loop executes as long as the condition remains true. In Python, any non-zero integer value is true and zero is false. The condition may also be a string or list value, in which case anything with a non-zero length is true and empty sequences are false. The body of the loop must be indented, and each line within a block must be indented by the same amount.

For Statement
The for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

Range Function
The range function returns an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops. The syntax for the range function is, with start defaulting to 0 and step defaulting to 1.

Break Statement
The break statement breaks out of the smallest enclosing for or while loop.

Continue Statement
The continue statement continues with the next iteration of the loop.

Nested Loops
Loops may be nested with one loop inside another.

Output: 11     12      13 21      22      23 31      32      33

11     12      13 21      22      23 31      32      33

Tutorials

 * 1) Complete one or more of the following tutorials:
 * 2) * LearnPython
 * 3) ** Loops
 * 4) * TutorialsPoint
 * 5) ** Loops
 * 6) * Codecademy
 * 7) ** Loops
 * 8) * SoloLearn
 * 9) ** Control Structures
 * 10) * Wikiversity
 * 11) ** Python/While Statement
 * 12) ** Python/For Statement
 * 13) * Wikibooks
 * 14) ** A Beginner's Python Tutorial/Loops, Conditionals

Practice

 * 1) Review MathsIsFun: Definition of Average. Create a Python program that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a total. Finally, calculate and display the average for the entered scores. Include try and except to handle input errors. Revise the program to compare the code required when the loop control structure is based on both while and for statements.
 * 2) Create a Python program that uses a loop to generate a list of multiplication expressions for a given value. Ask the user to enter the value and the number of expressions to be displayed. Include try and except to handle input errors. For example, a list of three expressions for the value 1 would be:
 * 3) Review MathsIsFun: 10x Printable Multiplication Table. Create a Python program that uses nested loops to generate a multiplication table. Rather than simply creating a 10 by 10 table, ask the user to enter the starting and ending values. Include row and column labels, and include try and except to handle input errors. For example, the output might look like:
 * 4) Review MathsIsFun: 10x Printable Multiplication Table. Create a Python program that uses nested loops to generate a multiplication table. Rather than simply creating a 10 by 10 table, ask the user to enter the starting and ending values. Use a condition with ( % 2) to test for odd or even values. Generate the multiplication table only for even values. For odd values, use the continue statement to continue the next iteration of the loop. Include row and column labels, and include try and except to handle input errors. For example, the output might look like:
 * 5) Review MathsIsFun: 10x Printable Multiplication Table. Create a Python program that uses nested loops to generate a multiplication table. Rather than simply creating a 10 by 10 table, ask the user to enter the starting and ending values. Use a break statement to terminate the loop if the number of rows or columns exceeds 10. Include row and column labels, and include try and except to handle input errors.

Games

 * 1) Play CodeCombat.

Control Flow Concepts

 * Control flow refers to the order in which the individual statements, instructions or function calls of an imperative or a declarative program are executed or evaluated.
 * Control flow statement types include unconditional branch, conditional branch, conditional loop, subroutines, and unconditional halt.
 * Python does not support an unconditional branch. All control flow must be structured using conditions, loops. functions, or exit (unconditional halt).
 * Python uses colons and indentation to designate code blocks and control structures.
 * A loop is a sequence of statements which is specified once but which may be carried out several times in succession.

Python Loops

 * Python loop structures include while and for.
 * While is a condition-controlled loop, repeating until some condition changes.
 * Python for loops are collection-controlled loops repeating for all elements of a sequence, which is more like foreach in other programming languages.
 * Loops may be continued prematurely using the continue statement.
 * Loops may be terminated prematurely using the break statement.
 * Programs may be terminated prematurely using the exit function.
 * The while loop executes as long as the condition remains true. In Python, any non-zero integer value is true and zero is false. The condition may also be a string or list value, in which case anything with a non-zero length is true and empty sequences are false.
 * The body of the loop must be indented, and each line within a block must be indented by the same amount.
 * The syntax for the while loop is:
 * The for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.
 * The syntax for the for loop is:
 * The range function returns an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
 * The syntax for the range function is, with start defaulting to 0 and step defaulting to 1.
 * The break statement breaks out of the smallest enclosing for or while loop.
 * The continue statement continues with the next iteration of the loop.

Key Terms

 * accumulator
 * A variable used in a loop to add up or accumulate a result.


 * counter
 * A variable used in a loop to count the number of times something happened. We initialize a counter to zero and then increment the counter each time we want to “count” something.


 * decrement
 * An update that decreases the value of a variable.


 * initialize
 * An assignment that gives an initial value to a variable that will be updated.


 * increment
 * An update that increases the value of a variable (often by one).


 * infinite loop
 * A loop in which the terminating condition is never satisfied or for which there is no terminating condition.


 * iteration
 * Repeated execution of a set of statements using either a function that calls itself or a loop.

Assessments

 * Flashcards: Quizlet: Python Loops
 * Quiz: Quizlet: Python Loops