wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Basics: Loops and Formatting

Total questions: 30

Worksheet time: 3600secs

Name
Class
Date
1.

Which of the following is the correct way to start a for loop in Python that iterates from 0 to 4?

a)

for i in range(0, 5):

b)

for i = 0 to 4:

c)

for (i = 0; i < 5; i++):

d)

for i in 0..4:

2.

What is the output of the following code? ```python x = 3 while x > 0: print(x) x -= 1 ```

a)

3 2 1

b)

1 2 3

c)

3 2 1 0

d)

2 1 0

3.

Which of the following is a valid data type in Python?

a)

integer

b)

int

c)

number

d)

digit

4.

What will be printed by the following code? ```python print("The temperature is {}°C".format(20)) ```

a)

The temperature is 20°C

b)

The temperature is {}°C

c)

The temperature is 20

d)

The temperature is °C20

5.

Which of the following is the correct way to get user input in Python 3?

a)

input()

b)

raw_input()

c)

scan()

d)

get_input()

6.

What is the output of the following code? ```python print("My favourite colour is", "blue") ```

a)

My favourite colour is blue

b)

My favourite colour isblue

c)

My favourite colour is, blue

d)

My favourite colour is: blue

7.

Which of the following is NOT a built-in data type in Python?

a)

float

b)

str

c)

char

d)

bool

8.

What is the output of the following code? ```python for i in range(2, 6): print(i) ```

a)

2 3 4 5

b)

2 3 4 5 6

c)

1 2 3 4 5

d)

3 4 5 6

9.

Which of the following statements about while loops is true?

a)

A while loop always runs at least once.

b)

A while loop may never run if the condition is False at the start.

c)

A while loop must have an else clause.

d)

A while loop cannot be nested.

10.

What is the output of the following code? ```python print("Weight: {:.2f} kg".format(12.3456)) ```

a)

Weight: 12.35 kg

b)

Weight: 12.34 kg

c)

Weight: 12.3456 kg

d)

Weight: 12.3 kg

11.

Which of the following is the correct way to print the value of a variable called `distance` in miles using an f-string?

a)

print("Distance: {distance} miles")

b)

print(f"Distance: {distance} miles")

c)

print("Distance: f{distance} miles")

d)

print("Distance: distance miles")

12.

What is the data type of the value returned by the input() function in Python 3?

a)

int

b)

float

c)

str

d)

bool

13.

Which of the following will print the string "Hello, World!" with a newline at the end?

a)

print("Hello, World!")

b)

print("Hello, World!", end="")

c)

print("Hello, World!", end="\t")

d)

print("Hello, World!", end=" ")

14.

What is the output of the following code? ```python for i in range(1, 10, 3): print(i) ```

a)

1 4 7

b)

1 2 3 4 5 6 7 8 9

c)

1 3 6 9

d)

1 4 7 10

15.

Which of the following is the correct way to format a string using the % operator?

a)

print("The answer is %d" % 42)

b)

print("The answer is {}".format(42))

c)

print(f"The answer is {42}")

d)

print("The answer is" % 42)

16.

What is the output of the following code? ```python x = 5 while x < 8: print(x) x += 1 ```

a)

5 6 7

b)

5 6 7 8

c)

6 7 8

d)

5 6 7 8 9

17.

Which of the following is NOT a valid way to print the value of a variable `temp` in Celsius?

a)

print("Temperature:", temp, "°C")

b)

print("Temperature: {}°C".format(temp))

c)

print(f"Temperature: {temp}°C")

d)

print("Temperature: " + temp + "°C")

18.

What is the output of the following code? ```python print(type(3.14)) ```

a)

b)

c)

d)

19.

Which of the following will print the numbers from 10 down to 1 (inclusive) using a for loop?

a)

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

b)

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

c)

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

d)

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

20.

What is the output of the following code? ```python print("Volume: {:.1f} ounces".format(8.75)) ```

a)

Volume: 8.8 ounces

b)

Volume: 8.7 ounces

c)

Volume: 8.75 ounces

d)

Volume: 9.0 ounces

21.

Which of the following is the correct way to check if a variable `x` is of type int?

a)

type(x) == int

b)

type(x) = int

c)

x.type() == int

d)

x == int

22.

What is the output of the following code? ```python for i in range(3): print("Metre") ```

a)

Metre Metre Metre

b)

Metre

c)

Metre 3

d)

3 Metre

23.

Which of the following is the correct way to print without a newline at the end?

a)

print("Hello", end="")

b)

print("Hello", end="\n")

c)

print("Hello", end="\\n")

d)

print("Hello")

24.

What is the output of the following code? ```python x = input("Enter a number: ") print(type(x)) ``` (Assume the user enters 5.)

a)

b)

c)

d)

25.

Which of the following is the correct way to print the value of 2\sqrt{2} rounded to 3 decimal places?

a)

print("{:.3f}".format(2 ** 0.5))

b)

print("{:.2f}".format(2 ** 0.5))

c)

print("{:.1f}".format(2 ** 0.5))

d)

print("{:.4f}".format(2 ** 0.5))

26.

Which of the following is a valid float value in Python?

a)

3.0

b)

"3.0"

c)

3,

d)

3

27.

What is the output of the following code? ```python for i in range(0, 6, 2): print(i) ```

a)

0 2 4

b)

0 1 2 3 4 5

c)

2 4 6

d)

0 2 4 6

28.

Which of the following is the correct way to print the string "5 pounds and 8 ounces"?

a)

print("5 pounds and 8 ounces")

b)

print('5 pounds and 8 ounces')

c)

print(f"{5} pounds and {8} ounces")

d)

All of the above

29.

What is the output of the following code? ```python print("Distance: {} miles".format(15)) ```

a)

Distance: 15 miles

b)

Distance: {} miles

c)

Distance: miles 15

d)

Distance: 15

30.

Which of the following is the correct way to print the value of a variable `temp` in Celsius using an f-string?

a)

print(f"Temperature: {temp}°C")

b)

print("Temperature: {temp}°C")

c)

print("Temperature: temp°C")

d)

print(f"Temperature: temp°C")