Test your understanding of Python loops, conditionals, and control flow with these exercises. Calculate the output or rewrite code as requested.
Easy Medium Hard
Question 4 Easy
How many asterisks does the following code fragment print?
a = 0
while a < 100:
print('*', end='')
a += 1
print()Question 5 Easy
How many asterisks does the following code fragment print?
a = 0
while a < 100:
print('*', end='')
print()Question 6 Easy
How many asterisks does the following code fragment print?
a = 0
while a > 100:
print('*', end='')
a += 1
print()Question 7 Medium
How many asterisks does the following code fragment print?
a = 0
while a < 100:
b = 0
while b < 55:
print('*', end='')
b += 1
print()
a += 1Question 8 Medium
How many asterisks does the following code fragment print?
a = 0
while a < 100:
if a % 5 == 0:
print('*', end='')
a += 1
print()Question 9 Hard
How many asterisks does the following code fragment print?
a = 0
while a < 100:
b = 0
while b < 40:
if (a + b) % 2 == 0:
print('*', end='')
b += 1
print()
a += 1Question 10 Hard
How many asterisks does the following code fragment print?
a = 0
while a < 100:
b = 0
while b < 100:
c = 0
while c < 100:
print('*', end='')
c += 1
b += 1
a += 1
print()Question 11 Easy
How many asterisks does the following code fragment print?
for a in range(100):
print('*', end='')
print()Question 12 Medium
How many asterisks does the following code fragment print?
for a in range(20, 100, 5):
print('*', end='')
print()Question 13 Medium
How many asterisks does the following code fragment print?
for a in range(100, 0, -2):
print('*', end='')
print()Question 14 Easy
How many asterisks does the following code fragment print?
for a in range(1, 1):
print('*', end='')
print()Question 15 Medium
How many asterisks does the following code fragment print?
for a in range(-100, 100):
print('*', end='')
print()Question 16 Medium
How many asterisks does the following code fragment print?
for a in range(-100, 100, 10):
print('*', end='')
print()Question 17 Medium
Rewrite the code in the previous question so it uses a while instead of a for. Your code should behave identically.
# Original Code:
for a in range(-100, 100, 10):
print('*', end='')
print()Question 18 Hard
How many asterisks does the following code fragment print?
for a in range(-100, 100, -10):
print('*', end='')
print()Question 19 Hard
How many asterisks does the following code fragment print?
for a in range(100, -100, 10):
print('*', end='')
print()Question 20 Hard
How many asterisks does the following code fragment print?
for a in range(100, -100, -10):
print('*', end='')
print()Question 21 Easy
What is printed by the following code fragment?
a = 0
while a < 100:
print(a)
a += 1
print()Question 22 Medium
Rewrite the code in the previous question so it uses a for instead of a while. Your code should behave identically.
# Original code:
a = 0
while a < 100:
print(a)
a += 1
print()Question 23 Easy
What is printed by the following code fragment?
a = 0
while a > 100:
print(a)
a += 1
print()Question 24 Medium
Rewrite the following code fragment using a break statement and eliminating the done variable. Your code should behave identically to this code fragment.
done = False
n, m = 0, 100
while not done and n != m:
n = eval(input())
if n < 0:
done = True
print("n =", n)Question 25 Medium
Rewrite the following code fragment so it does not use a break statement. Your code should behave identically to this code fragment.
n = 0
while True:
n = int(input("Enter a number: "))
if n == 0:
break
print("You entered:", n)Question 26 Hard
Rewrite the following code fragment so it eliminates the continue statement. Your new code's logic should be simpler than the logic of this fragment.
x = 100
while x > 0:
y = int(input("Enter y: "))
if y == 25:
x += 1
continue
x = int(input("Enter x: "))
print('x =', x)Question 27 Medium
What is printed by the following code fragment?
a = 0
while a < 100:
print(a, end='')
a += 1
print()Question 28 Hard
What is the output of the following nested loop?
for i in range(3):
for j in range(3):
if i == j:
continue
print(f"({i}, {j})")Question 29 Hard
What does this code print?
i = 5
while i > 0:
j = i
while j > 0:
print("*", end="")
j -= 1
print()
i -= 1Question 30 Hard
Write a program that prints a multiplication table from 1 to 10 using nested loops.