Question

In: Computer Science

Question 1 (20pts): Create a dictionary of the form {“Jordan”: “Basketball”, “Federer”: “Tennis”, “Ronaldo”: “Football”} and...

Question 1 (20pts):

Create a dictionary of the form {“Jordan”: “Basketball”, “Federer”: “Tennis”, “Ronaldo”: “Football”} and perform the following actions

  • Print all the keys in the dictionary
  • Print all the values in the dictionary
  • Iterate through the dictionary and print the value only if the key is “Federer”
  • Iterate through the dictionary and print the key only if the value is “Football”

Question 2 (15pts):

List1 = [3, 4, 5, 20, 5]

List2 = [4, 9, 6, 2, 10].

Using list comprehensions, print the output if you multiply the i-th element of each list

Question 3 (15pts):¶

Dic1 = {"John":10, "Joe":15}

  • Check whether “John” is one of the keys
  • Remove the key “Joe” along with its associated value
  • Add another key and value pair - “Jean”: 40

Question 4 (15pts):

  • Write a user defined function to find the highest of the three numbers: 30, 10 and 20. The highest number should be printed. Note: The function has to be user defined.

  • With a given integral number n, write a program to generate a dictionary that contains key value pairs such as i: 2^i, and i takes values between 1 and n (both included). Then print the dictionary.

Suppose the following input is supplied to the program:

4

Then, the output should be:

{1: 2, 2: 4, 3: 8, 4: 16}

Question 5 (20pts):

  • Using list comprehensions, print all the values between 1 and 10 that are divisible by 2. The output should be [2, 4, 6, 8].

  • By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155].

Question 6 (15pts):

Remove the last value in [12,24,35,24,88,120,155] consecutively and print the removed value in each removal.

Solutions

Expert Solution

Dictionary={"Jordan":"Basketball", "Federer": "Tennis", "Ronaldo": "Football"}#Creation of dictionary
print(Dictionary.keys())#This method prints all the keys in dictionary
print(Dictionary.values())#This method prints all the values in dictionary
for i in Dictionary:#for lopp starts
    if(i == "Federer"):# 'i' looks at the key so if key equals the federer then the value gets printed.
        print(Dictionary[i])# Dictionary[i] looks at the value so if value equals the Football then the key gets printed.
for i in Dictionary:
    if(Dictionary[i] == "Football"):
        print(i)



___________________________________________________________________________END of question1_________________________________________________________________________________________

List1 = [3, 4, 5, 20, 5]#Creation of List1
List2 = [4, 9, 6, 2, 10]#Creation of List2
resultinglist = [List1[i] * List2[i] for i in range(len(List1))] # This is list comprehension means execution occurs in a single line. Here the loop repeats until the length of the list be false and each index of each list1 index0 * list2 index 0 and list1 index1 * list2 index1 . so this procedure continues. 
print(resultinglist)# Result is printed here

Output

############################################################################################ENd of question2##############################################

Dic1 = {"John":10, "Joe":15}#Dictionary is created
if "John" in Dic1 : print("John Exists")# Here loop is not required. we can check with simple 'in' Operator .
Dic1.pop("Joe")# using Pop we can remove the Joe element
print(Dic1) #Prints the updated the dictionary
Dic1["Jean"]= 40 #Adding the new element Jean with 40
print(Dic1)#Prints the updated the dictionary

++++++++++++++++++++++++++++++++++++++++++++++ENd of question3++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

def biggestnum(a,b,c):# function is defined here and the passsed parameters are num1,num2,num3 as a,b,c
    if(a>b and a>c):#This branch checks whether A is big or not
        print("{} is big".format(a))#prints 10 is  big
    elif(b>a and b>c):#This branch checks whether B is big or not
        print("{} is big" .format(b))#prints 20 is big
    else:#If none of them are true then 30 is big 
        print("{} is big" .format(c))

num1 = 10#initialization of num1
num2 = 20#initialization of num2
num3 = 30#initialization of num3

biggestnum(num1,num2,num3)# calling the function

OUTPUT


dict={}#Creation of empty dictionary
n=int(input())#Reads the value of n
for i in range(1,n+1):# Loop iterated from 1 to N+1 because python takes i<n but not i<=n. so we do that
    dict[i]=2**i#performing the power raising and storing at the particular key 

print(dict)#printing the dictionary

++++++++++++++++++++++++++ENd of question 4++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

resultinglist = [i for i in range(1,10) if(i%2==0)]# The resulting list is developed when the remainder is zero adn divided by 2 and we are stoing the ith value in the list
print(resultinglist)#prints the list

OUTPUT

