Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Model 1 for Statements A for loop executes the same block of code for each item in a sequence. Create a new file in Python
Model for Statements
A for loop executes the same block of code "for each item in a sequence". Create a new file in Python named loops.py and enter the following code:
print hello
for x in :
print the number is x
print goodbye
Run the loops.py program. How many times does the indented line of code execute under the for loop?
How many times does the line of code NOT indented execute after the for loop?
Identify the value of x each time the indented line of code is executed.
ast time:
bnd time:
crd time:
Modify the list in the following ways, and rerun the program each time. Indicate how many times the for loop executes.
a nonconsecutive numbers:
b numbers decreasing in value:
c all have the same value:
d single value in a list:
In general, what determines the number of times that the loop repeats?
What determines the value of the variable x Explain your answer in terms of what is assigned x each time the loop runs.
Modify the program as follows:
a Write a statement that assigns to the variable numbers.
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?
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 What determined how many times print c was called?
c Explain what a for statement does with strings.
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 doesnt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started