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)
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
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
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 7/16 In [12]: import math # package that contains cosine print ( " Cosines " ) print ( " x " , "|" , " cos(x) " ) print ( "--------|--------" ) for i in range ( 0 , 100 , 1 ): x = float (i) * 0.001 print ( "%.3f" % x, " |" , " %.4f " % math.cos(x)) # note the format cod
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 8/16 Cosines x | cos(x) --------|-------- 0.000 | 1.0000 0.001 | 1.0000 0.002 | 1.0000 0.003 | 1.0000 0.004 | 1.0000 0.005 | 1.0000 0.006 | 1.0000 0.007 | 1.0000 0.008 | 1.0000 0.009 | 1.0000 0.010 | 1.0000 0.011 | 0.9999 0.012 | 0.9999 0.013 | 0.9999 0.014 | 0.9999 0.015 | 0.9999 0.016 | 0.9999 0.017 | 0.9999 0.018 | 0.9998 0.019 | 0.9998 0.020 | 0.9998 0.021 | 0.9998 0.022 | 0.9998 0.023 | 0.9997 0.024 | 0.9997 0.025 | 0.9997 0.026 | 0.9997 0.027 | 0.9996 0.028 | 0.9996 0.029 | 0.9996 0.030 | 0.9996 0.031 | 0.9995 0.032 | 0.9995 0.033 | 0.9995 0.034 | 0.9994 0.035 | 0.9994 0.036 | 0.9994 0.037 | 0.9993 0.038 | 0.9993 0.039 | 0.9992 0.040 | 0.9992 0.041 | 0.9992 0.042 | 0.9991 0.043 | 0.9991 0.044 | 0.9990 0.045 | 0.9990 0.046 | 0.9989 0.047 | 0.9989 0.048 | 0.9988 0.049 | 0.9988 0.050 | 0.9988 0.051 | 0.9987 0.052 | 0.9986 0.053 | 0.9986
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 9/16 Example: Getting the hang of it! Write a Python script that takes a real input value (a float) for x and returns the y value according to the rules below 0.054 | 0.9985 0.055 | 0.9985 0.056 | 0.9984 0.057 | 0.9984 0.058 | 0.9983 0.059 | 0.9983 0.060 | 0.9982 0.061 | 0.9981 0.062 | 0.9981 0.063 | 0.9980 0.064 | 0.9980 0.065 | 0.9979 0.066 | 0.9978 0.067 | 0.9978 0.068 | 0.9977 0.069 | 0.9976 0.070 | 0.9976 0.071 | 0.9975 0.072 | 0.9974 0.073 | 0.9973 0.074 | 0.9973 0.075 | 0.9972 0.076 | 0.9971 0.077 | 0.9970 0.078 | 0.9970 0.079 | 0.9969 0.080 | 0.9968 0.081 | 0.9967 0.082 | 0.9966 0.083 | 0.9966 0.084 | 0.9965 0.085 | 0.9964 0.086 | 0.9963 0.087 | 0.9962 0.088 | 0.9961 0.089 | 0.9960 0.090 | 0.9960 0.091 | 0.9959 0.092 | 0.9958 0.093 | 0.9957 0.094 | 0.9956 0.095 | 0.9955 0.096 | 0.9954 0.097 | 0.9953 0.098 | 0.9952 0.099 | 0.9951
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
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 10/16 Test the script with x values of 0.0, 1.0, 1.1, and 2.1. add functionality to automaticaly populate the table below: x y(x) 0.0 1.0 2.0 3.0 4.0 In [13]: Enter enter a float2.5 x: 2.5 y is equal to 4.5 userInput = input ( 'Enter enter a float' ) #ask for user's input x = float (userInput) print ( "x:" , x) if x >= 0 and x < 1 : y = x print ( "y is equal to" ,y) elif x >= 1 and x < 2 : y = x * x print ( "y is equal to" ,y) else : y = x + 2 print ( "y is equal to" ,y)
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 11/16 In [14]: ---x--- | ---y--- --------|-------- 0 | 0 1 | 1 2 | 4 3 | 5 4 | 6 5 | 7 # without pretty table print ( "---x---" , "|" , "---y---" ) print ( "--------|--------" ) for x in range ( 0 , 6 , 1 ): if x >= 0 and x < 1 : y = x print ( "%4.f" % x, " |" , " %4.f " % y) elif x >= 1 and x < 2 : y = x * x print ( "%4.f" % x, " |" , " %4.f " % y) else : y = x + 2 print ( "%4.f" % x, " |" , " %4.f " % y)
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 12/16 In [15]: The continue statement The continue instruction skips the block of code after it is executed for that iteration. It is best illustrated by an example. ------------------------------------------------------------------------- -- ModuleNotFoundError Traceback (most recent call las t) Cell In[15], line 3 1 # with pretty table ----> 3 from prettytable import PrettyTable #Required to create tables 5 t = PrettyTable([ 'x' , 'y' ]) #Define an empty table 8 for x in range ( 0 , 6 , 1 ): ModuleNotFoundError : No module named 'prettytable' # with pretty table from prettytable import PrettyTable #Required to create tables t = PrettyTable([ 'x' , 'y' ]) #Define an empty table for x in range ( 0 , 6 , 1 ): if x >= 0 and x < 1 : y = x print ( "for x equal to" , x, ", y is equal to" ,y) t.add_row([x, y]) #will add a row to the table "t" elif x >= 1 and x < 2 : y = x * x print ( "for x equal to" , x, ", y is equal to" ,y) t.add_row([x, y]) else : y = x + 2 print ( "for x equal to" , x, ", y is equal to" ,y) t.add_row([x, y]) print (t)
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
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 13/16 In [16]: EXERCISE Print your name in 3 rows and columns by using nested for loops In [17]: i = 0 , j = 2 this message will be skipped over if j = 6 i = 1 , j = 4 this message will be skipped over if j = 6 i = 2 , j = 6 i = 3 , j = 8 this message will be skipped over if j = 6 i = 4 , j = 10 this message will be skipped over if j = 6 Juan Juan Juan Juan Juan Juan Juan Juan Juan j = 0 for i in range ( 0 , 5 , 1 ): j += 2 print ( "\n i = " , i , ", j = " , j) #here the \n is a newline command if j == 6 : continue print ( " this message will be skipped over if j = 6 " ) # still within th #When j ==6 the line after the continue keyword is not printed. #Other than that one difference the rest of the script runs normally. #Write your code here name = "Juan" for i in range ( 3 ): for j in range ( 3 ): print (name, end = "\t" ) print ()
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 14/16 Here are some great reads on this topic: "Python for Loop" available at * https://www.programiz.com/python-programming/for-loop/ (https://www.programiz.com/python-programming/for-loop/) "Python "for" Loops (Definite Iteration)" by John Sturtz available at * https://realpython.com/python-for-loop/ (https://realpython.com/python-for-loop/) "Python "while" Loops (Indefinite Iteration)" by John Sturtz available at * https://realpython.com/python-while-loop/ (https://realpython.com/python-while-loop/) "loops in python" available at * https://www.geeksforgeeks.org/loops-in-python/ (https://www.geeksforgeeks.org/loops-in-python/) "Python Exceptions: An Introduction" by Said van de Klundert available at * https://realpython.com/python-exceptions/ (https://realpython.com/python-exceptions/) Here are some great videos on these topics: "Python For Loops - Python Tutorial for Absolute Beginners" by Programming with Mosh available at * https://www.youtube.com/watch?v=94UHCEmprCY (https://www.youtube.com/watch?v=94UHCEmprCY) "Python Tutorial for Beginners 7: Loops and Iterations - For/While Loops" by Corey S h f il bl t * htt // t b / t h? 6iF8Xb7Z3 Q
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 15/16 Exercise: FOR or WHILE? 1000 people have asked to be enlisted to take the first dose of COVID- 19 vaccine. You are asked to write a loop and allow the ones who meet the requirements to take the shot. What kind of loop will you use? a FOR loop or a WHILE loop? Explain the logic behind your choice briefly. * Make sure to cite any resources that you may use.
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
9/29/23, 12:02 PM Lab9 (1) - Jupyter Notebook localhost:8888/notebooks/Lab9 (1).ipynb# 16/16 In [18]: Cell In[18], line 2 based on their eligibility, it's more practical to use a WHILE loop. This is because a WHILE loop allows us to ^ SyntaxError: unterminated string literal (detected at line 2) When deciding between a FOR loop and a WHILE loop for the task of administe based on their eligibility, it 's more practical to use a WHILE loop. This i continuously check each person 's eligibility and administer the vaccine un individuals.