list1=[12,24,35,24,88,120,155]#creation of list
list1 = [i for i in (list1) if(i!=24)]#we are copying all the elements except 24
print(list1)#prints the list

OUTPUT

###################################END OF QUESTION5#####################################################################################################################################

list1=[12,24,35,24,88,120,155]#creation of list 
for i in range(0,len(list1)):#loops for the range of list 
    print(list1.pop())#prints the popped elements list

output

Rate me and for query resolution do comments ,.

Thanks


Related Solutions

World no.1 Rafael Nadal is to succeed football icons David Beckham and Cristiano Ronaldo as the...
World no.1 Rafael Nadal is to succeed football icons David Beckham and Cristiano Ronaldo as the male face of Emporio Armani Underwear and Armani jeans. The 24 year-old Spaniard is now building a massive portfolio of endorsement deals with concerns such as Richard Mille watches, Mapfe SA (Spain’s largest insurance company), Kia Motors, Lanvin fragrances and of course Nike Inc. and Babolat. Even the most conservative estimates maintain that Nadal’s commercial appeal will boost his annual earnings to somewhere comfortably...
World no.1 Rafael Nadal is to succeed football icons David Beckham and Cristiano Ronaldo as the...
World no.1 Rafael Nadal is to succeed football icons David Beckham and Cristiano Ronaldo as the male face of Emporio Armani Underwear and Armani jeans. The 24 year-old Spaniard is now building a massive portfolio of endorsement deals with concerns such as Richard Mille watches, Mapfe SA (Spain’s largest insurance company), Kia Motors, Lanvin fragrances and of course Nike Inc. and Babolat. Even the most conservative estimates maintain that Nadal’s commercial appeal will boost his annual earnings to somewhere comfortably...
Python create a function tryhard and print out the dictionary as a string form. For example...
Python create a function tryhard and print out the dictionary as a string form. For example def tryhard(d:dict): #Code here input: d = {'first': {}, 'second': {'1': {'move'}, '0': {'move', 'slow'}}, 'third': {'1': {'stop'}}} output = " first movie: [ ]\n third movie: [('1', ['stop'])]\n second movie: [('0', ['slow', 'move']), ('1', ['move'])]\n"
ET574 Dictionary (Chapter 6) 1) Create a dictionary containing the following pieces of information about a...
ET574 Dictionary (Chapter 6) 1) Create a dictionary containing the following pieces of information about a student: Her name is Emmylou; She is on track to graduate in Fall 2021; Her bill is paid; Her major is Archeology; She belongs to these school clubs – Photography, Acting and Glee (Your job as the programmer is to choose the best data type as well as any key names you will use within the program) 2) Do the same for another student:...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Part 1) Independent random samples of professional football and basketball players gave the following information. Assume...
Part 1) Independent random samples of professional football and basketball players gave the following information. Assume that the weight distributions are mound-shaped and symmetric. Weights (in lb) of pro football players: x1; n1 = 21 248 261 255 251 244 276 240 265 257 252 282 256 250 264 270 275 245 275 253 265 270 Weights (in lb) of pro basketball players: x2; n2 = 19 203 200 220 210 191 215 221 216 228 207 225 208 195...
Practice Study Guide Questions 1) Independent random samples of professional football and basketball players gave the...
Practice Study Guide Questions 1) Independent random samples of professional football and basketball players gave the following information. Assume that the weight distributions are mound-shaped and symmetric. Weights (in lb) of pro football players: x1; n1 = 21 245 263 254 251 244 276 240 265 257 252 282 256 250 264 270 275 245 275 253 265 271 Weights (in lb) of pro basketball players: x2; n2 = 19 205 200 220 210 191 215 222 216 228 207...
Question 1: J. Zheng, a former professional tennis star, operates Zheng’s Tennis Shop, Ltd. at the...
Question 1: J. Zheng, a former professional tennis star, operates Zheng’s Tennis Shop, Ltd. at the Yalong River Resort. At the beginning of the current season, the ledger of Zheng’s Tennis Shop showed Cash ¥2,200, Inventory ¥1,800, and Share Capital—Ordinary ¥4,000. The following transactions were completed during April. April. 4. Purchased racquets and balls from Jay-Mac Co. ¥8760, FOB shipping point, terms 2/10, n/30. 6. Paid freight on purchase from Jay-Mac Co. ¥240. 7. Purchased Supplies from Jay-Mac Co for...
An example of a health form with the following requirements: 1. Create a meaningful form for...
An example of a health form with the following requirements: 1. Create a meaningful form for your application, for example, a health monitoring app 2. Edit CSS for your application. The page must be styled with at least 10 CSS styles This is using Visual Studio to use reactive form application
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT