Python For Loops Practice
Python For Loops Practice
Question 1: Write a `for` loop that prints the numbers from 1 to 5 (inclusive).
print(i)
Question 2: Write a `for` loop that prints all even numbers from 2 to 10 (inclusive).
print(i)
Question 3: Write a `for` loop that calculates the sum of all numbers from 1 to 100 (inclusive).
total_sum = 0
total_sum += i
print(total_sum)
Question 4: Write a `for` loop that prints the multiplication table for the number 5 (from 1 to
10).
Question 5: Write a `for` loop that finds and prints all the prime numbers between 1 and 50.
if i % j == 0:
is_prime = False
break
if is_prime:
print(i)
Question 6: Write a `for` loop that prints a list of all numbers from 1 to 20, with specific rules
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
Question 7: Write a `for` loop that prints a list of all numbers from 1 to 100, with specific rules
if i % 3 == 0 and i % 5 == 0:
print("FooBar")
elif i % 3 == 0:
print("Foo")
elif i % 5 == 0:
print("Bar")
else:
print(i)
Question 8: Write a `for` loop that prints a multiplication table for numbers 1 to 5.
print()
number = 6
factorial = 1
factorial *= i
Question 10: Write a `for` loop that generates and prints a list of Fibonacci numbers up to a
a, b = 0, 1
while a <= 100:
print(a)
a, b = b, a + b