Lab9%20(9)(1… (9) - JupyterLab

.pdf

School

Texas Tech University *

*We aren’t endorsed by this school

Course

1330

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

13

Uploaded by DoctorMouse2563 on coursehero.com

9/21/23, 3:21 PM Lab9 (9)(1) localhost:8891/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab9 (9)(1).ipynb 1/13 Laboratory 9: Count Controlled Repetition Full name: Landyn Ramos R#: 11920651 Title of the notebook: Loops, Looops, Loooooops Date: Program flow control (Loops) Controlled repetition In [ ]: # Preamble script block to identify host, user, and kernel import sys ! hostname ! whoami print ( sys . executable ) print ( sys . version ) print ( sys . version_info )
9/21/23, 3:21 PM Lab9 (9)(1) localhost:8891/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab9 (9)(1).ipynb 2/13 Structured FOR Loop Structured WHILE Loop Count controlled repetition Count-controlled repetition is also called definite repetition because the number of repetitions is known before the loop begins executing. When we do not know in advance the number of times we want to execute a statement, we cannot use count-controlled repetition. In such an instance, we would use sentinel-controlled repetition. A count-controlled repetition will exit after running a certain number of times. The count is kept in a variable called an index or counter. When the index reaches a certain value (the loop bound) the loop will end. Count-controlled repetition requires control variable (or loop counter) initial value of the control variable increment (or decrement) by which the control variable is modified each iteration through the loop condition that tests for the final value of the control variable We can use both for and while loops, for count controlled repetition, but the for loop in combination with the range() function is more common.
9/21/23, 3:21 PM Lab9 (9)(1) localhost:8891/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab9 (9)(1).ipynb 3/13 Structured FOR loop We have seen the for loop already, but we will formally introduce it here. The for loop executes a block of code repeatedly until the condition in the for statement is no longer true. Looping through an iterable An iterable is anything that can be looped over - typically a list, string, or tuple. The syntax for looping through an iterable is illustrated by an example. First a generic syntax for a in iterable: print(a) Notice the colon : and the indentation. Now a specific example: Example: A Loop to Begin With! Make a list with "Walter", "Jesse", "Gus, "Hank". Then, write a loop that prints all the elements of your lisk. Walter Jesse Gus Hank The range() function to create an iterable The range(begin,end,increment) function will create an iterable starting at a value of begin, in steps defined by increment ( begin += increment ), ending at end . So a generic syntax becomes for a in range(begin,end,increment): print(a) The example that follows is count-controlled repetition (increment skip if greater) In [1]: # set a list BB = [ "Walter" , "Jesse" , "Gus" , "Hank" ] # loop thru the list for AllStrings in BB : print ( AllStrings )
9/21/23, 3:21 PM Lab9 (9)(1) localhost:8891/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab9 (9)(1).ipynb 4/13 Walter Jesse Gus Hank Example: That's odd! Write a loop to print all the odd numbers between 0 and 10. 1 3 5 7 9 Nested Repetition | Loops within Loops In [4]: # set a list BB = [ "Walter" , "Jesse" , "Gus" , "Hank" ] # loop thru the list for i in range ( 0 , 4 , 1 ): # Change the numbers, what happens? print ( BB [ i ]) In [5]: # For loop with range for x in range ( 1 , 10 , 2 ): # a sequence from 2 to 5 with steps of 1 print ( x )
9/21/23, 3:21 PM Lab9 (9)(1) localhost:8891/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab9 (9)(1).ipynb 5/13 Round like a circle in a spiral, like a wheel within a wheel Never ending or beginning on an ever spinning reel Like a snowball down a mountain, or a carnival balloon Like a carousel that's turning running rings around the moon Like a clock whose hands are sweeping past the minutes of its face And the world is like an apple whirling silently in space Like the circles that you find in the windmills of your mind! Windmills of Your Mind lyrics © Sony/ATV Music Publishing LLC, BMG Rights Management Songwriters: Marilyn Bergman / Michel Legrand / Alan Bergman Recommended versions: Neil Diamond | Dusty Springfield | Farhad Mehrad "Like the circles that you find in the windmills of your mind", Nested repetition is when a control structure is placed inside of the body or main part of another control structure. break to exit out of a loop Sometimes you may want to exit the loop when a certain condition different from the counting condition is met. Perhaps you are looping through a list and want to exit when you find the first element in the list that matches some criterion. The break keyword is useful for such an operation. For example run the following program: i = 0 j = 2 i = 1 j = 4 i = 2 j = 6 In the first case, the for loop only executes 3 times before the condition j == 6 is TRUE and the loop is exited. In the second case, j == 7 never happens so the loop completes all its anticipated traverses. In both cases an if statement was used within a for loop. Such "mixed" control structures are quite common (and pretty necessary). A while loop contained within a for loop, with In [6]: # j = 0 for i in range ( 0 , 5 , 1 ): j += 2 print ( "i = " , i , "j = " , j ) if j == 6 : break In [ ]: # One Small Change j = 0 for i in range ( 0 , 5 , 1 ): j += 2 print ( "i = " , i , "j = " , j ) if j == 7 : break
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help