Lucas DuBois ModuleSevenLessonFourActivity.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

9

Uploaded by DukeDanger9183

Report
12/11/23, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 1/9 I learned about lists functions I did have trouble with the last part im not sure if I got it Lucas DuBois 12/4/23 .extend(), +, .reverse(), .sort(), reversed() and sorted() for in: for loop using in for range: for range(start,stop,step) More list methods and functions: .extend() , +, .reverse(), reversed(), sorted(), .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 Four Activity View Concatenate List video + list addition Concept: Combine Lists
12/11/23, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 2/9 .extend() list method Combine lists with + and .extend() visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São Paulo", "H wish_cities = ["Reykjavík", "Moscow", "Beijing", "Lamu"] # combine in a new list all_cities = visited_cities + wish_cities # add a list to an existing list visited_cities.extend(wish_cities) Examples # [ ] review and run example visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São wish_cities = ["Reykjavík", "Moscow", "Beijing", "Lamu"] # .extend() # extending visited_cities list (IN PLACE) by concatenating wish_cities visited_cities.extend(wish_cities) print("ALL CITIES",visited_cities) ALL CITIES ['New York', 'Shanghai', 'Munich', 'Toyko', 'Dubai', 'Mexico City', 'São # [ ] review and run example visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São wish_cities = ["Reykjavík", "Moscow", "Beijing", "Lamu"] # (+) Addition operator for lists creates a (NEW) combined List all_cities = visited_cities + wish_cities print("ALL CITIES") for city in all_cities: print(city) ALL CITIES New York Shanghai Munich Toyko Dubai Mexico City São Paulo
12/11/23, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 3/9 Hyderabad Reykjavík Moscow Beijing Lamu # [ ] review and run example team_a = [0,2,2,2,4,4,4,5,6,6,6] team_b = [0,0,0,1,1,2,3,3,3,6,8] print("Team A:", team_a, "\nTeam B:",team_b) # (+) Addition operator team_totals = team_a + team_b print("Team Totals", team_totals) Team A: [0, 2, 2, 2, 4, 4, 4, 5, 6, 6, 6] Team B: [0, 0, 0, 1, 1, 2, 3, 3, 3, 6, 8] Team Totals [0, 2, 2, 2, 4, 4, 4, 5, 6, 6, 6, 0, 0, 0, 1, 1, 2, 3, 3, 3, 6, 8] # [ ] review and run example after running cell above # .extend() team_a.extend(team_b) print("Team_a extended", team_a) # what happens if you keep running this cell? Team_a extended [0, 2, 2, 2, 4, 4, 4, 5, 6, 6, 6, 0, 0, 0, 1, 1, 2, 3, 3, 3, 6, 8] extend the list common_birds with list birds_seen which you must create Task 1: Combine lists common_birds = ["chicken", "blue jay", "crow", "pigeon"] #The lists are added together birds_seen = ["blue jay", "crow"] common_birds.append(birds_seen) print(common_birds) ['chicken', 'blue jay', 'crow', 'pigeon', ['blue jay', 'crow']] Create 2 lists zero_nine and ten_onehundred that contain 1-9, and 10 - 100 by 10's. use list addition to concatenate the lists into all_num and print Task 2: Combine lists
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, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 4/9 zero_nine = [1, 2, 3, 4, 5, 6, 7, 8, 9] ten_onehundred = [10, 20 , 30, 40 ,50, 60, 70, 80, 90, 100] #The list becomes one all_num = [] all_num.append(zero_nine) all_num.append(ten_onehundred) print(all_num) [[1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]] .reverse() View Reverse Lists video cities_1 = ["Dubai", "Mexico City", "São Paulo", "Hyderabad"] print("regular", cities_1) cities_1.reverse() print("reversed", cities_1) You may use .reverse() for the purposes of this activity, but if you want to explore using reversed(), here is an important note: The reversed() function allows us to process the items in a sequence in reverse order. It accepts a sequence and returns an iterator. As you usually are interested in reversing the list and having it be a list, you need to cast it. Example: reversed(my_list) would return a message like: list_reverseiterator object at 0x7f457d8bd5f8 list(reversed(my_list)) would return my_list in reverse order. Concept: Reverse a list in place Examples # [ ] review and run example cities_1 = ["Dubai", "Mexico City", "São Paulo", "Hyderabad"] print("regular", cities_1) cities_1.reverse() print("reversed", cities_1)
12/11/23, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 5/9 # [ ] review and run example all_num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] print("regular list",all_num, "\n") all_num.reverse() print("reverse list",all_num, "\n") num_len = len(all_num) print("Three Multiple") for num in all_num: if num/3 == int(num/3): print(num) else: pass # [ ] review and run example # create a list of numbers by casting a range count_list = list(range(21)) print("before list", count_list) # and reverse count_list.reverse() print("after list", count_list) create and print a list of multiples of 5 from 5 to 100 reverse the list and print Task 3: .reverse() # [ ] create and print a list of multiples of 5 from 5 to 100 # { ] reverse the list and print multiples_list = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, multiples_list.reverse() print(multiples_list) [100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5] Create two lists: fours & more_fours containing multiples of four from 4 to 44 combine and print so that the output is mirrored [44, 40,...8, 4, 4, 8, ...40, 44] Task 4: .reverse()
12/11/23, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 6/9 fours = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40 ,44] more_fours = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40 ,44] #multiples of four and both list fours.reverse() fours.append(more_fours) print(fours) [44, 40, 36, 32, 28, 24, 20, 16, 12, 8, 4, [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44 View Sort and Sorted Lists video .sort() in place .sort() - orders a list in place quiz_scores = [20, 19, 20, 15, 20, 20, 20, 18, 18, 18, 19] quiz_scores.sort() sorted() copy sorted() - creates an ordered list copy game_points = [3, 14, 0, 8, 21, 1, 3, 8] sorted_points = sorted(game_points) Concept: .sort() and sorted() Examples: .sort() and sorted() # [ ] review and run example quiz_scores = [20, 19, 20, 15, 20, 20, 20, 18, 18, 18, 19] # use .sort() quiz_scores.sort() print("quiz_scores:", quiz_scores)
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, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 7/9 # [ ] review and run example game_points = [3, 14, 0, 8, 21, 1, 3, 8] # use sorted() sorted_points = sorted(game_points) print("game_points:", game_points) print("sorted_points:", sorted_points) # [ ] review and run example cities_1 = ["Dubai", "Mexico City", "São Paulo", "Hyderabad"] print("Unsorted", cities_1) cities_1.sort() print("Sorted", cities_1) print cites from visited_cities list in alphbetical order using .sort() only print cities that names start "Q" or earlier Task 5: .sort() & sorted() visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São visited_cities.sort() print(visited_cities) print(visited_cities[0:5]) ['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York', 'Shanghai', 'São Paulo', ['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York'] make a sorted copy (sorted_cities) of visited_cities list remove city names 5 characters or less from sorted_cities print visitied cites and sorted cities Task 5: .sort() & sorted() visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São sorted_cities = sorted(visited_cities) print(sorted_cities) print(sorted_cities[0:5]) ['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York', 'Shanghai', 'São Paulo', ['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York']
12/11/23, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 8/9 Create a program that takes user to build a list: add_animals merges add_animals with existing list: animals provides a sorted list to view in alpha or reverse alpha order Task 6 (program): Merge & Sort Animals step 1 get user input to build add_animals list add_animals = [] #It adds whatever animals you put in th list add_animals = [] user_add = input("enter some animals: ") while user_add != "": add_animals.append(user_add) user_add = input("enter some animals: ") print(add_animals) enter some animals: cow enter some animals: elphant enter some animals: pig enter some animals: bunny enter some animals: ['cow', 'elphant', 'pig', 'bunny'] step 2 Merge the lists: add_animals into animals animals = ["Chimpanzee", "Panther", "Wolf", "Armadillo"] #This merges the lists together animals.extend(add_animals) print(animals) ['Chimpanzee', 'Panther', 'Wolf', 'Armadillo', 'cow', 'elphant', 'pig', 'bunny'] step 3 Allow animals list to be viewed alpha or reverse alpha order step 4 Display both animals.sort() #this reverses the list animals.reverse() print(animals) ['pig', 'elphant', 'cow', 'bunny', 'Wolf', 'Panther', 'Chimpanzee', 'Armadillo']
12/11/23, 12:18 PM Lucas DuBois ModuleSevenLessonFourActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1cMVMWYGhTmTq1GkM6hQYu5LA3uHJPLo4#scrollTo=3w 0DmKs_KBOo&printMode=true 9/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