Skip to main content
Chemistry LibreTexts

2.10: Looping Structures -- Nested Loops

  • Page ID
    206277
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    hypothes.is tag: s20iostpy10ualr 
    Download Assignment: S20py10

    Learning Objectives

    Students will be able to:

    Content:

    • Read and write nested FOR loops
    • Identify inner and outer loops

    Process:

    • Write code that uses a nested FOR loop

    Prior knwoledge

    • Python concepts from previous activities

     

    Note

     A loop within another loop is known as a nest loop. Proper indentation is essential for the loops to work properluy

    Task 1:

    1. Enter and execute the following code:
    name=input("what is your name: ")
    for x in range(5):
        for x in range(3):
            print(name+" ",end=" ")
        print()
    name=input("what is your name: ")
    for x in range(5):
        for x in range(3):
            print(name+" ",end=" ")
        print()
    1.  What does the program display?
    2.  How many FOR loops are in this code? _______  Is one loop completely executed before the next loop begins?   _______   What do you call this type of loop?_____
    3.  How many times is the following line of code executed in the program?
     print(name+" ",end=" ")
    1. Label the inner loop and the outer loop.
    2. What does the inner loop do?
    3. What does the outer loop do?

    Task 2

    If  you were asked to create a Python program that displayed the adjacent rectangle, you could easily do it with a set of print statements. You can also create it with a FOR loop and a print statement. This exercise will go through the steps to create a program that will print similar output but allows the user to determine the length and width of the figure when they execute the program.

    stars

       a. create a code segment that prompts the user for a number between 1 and 10 and then prints that many asterisks (*) on one line.  Use a FOR loop. Although you should test the user input to be sure it is in range (between 1 & 10), you do not need to do that here.

       b.  You  want the program to create several lines of asterisks.  Extend the code in “a.” to also prompt the user for how many rows to print. Use an “outer” loop to print that many lines of asterisks. Write the revised code below.

       c.  Edit the program so that it prints numbers instead of asterisks. Write the line of code that was changed

    fig 2

    Task 3

    Examine the following code and determine the output. Indicate the changes in memory as the program is executed. Assume the user input is 5. 
     

    height=int(input("Enter height: "))
    for row in range(1, height+1):
        for column in range(row):
            print(row, end=" ")
        print()

     

    table

    Application Question:

    Use the Python Interpreter to check your work. 

    Create a Python program that prompts the user for a number and then prints an inverted right triangle containing that many rows similar to the output below.

    fig 4

     


    This page titled 2.10: Looping Structures -- Nested Loops is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?