Python Demo Paper
Python Demo Paper
Instructions:
def process_data(items) :
data_set = set (items)
result = ()
for item in data_set:
if items.count (item) > 1:
result[item] items.count (item)
return len(result)
A) 3
Activate Win
B) 2 Go to Settings to
) {'apple': 3, 'banana': 2}
){apple': 3, 'banana': 2}
D) 6
2. Consider the function calculate. Which of the following function cals will execute without raising an error?
def calculate (data, key):
if key in data:
if type(data[key]) == int:
return data[key ] * 2
return 0
4. Which code snippet correctly creates a dictionary where keys are numbers from 0 to 4 and values are their squares? A)
d = )
for i in range(5):
d[i] = i*
B)
d = ()
for i in range(5):
d[i] = /*
d = []
for i in range(5):
d. append ((i: i*i})
D)
d =
for i in range(5):
d. add(i, i*i)
A) 10
B) 14
) 26
D) 2
6. Given my_set = {1, 2, 3).Which operation will successfully add the number 4 to it?
A) my_set. append (4)
B) my_set[3] = 4
) my _set.add (4)
B) 30
) 50
A) "xy"
B) 1
) 2
D) 3
1. To get a list of all values from adictionary named my dict , you can use the method my dict. ().
2. Given the list data = [10, 20, 30, 40], the expression data[ will return the sublist [20, 30].
3. The control statement is used to terminate a loop prematurely.
4. Given data = {"info': ("A', 'B', 'c'), 'ids': [10, 20, 30])}. the expression data[ 'info'[ ] will retrieve the value 'B' .
1. Akey 'total revenue' with its value being the sum of all sale amounts.
2. Akey 'items_sold' with its value being a set of all unique item names that were sold.
3. A key 'sales_by_item' with its value being another dictionary. This inner dictionary should have item names as keys and the total sales amount for each item as
values.
Example:
sales data =
('item': 'book', 'amount' : 15},
{'item': 'pen', 'amount': 2),
{'item': 'book', 'amount' : 20),
{'item': 'notebook', 'amount': 5},
('item': 'pen', ' amount': 3}
summary = summarize_sales (sales data)
print (summary)
# Expected Output (order of items in 'items_sold' set may vary):
#{
'total revenue': 45,
'items_sold': ('book', 'pen', 'notebook'},
'sales by item': f'book ': 35, 'pen': 5, 'notebook': 5}
#}
Activate Win
Go to Sattings to
Answer Key
Part : Single Choice
1. B
2. D
3.C
4. A
5. A
6. C
7.B
8. C
9. D
10. D
result =(
'common': tuple(common_items),
'unique_to_list1' : list(unique_ items_list1)
return result
# ExampLe usage:
list_a = ('a', 'b', ' ' , 'd']
list_b = ['c', 'd', 'e', 'f']
result = find_common_and_unique_items (1ist_a, list_b)
print (result)
Question 2 Solution
total_revenue 0
items_sold = set()
sales_by_item = {}
Part 1 OF Question 2.
# 2. Add to set of unique items
items_sold. add(item)
summary_dict = {
'total_revenue': total_revenue,
'items_sold': items_sold,
'sales_by_item' : sales_by_item
return summary_dict
# Example usage:
sales_data = [
('item': 'book' , 'amount': 15},
{'item' : 'pen', 'amount': 2},
('item' : 'book', 'amount': 20}, Part 2 of Question 2.
('item': 'notebook', 'amount': 5),
('item' : 'pen', 'amount': 3}