Lucas DuBois ModuleSevenLessonFiveActivity.ipynb - Colaboratory

pdf

School

Smithfield-selma High *

*We aren’t endorsed by this school

Course

10

Subject

Electrical Engineering

Date

Nov 24, 2024

Type

pdf

Pages

6

Uploaded by DukeDanger9183

Report
12/11/23, 12:25 PM Lucas DuBois ModuleSevenLessonFiveActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1BxvNO5-AjHzVPxePS-abvN9AtDNGc4aZ#scrollTo=QzVida0159_q&printMode=true 1/6 I learned about list casting and strings to lists I did not have trouble ψ # Lucas DuBois 12/5/23 # I learned about list casting and strings to lists # I did not have trouble Lucas DuBois 12/5/23 Between Strings and Lists 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() List cast & print("hello", end='') 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() Cast strings to lists / direct multiple print outputs to a single line 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 Five Activity View Split on Breaks video .split() by default, splits a string at spaces (" ") to create a list tip = "Notebooks can be exported as .pdf" tip_words = tip.split() for word in tip_words: print(word) Concept: Converting a string to a list with .split() Examples
12/11/23, 12:25 PM Lucas DuBois ModuleSevenLessonFiveActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1BxvNO5-AjHzVPxePS-abvN9AtDNGc4aZ#scrollTo=QzVida0159_q&printMode=true 2/6 # [ ] review and run example tip = "Notebooks can be exported as .pdf" tip_words = tip.split() print("STRING:", tip) print("LIST:", tip_words, "\n") for word in tip_words: print(word) # [ ] review and run example rhyme = "London bridge is falling down" rhyme_words = rhyme.split() rhyme_words.reverse() for word in rhyme_words: print(word) split the string(rhyme) into a list of words (rhyme_words) print each word on it's own line Task 1: Using .split() rhyme = 'Jack and Jill went up the hill To fetch a pail of water' #puts each word on its own line rhyme_words = rhyme.split() for word in rhyme_words: print(word) Jack and Jill went up the hill To fetch a pail of water split code_tip into a list and print the ±rst and every other word Task 2: Using .split() code_tip = "Python uses spaces for indentation" #Puts brackets around it and picks the specific words code_tip_list = code_tip.split() print(code_tip_list[0:5:2]) ['Python', 'spaces', 'indentation'] View Split on Strings video To split on characters other than " " (space), provide .split() a string argument to use as break points code_tip = "Python-uses-spaces-for-indentation" tip_words = code_tip.split('-') Concept: .split('-') Examples
12/11/23, 12:25 PM Lucas DuBois ModuleSevenLessonFiveActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1BxvNO5-AjHzVPxePS-abvN9AtDNGc4aZ#scrollTo=QzVida0159_q&printMode=true 3/6 `.split('-') : split with an argument # [ ] review and run example code_tip = "Python-uses-spaces-for-indentation" tip_words = code_tip.split('-') print(tip_words) # [ ] review and run example - study the list print output code_tip = "Python uses spaces for indentation" # split on "a" tip_words = code_tip.split('a') print(code_tip) print(tip_words) # [ ] review and run example # triple quotes ''' ''' preserve formatting such as spaces and line breaks big_quote = """Jack and Jill went up the hill To fetch a pail of water Jack fell down and broke his crown And Jill came tumbling after""" # split on line breaks (\n) quote_lines = big_quote.split('\n') print(quote_lines, '\n') # print the list in reverse with index slicing for line in quote_lines[::-1]: print(line) split poem into a list of phrases by splitting on "*" a print each phrase on a new line in title case Task 3: .split() poem = "Write code frequently*Save code frequently*Comment code frequently*Study code frequently*" #Puts each sentence into its own line split_poem = poem.split("*") for word in split_poem: print(word.title()) Write Code Frequently Save Code Frequently Comment Code Frequently Study Code Frequently # .join() View Build Using Join Sequence video .join() is a method applied to a separator string and iterates through its argument tip_words = ['Notebooks', 'can', 'be', 'exported', 'as', '.pdf'] " ".join(tip_words) A space (" ") is the separator that gets injected between the objects in the argument (the list "tip_words"). Concept: Build a string from a list Examples: .join()
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:25 PM Lucas DuBois ModuleSevenLessonFiveActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1BxvNO5-AjHzVPxePS-abvN9AtDNGc4aZ#scrollTo=QzVida0159_q&printMode=true 4/6 # [ ] review and run example tip_words = ['Notebooks', 'can', 'be', 'exported', 'as', '.pdf'] # join tip_words objects with spaces print(" ".join(tip_words)) # [ ] review and run example no_space = "" letters = ["P", "y", "t", "h", "o", "n"] print(no_space.join(letters)) # [ ] review and run example - .join() iterates through sequences dash = "-" space = " " word = "Iteration" ellipises = "..." dash_join = dash.join(word) print(dash_join) print(space.join(word)) print(ellipises.join(word)) .join() letters list objects with an Asterisk: "*" Task 4: .join() letters = ["A", "s", "t", "e", "r", "i", "s", "k"] #Joins the letters with an asterisk join_letters = "*".join(letters) print(join_letters) A*s*t*e*r*i*s*k get user input on what to use to join words (" ", *, -, etc...) - store in variable: separator join pharse_words with the separator and print Task 5 (program): Choose the separator phrase_words = ['Jack', 'and', 'Jill', 'went', 'up', 'the', 'hill', 'To', 'fetch', 'a', 'pail', 'of', 'water'] #Puts dashes in between seperator = input("what do you want to use to join words: ") phrase_words_together = seperator.join(phrase_words) print(phrase_words_together) what do you want to use to join words: - Jack-and-Jill-went-up-the-hill-To-fetch-a-pail-of-water View Useful String Tricks video Cast a string to a list of characters hello_letters = list("Hello") Print to the same line with multiple print statements ( end= ) Or insert any character as an end in print("String", end="+"). print('Hello', end = '') print('world') Concept: More Python string tools Examples
12/11/23, 12:25 PM Lucas DuBois ModuleSevenLessonFiveActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1BxvNO5-AjHzVPxePS-abvN9AtDNGc4aZ#scrollTo=QzVida0159_q&printMode=true 5/6 # [ ] review and run example hello_letters = list("Hello") print(hello_letters) # [ ] review and run example # cast sting to list word_letters = list("concatenates") # .join() concatenates the list # print on same line setting the end character print('~'.join(word_letters)) # [ ] review and run example print("Hello ", end = '') print("world") # [ ] review and run example # This is the default print end print("Hello World!", end="\n") print('still something to learn about print()') # [ ] review and run example # end inserts any valid str character: A-z, 0-9,!,@,*,\n,\t or ''(empty string)... for letter in "Concatenation": print(letter, end='*') use 3 print() statements to output text to one line separate the lines by using "- " (dash space) Hint: use something like: print('The String', end='') Task 6: end=" " con±guration in printing print('Be strong and couragous.', end='- ') #Prints into its own 3 sentences print('For the Lord is with you', end='- ') print('wherever you go.', end='- ') Be strong and couragous.- For the Lord is with you- wherever you go.- create a string (fact) of 20 or more characters and cast to a list (fact_letters) iterate fact_letters, printing each char on one line, except for spaces print a new line Hint: to cast, use something like: Msg_characters = list("Always test your code") Task 7: cast str to list fact = "All humans have two sections of a brain" fact_letters = list("All humans have two sections of a brain") #Puts the sentence into into its own line for letters in fact: if letters == " ": print() else: print(letters, end='') All humans have two sections of a brain Create a 20 digit string, and cast to a list Then add all the digits as integers Task 8 (program): Add the digits
12/11/23, 12:25 PM Lucas DuBois ModuleSevenLessonFiveActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1BxvNO5-AjHzVPxePS-abvN9AtDNGc4aZ#scrollTo=QzVida0159_q&printMode=true 6/6 Print the equation and answer Hint: use cast to sum the digits, and .join() to create the equation (1+2+3+...) str_value = "12345678987654321234" #Adds allthe digits together str_list = list("12345678987654321234") sum = 0 for num in str_list: if num.isdigit(): sum += int(num) else: print("string is not all digits") print("+".join(str_list), "=", sum) 1+2+3+4+5+6+7+8+9+8+7+6+5+4+3+2+1+2+3+4 = 90 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