Skip to main content
Chemistry LibreTexts

2.8: Looping Structures: FOR Loops

  • Page ID
    206266
  • \( \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: s20iostpy08ualr
    Download Assignment: S20py08

     

    Learning Objectives

    Students will be able to:

    Content:

    • Explain the difference between while loop and a FOR loop
    • Explain the syntax of a FOR loop
    • Explain how to use the range() function in a FOR loop
    • Explain an accumulator in a FOR loop

    Process:

    • Write code that includes FOR loop
    • Write code that uses use FOR loops within functions

    Prior Knowledge

    • Python and Raspberry Pi concepts from all previous activities

    Further reading

    For Loops

    Critical Thinking Questions: 

    1.   Enter the following two Python programs in your Thonny IDE. Make sure your variable window is shown (under View put a check next to Variables). Use the “Debug Current Script” tool under run to “Step into” the program. Press F7 on your keyboard to step through. Be sure to watch the variable values while running these programs

     

    Python Program 1 Python Program 2
    name= input("Enter your name: ")
    x = 0
    while(x < 20):
        print(name)
        x = x + 1

     

    name= input("Enter your name: ")
    for x in range(20):
        print(name)

     

       a. What is the output for each program?

       b. Both programs produce the same output.  Which code fragment is more concise?

    FYI:  The Python predefined range() function is used to define a series of numbers and can be used in a FOR loop to determine the number of times the loop is executed..

    2. Enter and execute the following code fragments in the Thonny editor window and state the output:

    for x in range(5):
        print(x, end=" ")
    for x in range(1,5):
        print(x, end=" ")
    for x in range(3,20,2):
        print(x, end=" ")	
    numIterations = 6
    for x in range(numIterations):
        print(x, end=" ")			
    
    numIterations = 6	
    for x in range(1, numIterations+1):
        print(x, end=" ")

    3.   After examining the five code fragments in #2, explain how the range() function works. Include an explanation of the arguments.

    Note

     In a FOR loop you can include a list of values in place of the range() function.

    4.  Enter and execute the following code.  

    for x in [3,6,9,12,15,18]:
        print(x, end=" ")

       a.  Rewrite the code using the range() function.

       b.  Why would you use the range() function when you could just list numbers?

     

    5. As you learned in Python Activity 7, every loop structure requires three actions. Explain how these actions are implemented in the following Python FOR loop. Use the Thonny debug feature if you need assistance in answering the questions.

    for x in range(1,5):
        print(x, end=" ")

       a.  Initialize a variable used in the test condition:

       b.  Include a test condition that causes the loop to end when the condition is false:

       c.  Within the loop body, update the variable uses in the test condition:

    Note

    The type() function indicates the class of data stored in a variable.
    The str() function converts what is the parentheses ( ) to a String


    6.  Read through the code and determine what it does

    favorite = input("Enter your favorite ice cream flavor: ")
    for x in range(1,5):
        print(str(x) + ".", favorite, end="\t")

       a.  Explain what you think the program does.

       b.  Enter and execute the code to determine if you were correct. What does the program actually do? Provide a detailed explanation.

       c.  Explain the use of the str() function in the print statement.  Why is it needed?
     

    7.  Complete the arguments in the following range function so that the code prints the even numbers between 100 and 200 inclusive.  

    for x in range():
        print(x)

     

    8.   Complete the arguments in the following range function so that the code prints:  5  4  3  2  1  0.

    for x in range():
        print(x)
    FYI:  An accumulator is a variable that stores the sum of a group of values.

    9.   Examine the following code segment.

    total = 0
    for x in range(5):
        number = int(input("Enter a number: "))
        total += number
    print("The total is:",total)

       a.  Why is the variable total initialized to 0 in the first line of code?

       b. Explain what the following code does.

    number = int(input("Enter a number: "))

       c. Explain what the following code does.

    total += number

       d. How many numbers does the program prompt for?

       e.  What is the accumulator in the code segment?

     

    10.  Is it better to use a FOR loop when you know the number of times the loop should be executed or when you do not know?  Explain your answer.


    Application Question: Use the Python Interpreter to check your work

    1. Write a code segment using a FOR loop that prints multiples of 5 from 5 to 500.


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

    • Was this article helpful?