Lab9 (1) - Jupyter Notebook

.pdf

School

Texas Tech University *

*We aren’t endorsed by this school

Course

1330

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

16

Uploaded by juanchozulu31

Report
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 1/16 Laboratory 9: Count Controlled Repetition In [6]: Full name: Juan Zuluaga R#: 11830028 Title of the notebook: Loops, Looops, Loooooops Date: 09/28/23 Juans-MacBook-Pro.local juanandreszuluqga /Users/juanandreszuluqga/anaconda3/bin/python 3.11.4 (main, Jul 5 2023, 09:00:44) [Clang 14.0.6 ] sys.version_info(major=3, minor=11, micro=4, releaselevel='final', serial =0) # Preamble script block to identify host, user, and kernel import sys ! hostname ! whoami print (sys.executable) print (sys.version) print (sys.version_info)
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 2/16 Program flow control (Loops) Controlled repetition 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
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 3/16 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. 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. In [7]: 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 Walter Jesse Gus Hank # set a list BB = [ "Walter" , "Jesse" , "Gus" , "Hank" ] # loop thru the list for AllStrings in BB: print (AllStrings)
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 4/16 for a in range(begin,end,increment): print(a) In [8]: Example: That's odd! Write a loop to print all the odd numbers between 0 and 10. In [9]: Walter Jesse Gus Hank 1 3 5 7 9 # 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]) # For loop with range for x in range ( 1 , 10 , 2 ): # a sequence from 2 to 5 with steps of 1 print (x)
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 5/16 Nested Repetition | Loops within Loops 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: In [10]: i = 0 j = 2 i = 1 j = 4 i = 2 j = 6 # j = 0 for i in range ( 0 , 5 , 1 ): j += 2 print ( "i = " ,i, "j = " ,j) if j == 6 : break
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 6/16 In [11]: 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 several if statements would be very common and such a structure is called nested control. There is typically an upper limit to nesting but the limit is pretty large - easily in the hundreds. It depends on the language and the system architecture ; suffice to say it is not a practical limit except possibly for general-domain AI applications. We can also do mundane activities and leverage loops, arithmetic, and format codes to make useful tables like Example: Cosines in the loop! Write a loop to print a table of the cosines of numbers between 0 and 0.01 with steps of 0.001. i = 0 j = 2 i = 1 j = 4 i = 2 j = 6 i = 3 j = 8 i = 4 j = 10 # 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