In: Computer Science
Question 1 (20pts):
Create a dictionary of the form {“Jordan”: “Basketball”, “Federer”: “Tennis”, “Ronaldo”: “Football”} and perform the following actions
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}
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.
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