Model 1 for Statements A for loop executes the same block of code for each item...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
cck3 java help them all If a processor exhibited one branch delay slot how would you reorder (and possibly modify) the instructions in the following loop to gain a performance advantage? loop ldr...
-
Q1. You have identified a market opportunity for home media players that would cater for older members of the population. Many older people have difficulty in understanding the operating principles...
-
Carlberg Company has two manufacturing departments, Assembly and Painting. The Assembly department started 11,000 units during November. The following production activity in both units and costs...
-
Suppose that in Figure 4-4, the government reduces the floor price of milk below the displayed $0.10-per-pound floor price, to $0.08 per pound. Will the excess quantity of milk supplied increase or...
-
make a class named BankCustomer . A bank customer should include four pieces of information as customer_id (type String), customer_id (type String), saving_accounts (type SavingAccount[], an array of...
-
The probability of randomly choosing a cricket player who also played for his school team. (Assume that you are choosing from the population of all cricket players.) Write a statement that represents...
-
A man buys a corporate bond from a bond brokerage house for $925. The bond has a face value of $1000 and pays 4% of its face value each year. If the bond will be paid off at the end of 10 years, what...
-
The popularity of Joe's Deli has caused lines to form throughout the operation and customers to start grumbling about the length of time they must spend to get their shopping completed. Joe's process...
-
Some users on your network use Acme Bank for their personal banking. Those users have all recently been the victim of an attack, in which they visited a fake Acme Bank website and their logins were...
-
What is the relationship between social commerce and e-commerce? How are mobile devices and software applications influencing the development of social commerce?
-
Classfiy the following items into Revenue and Expenses ITEMS REVENUE EXPENSES Sales Salaries Rent Insurance Utilities Cost of goods sold Service revenue Depreciation
-
Grant and Herd are in partnership sharing profits and losses in the ratio 3 to 2. The following information relates to the year to 31 December 2011: 1 The partnership agreement allows for Herd to be...
-
It has been suggested that CSR from an Islamic perspective has a wider focus. Discuss.
-
How are social networks structured?
-
Research: Please provide more information base on the regulation that applied to SCH A when we file Married filing separately MFS. This has to do with couple that would like to file on their own, and...
-
(a) Prove that form an orthonormal basis for R3 for the usual dot product. (b) Find the coordinates of v = (1, 1, 1)T relative to this basis. (c) Verify formula (5.5) in this particular case. 48-65...
-
Charlie's Green Lawn Care is a cash basis taxpayer. Charlie Adame, the sole proprietor, is considering delaying some of his December 2012 customer billings for lawn care into the next year. In...
-
Laura is a single taxpayer living in New Jersey with adjusted gross income for the 2012 tax year of $35,550. Laura's employer withheld $3,300 in state income tax from her salary. In April of 2012,...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-6. On December 12, Irene purchased the building where her store is located. She paid...
-
What are the numbers of observations in each of the five categories that rate the respondents level of job satisfaction?
-
Which of the seven store departments had the most customer visits, and what was this number?
-
What proportion of the 26,280 individuals are in each of the five categories?
Study smarter with the SolutionInn App