WorksheetsPython Concepts Worksheet
Total questions: 25
Worksheet time: 13mins
What is the output of print(outer()())? Python def outer(): x = 10 def inner(): return x * 2 return inner print(outer()())
20
10
NameError: name 'x' is not defined
0
What does list(gen) produce? Python def gen_squares(n): for i in range(n): yield i ** 2 gen = gen_squares(4) next(gen) list(gen)
[0, 1, 4, 9]
[1, 4, 9]
[0, 1, 4]
StopIteration
What is the output of print(lst)? Python data = [1, 2, 3, 4] lst = [(x, y) for x in data for y in data if x != y]
[(1,2), (1,3), (1,4), (2,1), (2,3), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,3)]
[(1,1), (2,2), (3,3), (4,4)]
[(1,2), (2,3), (3,4)]
[]
What is the output of print(add(3, 4))? Python def timer(func): def wrapper(*args, **kwargs): result = func(*args, **kwargs) return result * 2 return wrapper @timer def add(a, b): return a + b
14
7
28
TypeError
What is the output of print(sorted([('b', 2), ('a', 3), ('c', 1)], key=lambda x: x[1]))?
[('c', 1), ('b', 2), ('a', 3)]
[('a', 3), ('b', 2), ('c', 1)]
[('b', 2), ('a', 3), ('c', 1)]
[('a', 3), ('c', 1), ('b', 2)]
What is the result of print(c)? Python d1 = {'a': 1, 'b': 2} d2 = {'b': 3, 'c': 4} c = {**d1, **d2}
{'a': 1, 'b': 3, 'c': 4}
{'a': 1, 'b': 2, 'c': 4}
{'a': 1, 'b': [2, 3], 'c': 4}
SyntaxError
What is printed after executing this? Python try: raise ValueError("err") except Exception as e: print("Caught") finally: print("Finally")
Caught Finally
Finally Caught
err
Nothing
What does with open('file.txt', 'r') as f: content = f.read() do if 'file.txt' doesn't exist?
Raises FileNotFoundError
Returns empty string
Creates the file
Ignores and continues
What is list(result)? Python nums = [1, 2, 3, 4] result = map(lambda x: x * 2, filter(lambda x: x % 2 == 0, nums))
[4, 8]
[2, 4, 6, 8]
[1, 3]
[2, 4]
What is the output of print(reduce(lambda x, y: x + y, [1, 2, 3])) (assuming from functools import reduce)?
6
[1, 3, 6]
3
TypeError
What does re.findall(r"\d+", "abc12def34") return (import re)?
['12', '34']
[12, 34]
'12def34'
[]
What is s1.symmetric_difference(s2) for s1 = {1,2,3}; s2 = {3,4,5}?
{1,2,4,5}
{1,2,3,4,5}
{3}
{1,2}
What is printed? Python data = [1, 5, 3] if (n := max(data)) > 2: print(n) A. 5 B. 1 C. 3 D. Nothing
5
1
3
Nothing
What is printed in the with block? Python from contextlib import contextmanager @contextmanager def managed(): print("Enter") yield print("Exit") with managed(): pass
Enter Exit
Exit Enter
Nothing
SyntaxError
What is printed by asyncio.run(main())? Python import asyncio async def greet(): await asyncio.sleep(0) return "Hello" async def main(): msg = await greet() print(msg)
Hello
None
asyncio.sleep(0)
main
What is the type of f(5) if f: Callable[[int], str]? Python from typing import Callable def f(x: int) -> str: return str(x) f(5)
str
int
Callable
TypeError
After import math; from math import sqrt as sq, what is sq(16)?
4.0
math.sqrt(16)
16
ImportError
What is print(f"{[1,2]*3}")?
[1, 2, 1, 2, 1, 2]
[1,2]*3
[1,1,1,2,2,2]
SyntaxError
What does list(itertools.permutations('abc', 2)) produce (import itertools)?
[('a','b'), ('a','c'), ('b','a'), ('b','c'), ('c','a'), ('c','b')]
['ab', 'ac', 'ba', 'bc', 'ca', 'cb']
[('a','b'), ('b','c'), ('c','a')]
[]
What is print(f"{lambda x: x**2}(3)")?
9
TypeError
3
What does plt.plot([1, 2, 3], [4, 5, 6]) create?
A bar chart
A line connecting points (1,4), (2,5), (3,6)
A scatter plot
Nothing, it needs labels
What must be called to display a plot in a script?
plt.save()
plt.show()
plt.close()
plt.title()
How do you add a title "My Plot" to a figure?
plt.label("My Plot")
plt.title("My Plot")
plt.name("My Plot")
plt.header("My Plot")
What does plt.xlabel("X Axis"); plt.ylabel("Y Axis") do?
Sets the plot colors
Labels the x and y axes
Saves the plot
Changes the line style
How do you save the current plot as 'output.png'?
plt.save('output.png')
plt.savefig('output.png')
plt.export('output.png')
plt.write('output.png')
