In: Computer Science
Short Python 3 questions
a. Create a dictionary that maps the first n counting numbers to their squares. Assign the dictionary to the variable squares
b. Given the list value_list, assign the number of nonduplicate values to the variable distinct_values
a)Python code:
#accepting n
n=int(input("Enter n: "))
#creating a dictionary having first n counting numbers as key and
their squares as value
squares={i:i*i for i in range(1,n+1)}
#printing the dictionary
print(squares)
Screenshot:
Input and Output:
b)Python code:
#initializing a sample value_list
value_list=[1,2,2,2,3,4,5,5]
#removing duplicate values and finding the number of non duplicate
elements in this list and assigning it to distinct_values
distinct_values=len(set(value_list))
#printing distinct_values
print(distinct_values)
Screenshot:
Output: