ages = (55, 28, 24, 34, 59, 22, 19, 72, 21, 37)Write a Python code that uses a list comprehension to create a list
of ages that are over 30 based on the ages list.
Answer
over30list = [age for age in ages if age > 30]
print("The list of ages over 30 is: ", over30list)Write a Python code that uses (1) input() function with a message in the Console “Please enter a number less than 100:” and (2) if-else statement to print (a) “The value entered is greater than or equal to 75.” if the value entered from the Spyder Console is greater than or equal to 75, and (b) “The value entered is greater than or equal to 50.” and “The value entered is also less than 75.” if the value entered from the Spyder Console is less than 75 and greater than or equal to 50, and (c) “The value entered is less than 50.” otherwise.
Answer
value_entered = input("please enter a number less than 100: ")
if int(value_entered) >= 75:
print("The value entered is greater than or equal to 75.")
elif int(value_entered) < 75 and int(value_entered) >= 50:
print("The value entered is greater than or equal to 50.")
print("The value entered is also less than 75.")
else:
print("The value entered is less than 50.")sequence = [1, 2, None, 4, None, 5, None, None, 6, 7, 8, None, 9, 10]Write a Python code that uses a for-loop statement to process each
element of sequence to calculate the sum of all the numeric
values in the sequence list.
Answer
sum_of_sequence = 0
for value in sequence:
if value == None:
continue
sum_of_sequence += value
sum_of_sequence
print("The total of the values in the sequence is: ", sum_of_sequence)Wordle = ["shark",
"wheal",
"cheat",
"stand",
"table",
"fight",
"crane",
"pease",
"sweet",
"lamed",
"stope",
"hints",
"month",
"shade",
"mayor",
"faith",
"cable",
"today",
"tidal",
"stick",
"lochs",
"short",
"docus",
"split",
"swift",
"sweat",
"skeet",
"swelt",
"blast",
"trash",
"tiger",
"setup",
"fiest",
"belly",
"pokey",
"sting",
"blond",
"goldy",
"stamp",
"hooly",
"moray",
"dumas",
"roast",
"chino",
"chest",
"check",
"roast",
"blind",
"check",
"fogle",
"cheek",
"oxide",
"olive",
"deash",
"paves",
"steam",
"absey",
"pesty",
"mardy",
"strap",
"forum",
"forby",
"baste",
"bagie",
"stomp",
"misty",
"adieu",
"balky",
"stick",
"couch",
"dutch",
"steam",
"stomp",
"calos",
"pasty",
"guild",
"meril",
"hasty",
"birch",
"grind",
"filer",
"paler",
"glide",
"fudge",
"kedge",
"wedge",
"hedge",
"plica",
"chirp",
"manor",
"maids",
"crest",
"covin",
"blart",
"chart",
"comma",
"gavel",
"aphid",
"orbit",
"sloth",
"stear",
"blurs"] Write a Python code that uses a for-loop statement and a
if-else statement to create a Wordle dictionary
for which a key is an alphabet and a value is a list of words.
Answer
by_letter = {}
for word in Wordle:
letter = word[0]
if letter not in by_letter:
by_letter[letter] = [word]
else:
by_letter[letter].append(word)
by_letterWordle_by_letter = {'s': ['shark',
'stand',
'sweet',
'stope',
'shade',
'stick',
'short',
'split',
'swift',
'sweat',
'skeet',
'swelt',
'setup',
'sting',
'stamp',
'steam',
'strap',
'stomp',
'stick',
'steam',
'stomp',
'sloth',
'stear'],
'w': ['wheal', 'wedge'],
'c': ['cheat',
'crane',
'cable',
'chino',
'chest',
'check',
'check',
'cheek',
'couch',
'calos',
'chirp',
'crest',
'covin',
'chart',
'comma'],
't': ['table', 'today', 'tidal', 'trash', 'tiger'],
'f': ['fight', 'faith', 'fiest', 'fogle', 'forum', 'forby', 'filer', 'fudge'],
'p': ['pease', 'pokey', 'paves', 'pesty', 'pasty', 'paler', 'plica'],
'l': ['lamed', 'lochs'],
'h': ['hints', 'hooly', 'hasty', 'hedge'],
'm': ['month', 'mayor', 'moray', 'mardy', 'misty', 'meril', 'manor', 'maids'],
'd': ['docus', 'dumas', 'deash', 'dutch'],
'b': ['blast',
'belly',
'blond',
'blind',
'baste',
'bagie',
'balky',
'birch',
'blart',
'blurs'],
'g': ['goldy', 'guild', 'grind', 'glide', 'gavel'],
'r': ['roast', 'roast'],
'o': ['oxide', 'olive', 'orbit'],
'a': ['absey', 'adieu', 'aphid'],
'k': ['kedge']}Write a Python code that uses (1) a dictionary,
Wordle_by_letter (2) sorted(), and (3)
dictionary comprehension to create an alphabetically-ordered Wordle
dictionary for which a key is an alphabet and a value is a list of
words.
Answer
dict_iterable = list( Wordle_by_letter.items() )
dict_iterable = sorted( dict_iterable )
by_letter = { k : v for k, v in dict_iterable }
by_letterThis Python code uses a function to look up code meanings for variables, region and happy, using dictionaries:
def code_lookup(codes):
region, happy = codes
print("region, happy: ", (region, happy))
region_dict = {1:"New England",
2:"Middle Atlantic",
3:"East North Central",
4:"West North Central",
5:"South Atlantic",
6:"East South Central",
7:"West South Central",
8:"Mountain",
9:"Pacific"}
happy_dict = {1:"Very happy",
2:"Pretty happy",
3:"Not too happy",
8:"Don't know",
9:"No answer",
0:"Not applicable"}
return(region_dict[region], happy_dict[happy])Write a Python code that uses the function code_lookup()
and the print() function to print the followings:
Answer
codes = 3, 2
response = code_lookup(codes)
print("Interview region: " + response[0])
print("Happiness level: " + response[1])