Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Mastering Print and Loops in Python

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is the output of print('Hello, World!')?

a)

Hello, World!

b)

Hello, World?

c)

Hello, World!!

d)

Hello World!

2.

How do you print multiple items in one line using print()?

a)

print(item1 + item2 + item3)

b)

print(item1; item2; item3)

c)

print(item1 | item2 | item3)

d)

print(item1, item2, item3)

3.

What is the purpose of the end parameter in the print() function?

a)

To control the speed of the printing process.

b)

To determine the number of characters printed in the output.

c)

To specify the string printed at the end of the output.

d)

To set the maximum length of the output string.

4.

How can you format a string using f-strings in Python?

a)

Use f'Hello, {name}!' to format a string with a variable.

b)

Use f'{name} is here!' to include a variable in a string.

c)

Format a string with f'Value: {value}' for variable output.

d)

Utilize f'Greetings, {user}!' to insert a variable into text.

5.

What will be the output of print('Value: {}'.format(10))?

a)

Value: ten

b)

Value: 1

c)

Value: 10.0

d)

Value: 10

6.

How do you iterate over a list using a for loop?

a)

for item in my_list:

b)

for item of my_list:

c)

for item in list(my_list):

d)

for each item in my_list:

7.

What is the output of the following code: for i in [1, 2, 3]: print(i)?

a)

4

b)

0

c)

5

d)

1 2 3

8.

How can you use a for loop to print each character in a string?

a)

for char in my_string: print(char)

b)

while char in my_string: print(char)

c)

for char in range(len(my_string)): print(my_string[char])

d)

for each char in my_string: print(char)

9.

What does the range() function do in Python?

a)

The range() function calculates the sum of numbers.

b)

The range() function creates a list of strings.

c)

The range() function generates a sequence of numbers.

d)

The range() function sorts a given list.

10.

How can you create a list of numbers from 0 to 9 using range()?

a)

list(range(0, 10))

b)

range(0, 10).tolist()

c)

list(range(10))

d)

array(range(10))

11.

What is the output of print('Number: %d' % 5)?

a)

Number: 6

b)

Number: 5

c)

Number: five

d)

Number: 5.0

12.

How do you use a for loop with the range() function to print numbers from 1 to 5?

a)

for i in range(1, 6): print(i)

b)

for i in range(0, 5): print(i)

c)

for i in range(5): print(i + 1)

d)

for i in range(1, 10): print(i)

13.

What will be the output of the following code: for i in range(3): print(i * 2)?

a)

0 2 4

b)

5

c)

1

d)

3

14.

How can you format a string to include variables in Python?

a)

You can format a string using the join() method.

b)

Variables can be included by concatenating strings.

c)

You must use the eval() function to format strings.

d)

You can format a string to include variables using f-strings, format() method, or % operator.

15.

What is the difference between print() and return in a function?

a)

print() returns a value; return displays output.

b)

print() is used for debugging; return is for final results.

c)

print() displays output; return provides a value to the caller.

d)

print() outputs to a file; return sends data to a database.

16.

How do you concatenate strings in the print() function?

a)

You can use '+' to concatenate strings or separate them with commas in print().

b)

You should use '&' to join strings in print().

c)

You can use '*' to concatenate strings in print().

d)

You must use '->' to combine strings in print().

17.

What will be the output of print('Hello', 'World', sep='-')?

a)

Hello, World

b)

Hello-World!

c)

Hello World

d)

Hello-World

18.

How can you use a for loop to sum the elements of a list?

a)

total = 0; while element in list: total += element

b)

sum = 0; for element in list: sum += element

c)

sum = 0; for item in list: sum = sum + item

d)

result = 0; for each in list: result = result - each

19.

What is the output of print('The answer is', 42, sep=': ')?

a)

The answer is - 42

b)

The answer is; 42

c)

The answer is: 42

d)

The answer is 42;

20.

How do you use a for loop to iterate over a dictionary's keys?

a)

for key in my_dict:

b)

for key in my_dict.items():

c)

for key in range(len(my_dict))

d)

for key in my_dict.keys():