Question: Model 1 for Statements A for loop executes the same block of code for each item in a sequence. Create a new file named

Model 1 for Statements A for loop executes the same block ofcode "for each item in a sequence". Create a new file named

Model 1 for Statements A for loop executes the same block of code "for each item in a sequence". Create a new file named loops.py, and enter the following code: print("hello") for x in [2, 7, 11: print("the number is", x) print("goodbye") For each item Questions (15 min) 1. Run the loops.py program. How many times does the indented line of code execute under the for loop? Block of instructions 2. How many times does the line of code NOT indented execute after the for loop? 3. Identify the value of x each time the indented line of code is executed. a) 1st time: b) 2nd time: c) 3rd time: Start time: 4. Modify the list [2, 7, 1] in the following ways, and rerun the program each time. Indicate how many times the for loop executes. a) non-consecutive numbers: [5, -7, 01 b) numbers decreasing in value: [3, 2, 1, 0] c) all have the same value: [4, 4] d) single value in a list: [8] 5. In general, what determines the number of times that the loop repeats? 6. What determines the value of the variable x? Explain your answer in terms of what is assigned (x = ...) each time the loop runs. 7. Modify the program as follows: a) Write a statement that assigns [0, 1, 2, 3, 4] to the variable numbers. 8. Add the following code at the end of your program: for c in "Hi!": print (c) a) What is the output of this for statement? b) Rewrite the for x ... statement to use the variable numbers instead. c) Does the assignment need to come before or after the for statement? c) Explain what a for statement does with strings. b) What determined how many times print (c) was called? 9. What other data types (besides lists and strings) can a for loop handle? Experiment by adding examples to your loops.py program. Summarize here what works and what doesn't. Model 2 The range Function The Python range function will generate a list of numbers. The range function can take up to three numbers as arguments. Fill in the table below by typing the code into a Python Shell: Python code Shell output range (5) list (range (5)) x = range (3) print (x) print (list (x)) list (range (5, 10)) list (range (-3, 4)) list (range (4, 10, 2)) for i in range (5): print (i) Questions (15 min) Start time: 10. Explain the difference in output between the first two lines of code (with and without the list function). 11. If the argument of the range function specifies a single number (x): a) What will be the first number listed? b) What will be the last number listed? c) How many numbers will be in the list? d) Use the range function to generate the sequence 0, 1, 2, 3. 12. If the argument of the range function specifies two numbers (x,y): a) What will be the first number listed? b) What will be the last number listed? c) How many numbers will be in the list? d) Use the range function to generate the sequence 1, 2, 3, 4. 13. If the argument of the range function specifies three numbers (x, y, z): a) What will be the first number listed? b) What does the third argument represent? c) How many numbers will be in the list? d) Use the range function to generate the sequence 1, 3, 5,7. 14. In your Editor, make a copy of the Model 1 code. Then modify the for statement so that the number of times the loop executes is determined by a variable named times. a) How did you change the for statement? b) How would you cause the loop to print the values 0 to 5? 15. Consider the two different types of for statements used in Model 1 and Model 2. a) If you wanted to execute a loop 100 times, which type of for statement would you choose and why? b) If you wanted to use each item of an existing list inside the loop, which type of for state- ment would you choose and why? 16. Does the range function work with strings? If so, show an example. If not, show how to print the letters A to Z in a loop.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!