wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python For Loops

Total questions: 33

Worksheet time: 1hrs 6mins

Name
Class
Date
1.

What is the syntax of a for loop in Python?

a)

for item in iterable: # code to be executed

b)

for item in iterable: # code to be executed

c)

for element in list: # code to be executed

d)

for i in range(n): # code to be executed

2.

How do you use the range() function in a for loop?

a)

Specify only the step value within the range() function.

b)

Specify the start, stop, and step values within the range() function.

c)

Specify only the stop value within the range() function.

d)

Specify only the start value within the range() function.

3.

What is the output of the following code? for i in range(5): print(i)

a)

0 1 2 3 4

b)

0 1 2 3 5

c)

1 2 3 4 5

d)

0 1 2 3 6

4.

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

a)

1 2 3 4 5

b)

1 2 3 4

c)

1 2 3 4 6

d)

1 2 3 4 6 7

5.

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

a)

1 3 5 7

b)

2 4 6 8

c)

0 2 4 6 8

d)

1 3 5 7 9

6.

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

a)

1

b)

10 9 8 7 6 5 4 3 2

c)

11

d)

0

7.

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

a)

for i in range(len(my_list)):

b)

for item in range(my_list):

c)

for item in my_list()

d)

for item in my_list:

8.

What is the output of the following code? fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(fruit)

a)

The output of the code is: apple banana cherry

b)

cherry, apple, banana

c)

orange, banana, apple

d)

apple, banana, orange

9.

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

a)

for char in string:

b)

for char in string():

c)

for char in range(string):

d)

for i in range(len(string)):

10.

What is the output of the following code? for char in 'Hello': print(char)

a)

H l l o

b)

H l l o

c)

H e l l o

d)

H e l l o

11.

FOR loops are

a)

loops which run an unknown number of times

b)

loops which run for a specific number of times

c)

the same as if statements

d)

not part of programming

12.

A condition is

a)

a statement that is either True or False

b)

a string

c)

an integer

d)

a list

13.

Which kind of loop would be used?


I need a program that will keep letting me add a monthly payment for 12 months.

a)

FOR Loop

b)

WHILE Loop

14.

Which kind of loop would be used?


I need a revision program that will run through revision questions 4 times.

a)

FOR Loop

b)

WHILE Loop

15.

What is a variable?

a)

a type of graphics

b)

A box(memory location) where you store values

c)

Data type

d)

a type of memory

16.

What symbol is used in python to assign values to a variable?

a)

equals =

b)

plus +

c)

minus -

d)

asterisk *

17.

What will be the output?

name = "Dave"

print (name)

a)

(name)

b)

Dave

c)

name

d)

Nothing

18.

A syntax error means your code has a 'grammar' mistake or you used symbols/operations incorrectly

a)

True

b)

False

19.

Which statement correctly assigns the string "Tanner" to the variable name?

a)

name  = print( "Tanner")

b)

input("Tanner")

c)

name= "Tanner"

d)

name = input("Tanner")

20.

What will be the output?

name = "Dave"

print ("name")

a)

Dave

b)

name

c)

"name"

d)

"Dave"

21.

Kind of loop used when you know the number of times you want it to repeat

a)

list

b)

for

c)

while

d)

if

22.

Choose the correct output of the following code.

for i in range(10,15):       

print(i)

a)

10

11

12

13

14

15

b)

10

11

12

13

14

c)

11

12

13

14

15

d)

11

12

13

14

23.

What symbol do you use to make a comment in Python?

a)

+

b)

//

c)

$

d)

#

24.

FOR loops are

a)

loops which run an unknown number of times

b)

loops which run for a specific number of times

c)

the same as if statements

d)

not part of programming

25.

Function range(3) is equivalent to "

a)

range(1,3)

b)

range(0,3)

c)

range(0,3,1)

d)

range(1,3,0)

26.

Which of the following function is used in python for loop to specify the initial, final and increment values?

a)

range

b)

print

c)

format

d)

input

27.

In Python, what are if-statements used for?

a)

Looping

b)

Indexing

c)

Decision Making

d)

Testing

28.

What is the output of this code?


click = True

Like = 0

if click:

Like = Like + 1

print(Like)

a)

0

b)

1

c)

2

d)

3

29.

What is the output of the following code?


What is the output of following code

Temperature = 20

Thermo = 15

if Temperature < 15:

Thermo = Thermo + 5

print(Thermo)

a)

20

b)

15

c)

25

d)

10

30.

What is the output of the following code?


Time = "Day"

Sleepy = False

Pajamas = "Off"

if Time == "Night" and Sleepy == True:

Pajamas = "On"

print(Pajamas)

a)

off

b)

on

c)

False

d)

Day

31.

What will the be the output of the following code?


if (10<0) and (0 <-10):

print(“A”)

else:

print(“B”)

a)

A

b)

B

c)

AB

d)

BA

32.

What is the output of the following code?


a = "b"

if True or True:

a = "a"

print(a * 2)

a)

bb

b)

aa

c)

ab

d)

ba

33.

The result of this program:

Friday = False

if Friday:

print "Jeans day!"

else:

print "Uniform day"

a)

Jeans day

b)

Today is Friday

c)

Uniform day

d)

Not Friday