ModuleSevenLessonThreeActivityTwo.ipynb - Colaboratory

pdf

School

Smithfield-selma High *

*We aren’t endorsed by this school

Course

10

Subject

Computer Science

Date

Nov 24, 2024

Type

pdf

Pages

7

Uploaded by DukeDanger9183

Report
12/11/23, 10:12 AM ModuleSevenLessonThreeActivityTw o.ipynb - Colaboratory https://colab.research.google.com/drive/1Qgoq2jf6jGAkSSyRWyZl4nVDCahXxI6D#scrollTo=cXzW9Nvvw 4Ke&printMode=true 1/7 Range Iteration for in: for loop using in for range: for range(start,stop,step) More list methods: .extend() , +, .reverse(), .sort() Strings to lists, .split() , and list to strings, .join() Student will be able to Iterate through lists using for with in Use for range() in looping operations Use list methods .extend() , +, .reverse(), .sort() Convert between lists and strings using .split() and .join() Before submission : Remember for max credit, you need to add a complete header comment in a code cell at the very top of this ±le and descriptive comments in each code cell below (examples AND tasks) that explains what your code is doing. ALL functions need a formatted docstring – see the bottom of Mod 3 Lesson 1 Activity for the requirements. Module Seven Lesson Three Activity Two View range(stop) video The range( stop ) function creates a sequence Using 1 argument with range( stop ): Default start: 0 stop: stopping integer, does not process stop number for count in range(10): print(count) Same as for count in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: print(count) Concept: range(stop) Examples: Range runs from 0 through the integer before stop # [ ] review and run example for count in range(10): print(count) 0 1 2 3 4 5 6 7 8 9
12/11/23, 10:12 AM ModuleSevenLessonThreeActivityTw o.ipynb - Colaboratory https://colab.research.google.com/drive/1Qgoq2jf6jGAkSSyRWyZl4nVDCahXxI6D#scrollTo=cXzW9Nvvw 4Ke&printMode=true 2/7 # review and run example digits = range(10) print("digits =", list(digits), "\n") for count in digits: print(count) digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 0 1 2 3 4 5 6 7 8 9 # [ ] review and run example sub_total = 0 for item in range(10): sub_total += item print("sub_total:", sub_total) print("Total =", sub_total) sub_total: 0 sub_total: 1 sub_total: 3 sub_total: 6 sub_total: 10 sub_total: 15 sub_total: 21 sub_total: 28 sub_total: 36 sub_total: 45 Total = 45 # [ ] review and run example # print the first half of a spelling list spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"] # find length of 1st half of list (must be int) half_1 = int(len(spell_list)/2) for word in range(half_1): print(spell_list[word]) Tuesday Wednesday February for x = 6, use range(x) to print the numbers 1 through 6 Task 1: range(stop) x = 6 for x in range(1, 7): print(x) 1 2 3 4 5 6 using range(x) multiply the numbers 1 through 7 1x2x3x4x5x6x7 = 5040 Task 2: range(stop)
12/11/23, 10:12 AM ModuleSevenLessonThreeActivityTw o.ipynb - Colaboratory https://colab.research.google.com/drive/1Qgoq2jf6jGAkSSyRWyZl4nVDCahXxI6D#scrollTo=cXzW9Nvvw 4Ke&printMode=true 3/7 total = 1 for x in range(1, 8): total*=x print(total) 5040 Use range(stop) to print the second half of spell_list below - Do not hard code the values. Task 3: range(stop) spell_list = ["Wednesday", "Tuesday", "February", "November", "Annual", "Calendar", "Solstice"] for letter in range(4, 7): print(spell_list[letter]) Annual Calendar Solstice View range(start, stop) video The range( start,stop ) function creates a sequence Using 2 arguments with range( start,stop ): start: starting integer value of a range loop stop: stopping integer (second argument), does not process stop number for count in range(5,10): print(count) Concept: range(start,stop) Examples: Range runs from start integer through the integer before stop # [ ] review and run example for count in range(5,10): print(count) 5 6 7 8 9 # [ ] review and run example sub_total = 0 temp = 0 for item in range(5, 11): temp = sub_total sub_total += item print("sub_total:", temp, "+", item, "=",sub_total) print("Total =", sub_total) sub_total: 0 + 5 = 5 sub_total: 5 + 6 = 11 sub_total: 11 + 7 = 18 sub_total: 18 + 8 = 26 sub_total: 26 + 9 = 35 sub_total: 35 + 10 = 45 Total = 45
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
12/11/23, 10:12 AM ModuleSevenLessonThreeActivityTw o.ipynb - Colaboratory https://colab.research.google.com/drive/1Qgoq2jf6jGAkSSyRWyZl4nVDCahXxI6D#scrollTo=cXzW9Nvvw 4Ke&printMode=true 4/7 # [ ] review and run example spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"] # find length list spell_len = len(spell_list) # find lenght of 1st half (aka - start of 2nd half) half_1 = int(spell_len/2) # print 2nd half list for word in range(half_1,spell_len): print(spell_list[word]) November Annual Calendar Solstice using range(start,stop), .append() the numbers 5 to 15 to the list: ±ve_±fteen print list ±ve_±fteen Task 4: range(start,stop) five_fifteen = [] five_fifteen.append(5) five_fifteen.append(15) print(five_fifteen) [5, 15] using range(start,stop) - print the 3rd, 4th and 5th words in spell_list output should include "February", "November", "Annual" Task 5: range(start,stop) spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"] for word in range(0, 8): print(spell_list[2:5]) break ['February', 'November', 'Annual'] using code, ±nd the index of "Annual" in spell_list using range, print the spell_list including "Annual" to end of list Task 6: range(start,stop) spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"] spell_list.append("Annual") print(spell_list) ['Tuesday', 'Wednesday', 'February', 'November', 'Annual', 'Calendar', 'Solstice', 'Annual'] View range(start, stop, step) video The range( start,stop,step ) function creates a sequence Using 3 arguments with range( start,stop,step ): start: starting integer value of a range loop stop: stopping integer (second argument), does not process stop number step: skip value for each loop Concept: range(start,stop,step)
12/11/23, 10:12 AM ModuleSevenLessonThreeActivityTw o.ipynb - Colaboratory https://colab.research.google.com/drive/1Qgoq2jf6jGAkSSyRWyZl4nVDCahXxI6D#scrollTo=cXzW9Nvvw 4Ke&printMode=true 5/7 for count in range(10,101,10): print(count) Examples: Range runs from start integer, skipping by step , through the largest step integer before reaching stop # [ ] review and run example for count in range(25,101,25): print(count) 25 50 75 100 # [ ] review and run example sub_total = 0 temp = 0 for item in range(25,46,5): temp = sub_total sub_total += item print("sub_total:", temp, "+", item, "=",sub_total) print("Total =", sub_total) sub_total: 0 + 25 = 25 sub_total: 25 + 30 = 55 sub_total: 55 + 35 = 90 sub_total: 90 + 40 = 130 sub_total: 130 + 45 = 175 Total = 175 # [ ] review and run example printing the 1st and then every other word in spell_list spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"] for index in range(0,len(spell_list),2): print(spell_list[index]) Tuesday February Annual Solstice # [ ] review and run example casting range to list odd_list = list(range(1,20,2)) print(odd_list) [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] print numbers 10 to 20 by 2's using range Task 7: range(start,stop,step) for numbers in range(10, 22, 2): print(numbers) 10 12 14 16 18 20 print numbers 20 to 10 using range (need to countdown) Hint: start at 20 Task 8: range(start,stop,step)
12/11/23, 10:12 AM ModuleSevenLessonThreeActivityTw o.ipynb - Colaboratory https://colab.research.google.com/drive/1Qgoq2jf6jGAkSSyRWyZl4nVDCahXxI6D#scrollTo=cXzW9Nvvw 4Ke&printMode=true 6/7 for numbers in range(20, 9, -1): print(numbers) 20 19 18 17 16 15 14 13 12 11 10 print ±rst and every third word in spell_list Task 9: range(start,stop,step) spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"] print(spell_list[0:7:3]) ['Tuesday', 'November', 'Solstice'] Input a word string ( word ) Find the string length of word use range() to iterate through each letter in word (can use to range loops) Save odd and even letters from the word as lists odd_letters : starting at index 0,2,... even_letters : starting at index 1,3,... Print odd and even lists test with the word "complexity" Task 10 (program): List of letters word = input("enter a word") odd_letters = [] even_letters = [] for odd in range(0, len(word), 2): odd_letters.append(word[odd]) for even in range(1, len(word), 2): even_letters.append(word[even]) print(odd_letters) print(even_letters) enter a wordhello ['h', 'l', 'o'] ['e', 'l'] ±x the error printing odd numbers 1 - 9 remember to include in your comments what was wrong and how you ±xed it Task 11: Fix the error for num in range(1,10,2): print(num) 1 3 5 7 9 Terms of use Privacy & cookies © 2017 Microsoft
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
12/11/23, 10:12 AM ModuleSevenLessonThreeActivityTw o.ipynb - Colaboratory https://colab.research.google.com/drive/1Qgoq2jf6jGAkSSyRWyZl4nVDCahXxI6D#scrollTo=cXzW9Nvvw 4Ke&printMode=true 7/7