wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

For and While Loops in Python

Total questions: 16

Worksheet time: 16mins

Name
Class
Date
1.

The program above

a)

prints all the odd numbers between 1 and 20

b)

prints all even numbers between 1 and 20

c)

print all the odd numbers between 1 and 21

d)

prints all the even numbers between 1 and 21

2.

The equivalent of the above for loop using the while syntax would be

a)
b)
c)
d)
3.

The above program prints

a)

Numbers 1 to 10 in reverse order

b)

Numbers 1 to 11 in reverse order

c)

Numbers 1 to 10

d)

Numbers 1 to 11

4.

Look at the following code, what will it generate

a)

1,2,3,4,5,6,7,8,9,10,11,12

b)

0,1,2,3,4,5,6,7,8,9,10,11,12

c)

1,2,3,4,5,6,7,8,9,10,11,12,13

d)

0,1,2,3,4,5,6,7,8,9,10,11,12,13

5.
a)
b)
c)
d)

1,2,3,4

6.

What best describes what is printed by the following code?

a)

Prints the numbers 0 through 6

b)

Prints

h

e

l

l

o

w

c)

Prints the phrase "hello world!" 7 times

d)

Prints "hello w" 7 times

7.

What best describes what is printed by the following code?

a)

Prints the numbers 0 through 6

b)

Prints

h

e

l

l

o

w

c)

Prints the phrase "hello world!" 7 times

d)

Prints "hello w" 7 times

8.

What best describes what is printed by the following code?

a)

Prints the numbers 0 through 6

b)

Prints

h

e

l

l

o

w

c)

Prints the phrase "hello world!" 7 times

d)

Prints "hello w" 7 times

9.
a)
Welcome
b)
Try again!
c)
Error
d)
Enter your password
10.

In a Python program, what would be the ending value of total in the WHILE LOOP code?

a)

0

b)

5

c)

2

d)

20

11.

In a Python program, what would be the ending value for y in the WHILE LOOP code?

a)

5

b)

51

c)

10

d)

3

12.

a)

5

4

3

2

1

b)

4

3

2

1

0

c)

0

d)

Infinite loop

13.

What is the value of c after the loop finishes?

a)

1

b)

2

c)

3

d)

0

14.

a)

0

b)

4

3

2

1

0

c)

5

d)

4 3 2 1 0

15.

What is the value of nums after the code is executed?

a)

[0, 5, 2]

b)

[0, 2, 4]

c)

[1, 3, 5]

d)

[0, 5]

e)

[2, 3, 4]

16.

a)

5

4

3

2

1

b)

4

3

2

1

0

c)

0

d)

Infinite loop