Week 7 Tutorial
Week 7 Tutorial
A) 5
B) 11
C) (5, 11)
D) Error
Answer: A) 5
Question 2: Which of the following methods returns a dictionary from a function in Python?
items.sort(key=lambda x: x[0])
print(items)
A) data.sort(reverse=True)
B) sorted(data, reverse=True)
C) data.sort()
D) sorted(data, key=lambda x: x[1])
Answer: C) data.sort()
return a * b, a - b
result = process_data(6, 2)
print(result[1])
A) 8
B) 12
C) (12, 4)
D) 4
Answer: D) 4
7. Returning a Tuple: The most common way is to return a tuple by separating values with
commas.
diff_value = a - b
result = calculate(10, 5)
print(result)
# Output: (15, 5)
Here, the calculation returns both the sum and difference as a tuple.
8. Returning a List: Functions can also return a list, which is mutable and can contain multiple
data types.
def get_data():
result = get_data()
9. Returning a Dictionary: This method is helpful when returning values with keys to describe
each.
def create_profile(name, age):
print(profile)
10.Returning a Tuple Using return Statement: When returning a tuple directly, values are separated
by commas.
def coordinates():
return 5, 10
x, y = coordinates()
# Output: x: 5, y: 10
How to Do Sorting
Python provides several ways to sort data, such as using the sorted() function or the sort() method
for lists.
11. Using sort() Method for Lists: This method sorts the list in place (it modifies the original list).
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort()
12. Using sorted() Function: This function returns a new sorted list and works on any iterable.
numbers = [3, 1, 4, 1, 5, 9]
sorted_numbers = sorted(numbers)
13. Sorting in Reverse Order: Both sort() and sorted() can sort in descending order using
reverse=True.
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort(reverse=True)
14. Sorting with a Key: You can use a key argument to sort complex structures like lists of tuples.
fruits = [('apple', 2), ('banana', 1), ('cherry', 3)]
MCQ Questions
def values():
return 1, 2, 3
x, y, z = values()
print(x + y + z)
A) 6
B) (1, 2, 3)
C) [1, 2, 3]
D) Error
Answer: A) 6
16. Which of the following code snippets correctly returns a list from a function?
A) def get_numbers():
return 1, 2, 3
B) def get_numbers():
return [1, 2, 3]
C) def get_numbers():
return {1, 2, 3}
D) def get_numbers():
return 1; 2; 3
Answer: B)
def get_numbers():
return [1, 2, 3]
17. Which of the following statements will sort the list data in descending order?
data = [5, 2, 9, 1]
A) data.sort()
B) sorted(data)
C) sorted(data, reverse=True)
D) data.sort(reverse=True)
Answer: D) data.sort(reverse=True)
items.sort(key=lambda x: x[0])
print(items)
return a + b, a * b
result = process_data(3, 4)
print(result[1])
a) 7
b) 12
c) (7, 12)
d) 3
Answer: B) 12
20. Which of the following code snippets returns a tuple from a function?
A) def get_values():
return [1, 2, 3]
B) def get_values():
return {1, 2, 3}
C) def get_values():
return 1, 2, 3
D) def get_values():
Answer: C)
def get_values():
return 1, 2, 3
Ans:a
Ans:b
23. Where is function defined?
a) Module
b) Class
c) Another function
d) All of the above
24. x=str
def func1(str):
print("Hi")
y=func1(x)
Output for the above code
a) Error
b) Hi
c) No output
d) Hi Hi
Ans:b
25. Does Lambda contain return statements?
a) True
b) False
Ans:b
a) 25
b) 10 15
c) 0
d) (25,0)
Ans:(d)
def return_AlphabetOnly_Strings(*args):
return [x for x in args if x.isalpha()]
print(return_AlphabetOnly_Strings("Sidharth","Sid123","12345","Bennett
University"))
Solution. ['Sidharth']
def greater_lesser(x,y):
if x>=y:return x,y
else:return y,x
print(greater_lesser(int(input()),int(input())))
Solution: The major limitation of above code could be that in above code the outcome of function
generate_list will always generate the same outcome irrespective of the input provided
Solution
def generate_list(n):
return [x**2 for x in range(2,n,2)]
print(generate_list(int(input())))
32. Modify the code given in Q5 to output list of only squares of odd numbers in the
required range
Solution:
def generate_list(n):
return [x**2 for x in range(1,n,2) if x%2==1]
print(generate_list(int(input())))
Top of Form