NEW
Font size
WorksheetsPython Basics: Loops and Formatting
Total questions: 30
Worksheet time: 3600secs
Which of the following is the correct way to start a for loop in Python that iterates from 0 to 4?
for i in range(0, 5):
for i = 0 to 4:
for (i = 0; i < 5; i++):
for i in 0..4:
What is the output of the following code? ```python x = 3 while x > 0: print(x) x -= 1 ```
3 2 1
1 2 3
3 2 1 0
2 1 0
Which of the following is a valid data type in Python?
integer
int
number
digit
What will be printed by the following code? ```python print("The temperature is {}°C".format(20)) ```
The temperature is 20°C
The temperature is {}°C
The temperature is 20
The temperature is °C20
Which of the following is the correct way to get user input in Python 3?
input()
raw_input()
scan()
get_input()
What is the output of the following code? ```python print("My favourite colour is", "blue") ```
My favourite colour is blue
My favourite colour isblue
My favourite colour is, blue
My favourite colour is: blue
Which of the following is NOT a built-in data type in Python?
float
str
char
bool
What is the output of the following code? ```python for i in range(2, 6): print(i) ```
2 3 4 5
2 3 4 5 6
1 2 3 4 5
3 4 5 6
Which of the following statements about while loops is true?
A while loop always runs at least once.
A while loop may never run if the condition is False at the start.
A while loop must have an else clause.
A while loop cannot be nested.
What is the output of the following code? ```python print("Weight: {:.2f} kg".format(12.3456)) ```
Weight: 12.35 kg
Weight: 12.34 kg
Weight: 12.3456 kg
Weight: 12.3 kg
Which of the following is the correct way to print the value of a variable called `distance` in miles using an f-string?
print("Distance: {distance} miles")
print(f"Distance: {distance} miles")
print("Distance: f{distance} miles")
print("Distance: distance miles")
What is the data type of the value returned by the input() function in Python 3?
int
float
str
bool
Which of the following will print the string "Hello, World!" with a newline at the end?
print("Hello, World!")
print("Hello, World!", end="")
print("Hello, World!", end="\t")
print("Hello, World!", end=" ")
What is the output of the following code? ```python for i in range(1, 10, 3): print(i) ```
1 4 7
1 2 3 4 5 6 7 8 9
1 3 6 9
1 4 7 10
Which of the following is the correct way to format a string using the % operator?
print("The answer is %d" % 42)
print("The answer is {}".format(42))
print(f"The answer is {42}")
print("The answer is" % 42)
What is the output of the following code? ```python x = 5 while x < 8: print(x) x += 1 ```
5 6 7
5 6 7 8
6 7 8
5 6 7 8 9
Which of the following is NOT a valid way to print the value of a variable `temp` in Celsius?
print("Temperature:", temp, "°C")
print("Temperature: {}°C".format(temp))
print(f"Temperature: {temp}°C")
print("Temperature: " + temp + "°C")
What is the output of the following code? ```python print(type(3.14)) ```
Which of the following will print the numbers from 10 down to 1 (inclusive) using a for loop?
for i in range(10, 0, -1): print(i)
for i in range(1, 11): print(i)
for i in range(10, 1, -1): print(i)
for i in range(10, 0): print(i)
What is the output of the following code? ```python print("Volume: {:.1f} ounces".format(8.75)) ```
Volume: 8.8 ounces
Volume: 8.7 ounces
Volume: 8.75 ounces
Volume: 9.0 ounces
Which of the following is the correct way to check if a variable `x` is of type int?
type(x) == int
type(x) = int
x.type() == int
x == int
What is the output of the following code? ```python for i in range(3): print("Metre") ```
Metre Metre Metre
Metre
Metre 3
3 Metre
Which of the following is the correct way to print without a newline at the end?
print("Hello", end="")
print("Hello", end="\n")
print("Hello", end="\\n")
print("Hello")
What is the output of the following code? ```python x = input("Enter a number: ") print(type(x)) ``` (Assume the user enters 5.)
Which of the following is the correct way to print the value of 2 rounded to 3 decimal places?
print("{:.3f}".format(2 ** 0.5))
print("{:.2f}".format(2 ** 0.5))
print("{:.1f}".format(2 ** 0.5))
print("{:.4f}".format(2 ** 0.5))
Which of the following is a valid float value in Python?
3.0
"3.0"
3,
3
What is the output of the following code? ```python for i in range(0, 6, 2): print(i) ```
0 2 4
0 1 2 3 4 5
2 4 6
0 2 4 6
Which of the following is the correct way to print the string "5 pounds and 8 ounces"?
print("5 pounds and 8 ounces")
print('5 pounds and 8 ounces')
print(f"{5} pounds and {8} ounces")
All of the above
What is the output of the following code? ```python print("Distance: {} miles".format(15)) ```
Distance: 15 miles
Distance: {} miles
Distance: miles 15
Distance: 15
Which of the following is the correct way to print the value of a variable `temp` in Celsius using an f-string?
print(f"Temperature: {temp}°C")
print("Temperature: {temp}°C")
print("Temperature: temp°C")
print(f"Temperature: temp°C")
