wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Concepts Worksheet

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

What is the output of print(outer()())? Python def outer(): x = 10 def inner(): return x * 2 return inner print(outer()())

a)

20

b)

10

c)

NameError: name 'x' is not defined

d)

0

2.

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)

a)

[0, 1, 4, 9]

b)

[1, 4, 9]

c)

[0, 1, 4]

d)

StopIteration

3.

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]

a)

[(1,2), (1,3), (1,4), (2,1), (2,3), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,3)]

b)

[(1,1), (2,2), (3,3), (4,4)]

c)

[(1,2), (2,3), (3,4)]

d)

[]

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

a)

14

b)

7

c)

28

d)

TypeError

5.

What is the output of print(sorted([('b', 2), ('a', 3), ('c', 1)], key=lambda x: x[1]))?

a)

[('c', 1), ('b', 2), ('a', 3)]

b)

[('a', 3), ('b', 2), ('c', 1)]

c)

[('b', 2), ('a', 3), ('c', 1)]

d)

[('a', 3), ('c', 1), ('b', 2)]

6.

What is the result of print(c)? Python d1 = {'a': 1, 'b': 2} d2 = {'b': 3, 'c': 4} c = {**d1, **d2}

a)

{'a': 1, 'b': 3, 'c': 4}

b)

{'a': 1, 'b': 2, 'c': 4}

c)

{'a': 1, 'b': [2, 3], 'c': 4}

d)

SyntaxError

7.

What is printed after executing this? Python try: raise ValueError("err") except Exception as e: print("Caught") finally: print("Finally")

a)

Caught Finally

b)

Finally Caught

c)

err

d)

Nothing

8.

What does with open('file.txt', 'r') as f: content = f.read() do if 'file.txt' doesn't exist?

a)

Raises FileNotFoundError

b)

Returns empty string

c)

Creates the file

d)

Ignores and continues

9.

What is list(result)? Python nums = [1, 2, 3, 4] result = map(lambda x: x * 2, filter(lambda x: x % 2 == 0, nums))

a)

[4, 8]

b)

[2, 4, 6, 8]

c)

[1, 3]

d)

[2, 4]

10.

What is the output of print(reduce(lambda x, y: x + y, [1, 2, 3])) (assuming from functools import reduce)?

a)

6

b)

[1, 3, 6]

c)

3

d)

TypeError

11.

What does re.findall(r"\d+", "abc12def34") return (import re)?

a)

['12', '34']

b)

[12, 34]

c)

'12def34'

d)

[]

12.

What is s1.symmetric_difference(s2) for s1 = {1,2,3}; s2 = {3,4,5}?

a)

{1,2,4,5}

b)

{1,2,3,4,5}

c)

{3}

d)

{1,2}

13.

What is printed? Python data = [1, 5, 3] if (n := max(data)) > 2: print(n) A. 5 B. 1 C. 3 D. Nothing

a)

5

b)

1

c)

3

d)

Nothing

14.

What is printed in the with block? Python from contextlib import contextmanager @contextmanager def managed(): print("Enter") yield print("Exit") with managed(): pass

a)

Enter Exit

b)

Exit Enter

c)

Nothing

d)

SyntaxError

15.

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)

a)

Hello

b)

None

c)

asyncio.sleep(0)

d)

main

16.

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)

a)

str

b)

int

c)

Callable

d)

TypeError

17.

After import math; from math import sqrt as sq, what is sq(16)?

a)

4.0

b)

math.sqrt(16)

c)

16

d)

ImportError

18.

What is print(f"{[1,2]*3}")?

a)

[1, 2, 1, 2, 1, 2]

b)

[1,2]*3

c)

[1,1,1,2,2,2]

d)

SyntaxError

19.

What does list(itertools.permutations('abc', 2)) produce (import itertools)?

a)

[('a','b'), ('a','c'), ('b','a'), ('b','c'), ('c','a'), ('c','b')]

b)

['ab', 'ac', 'ba', 'bc', 'ca', 'cb']

c)

[('a','b'), ('b','c'), ('c','a')]

d)

[]

20.

What is print(f"{lambda x: x**2}(3)")?

a)

9

b)

at 0x...>

c)

TypeError

d)

3

21.

What does plt.plot([1, 2, 3], [4, 5, 6]) create?

a)

A bar chart

b)

A line connecting points (1,4), (2,5), (3,6)

c)

A scatter plot

d)

Nothing, it needs labels

22.

What must be called to display a plot in a script?

a)

plt.save()

b)

plt.show()

c)

plt.close()

d)

plt.title()

23.

How do you add a title "My Plot" to a figure?

a)

plt.label("My Plot")

b)

plt.title("My Plot")

c)

plt.name("My Plot")

d)

plt.header("My Plot")

24.

What does plt.xlabel("X Axis"); plt.ylabel("Y Axis") do?

a)

Sets the plot colors

b)

Labels the x and y axes

c)

Saves the plot

d)

Changes the line style

25.

How do you save the current plot as 'output.png'?

a)

plt.save('output.png')

b)

plt.savefig('output.png')

c)

plt.export('output.png')

d)

plt.write('output.png')