# coding on demand
def get_middle_word(sentence):
splitted_sentence = sentence.split()
middle = int(len(splitted_sentence)/2)
return splitted_sentence[middle]
user_input = input("Enter full sentence to get middle word:")
print(f"The middle word of the sentence '{user_input}' is: {get_middle_word(user_input)}")
# [X] print out the "physical states of matter" (matter_states) in 4 sentences using list iteration
# each sentence should be of the format: "Solid - is state of matter #1"
matter_states = ['solid', 'liquid', 'gas', 'plasma']
number = 1
for item in matter_states:
print(f"{item.title()} - is state of matter #{number}")
number += 1
# [X] iterate the list (birds) to see any bird names start with "c" and remove that item from the list
# print the birds list before and after removals
birds = ["turkey", "hawk", "chicken", "dove", "crow"]
print(f"birds before = {birds}")
for bird in birds:
if bird.lower().startswith("c"):
birds.remove(bird)
print(f"birds after = {birds}")
# the team makes 1pt, 2pt or 3pt baskets
# [X] print the occurace of each type of basket(1pt, 2pt, 3pt) & total points using the list baskets
baskets = [2,2,2,1,2,1,3,3,1,2,2,2,2,1,3]
ones = 0
twos = 0
threes = 0
total = 0
for item in baskets:
total += item
if item == 1:
ones += 1
elif item == 2:
twos += 1
elif item == 3:
threes += 1
print(f"1pts = {ones}")
print(f"2pts = {twos}")
print(f"3pts = {threes}")
print(f"total = {total}")
# [X] using range() print "hello" 4 times
for number in range(4):
print("hello")
# [X] find spell_list length
# [X] use range() to iterate each half of spell_list
# [X] label & print the first and second halves
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
list_length = len(spell_list)
half_index = int(list_length/2)
print("First half:")
for number in range(half_index):
print(spell_list[number])
print()
print("Second half:")
for number in range(half_index, list_length):
print(spell_list[number])
# [X] build a list of numbers from 20 to 29: twenties
# append each number to twenties list using range(start,stop) iteration
# [X] print twenties
twenties = []
for number in range(20, 30):
twenties.append(number)
print(twenties)
# [X] iterate through the numbers populated in the list twenties and add each number to a variable: total
# [X] print total
total = 0
for number in twenties:
total += number
print(f"total = {total}")
# check your answer above using range(start,stop)
# [X] iterate each number from 20 to 29 using range()
# [X] add each number to a variable (total) to calculate the sum
# should match earlier task
total = 0
for number in range(20, 30):
total += number
print(f"total = {total}")
# [X] create a list of odd numbers (odd_nums) from 1 to 25 using range(start,stop,skip)
# [X] print odd_nums
# hint: odd numbers are 2 digits apart
odd_nums = []
for number in range(1, 26, 2):
odd_nums.append(number)
print(odd_nums)
# [X] create a Decending list of odd numbers (odd_nums) from 25 to 1 using range(start,stop,skip)
# [X] print odd_nums, output should resemble [25, 23, ...]
odd_nums = []
for number in range(25, 0, -2):
odd_nums.append(number)
print(odd_nums)
# the list, elements, contains the names of the first 20 elements in atomic number order
# [X] print the even number elements "2 - Helium, 4 - Beryllium,.." in the list with the atomic number
elements = ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', \
'Neon', 'Sodium', 'Magnesium', 'Aluminum', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon', \
'Potassium', 'Calcium']
for number in range(0, len(elements), 2):
print(f"{number + 2} - {elements[number + 1]}")
# [X] # the list, elements_60, contains the names of the first 60 elements in atomic number order
# [X] print the odd number elements "1 - Hydrogen, 3 - Lithium,.." in the list with the atomic number elements_60
elements_60 = ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', \
'Oxygen', 'Fluorine', 'Neon', 'Sodium', 'Magnesium', 'Aluminum', 'Silicon', \
'Phosphorus', 'Sulfur', 'Chlorine', 'Argon', 'Potassium', 'Calcium', 'Hydrogen', \
'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', \
'Neon', 'Sodium', 'Magnesium', 'Aluminum', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', \
'Argon', 'Potassium', 'Calcium', 'Scandium', 'Titanium', 'Vanadium', 'Chromium', 'Manganese', \
'Iron', 'Cobalt', 'Nickel', 'Copper', 'Zinc', 'Gallium', 'Germanium', 'Arsenic', 'Selenium', \
'Bromine', 'Krypton', 'Rubidium', 'Strontium', 'Yttrium', 'Zirconium']
for number in range(0, len(elements_60), 2):
print(f"{number + 1} - {elements_60[number]}")
# [X] print the combined lists (numbers_1 & numbers_2) using "+" operator
numbers_1 = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
# pythonic casting of a range into a list
numbers_2 = list(range(30,50,2))
print("numbers_1:",numbers_1)
print("numbers_2:",numbers_2)
print("all_numbers:", numbers_1 + numbers_2)
# [X] print the combined element lists (first_row & second_row) using ".extend()" method
first_row = ['Hydrogen', 'Helium']
second_row = ['Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon']
print("1st Row:", first_row)
print("2nd Row:", second_row)
first_row.extend(second_row)
print("All Rows:", first_row)
# [X] create the program: combined 3 element rows
elem_1 = ['Hydrogen', 'Helium']
elem_2 = ['Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon']
elem_3 = ['Sodium', 'Magnesium', 'Aluminum', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon']
print("The 1st three rows of the Period Table of Elements contain:")
print(elem_1 + elem_2 + elem_3)
# [X] .extend() jack_jill with "next_line" string - print the result
jack_jill = ['Jack', 'and', 'Jill', 'went', 'up', 'the', 'hill']
next_line = ['To', 'fetch', 'a', 'pail', 'of', 'water']
jack_jill.extend(next_line)
print(jack_jill)
# [X] use .reverse() to print elements starting with "Calcium", "Chlorine",... in reverse order
elements = ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', \
'Neon', 'Sodium', 'Magnesium', 'Aluminum', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon', \
'Potassium', 'Calcium']
elements.reverse()
for element in elements:
print(element)
# [X] print words in spell_list that 8 or more characters in length in reverse order
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
spell_list.reverse()
for spell in spell_list:
if len(spell) >= 8:
print(spell)
# [X] sort the list element, so names are in alphabetical order and print elements
elements = ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', \
'Neon', 'Sodium', 'Magnesium', 'Aluminum', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon', \
'Potassium', 'Calcium']
elements.sort()
print(elements)
# [X] print the list, numbers, sorted and then below print the original numbers list
numbers = [2,2,2,1,2,1,3,3,1,2,2,2,2,1,3]
old_numbers = str(numbers)
numbers.sort()
print(f"numbers sorted: {numbers}")
print(f"original numbers: {old_numbers}")
# [X] split the string, daily_fact, into a list of word strings: fact_words
# [X] print each string in fact_words in upper case on it's own line
daily_fact = "Did you know that there are 1.4 billion students in the world?"
fact_words = daily_fact.split()
for item in fact_words:
print(item.upper())
# [X] convert the string, code_tip, into a list made from splitting on the letter "o"
code_tip = "always save your notebook!"
code_list = code_tip.split("o")
for item in code_list:
print(item)
# [X] split poem on "b" to create a list: poem_words
# [X] print poem_words by iterating the list
poem = "The bright brain, has bran!"
poem_words = poem.split("b")
for item in poem_words:
print(item)
# [X] print a comma separated string output from the list of Halogen elements using ".join()"
halogens = ['Chlorine', 'Florine', 'Bromine', 'Iodine']
print(", ".join(halogens))
# [X] split the sentence, code_tip, into a words list
# [X] print the joined words in the list with no spaces in-between
# [X] Bonus: capitalize each word in the list before .join()
code_tip ="Read code aloud or explain the code step by step to a peer"
code_list = code_tip.split()
for number in range(len(code_list)):
code_list[number] = code_list[number].title()
print("".join(code_list))
# [X] cast the long_word into individual letters list
# [X] print each letter on a line
long_word = 'decelerating'
long_list = list(long_word)
for item in long_list:
print(item)
# [X] use use end= in print to output each string in questions with a "?" and on new lines
questions = ["What's the closest planet to the Sun", "How deep do Dolphins swim", "What time is it"]
for item in questions:
print(item, end = "?\n")
# [X] print each item in foot bones
# - capitalized, both words if two word name
# - separated by a comma and space
# - and keeping on a single print line
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
for item in foot_bones:
print(item.title(), end = ", ")