0% found this document useful (0 votes)
9 views4 pages

Python For Loops Practice

The document contains a series of practice questions and answers focused on Python 'for' loops. Each question provides a specific task, such as printing numbers, calculating sums, generating multiplication tables, and identifying prime numbers. The answers include code snippets demonstrating the use of 'for' loops to accomplish these tasks.

Uploaded by

antoniopatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Python For Loops Practice

The document contains a series of practice questions and answers focused on Python 'for' loops. Each question provides a specific task, such as printing numbers, calculating sums, generating multiplication tables, and identifying prime numbers. The answers include code snippets demonstrating the use of 'for' loops to accomplish these tasks.

Uploaded by

antoniopatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python 'for' Loops Practice Questions and Answers

Question 1: Write a `for` loop that prints the numbers from 1 to 5 (inclusive).

for i in range(1, 6):

print(i)

Question 2: Write a `for` loop that prints all even numbers from 2 to 10 (inclusive).

for i in range(2, 11, 2):

print(i)

Question 3: Write a `for` loop that calculates the sum of all numbers from 1 to 100 (inclusive).

total_sum = 0

for i in range(1, 101):

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).

for i in range(1, 11):

print(f"5 x {i} = {5 * i}")

Question 5: Write a `for` loop that finds and prints all the prime numbers between 1 and 50.

for i in range(2, 51):


is_prime = True

for j in range(2, i):

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

for divisibility by 3 and 5.

for i in range(1, 21):

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

for multiples of 3 and 5.

for i in range(1, 101):

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.

for i in range(1, 6):

for j in range(1, 6):

print(f"{i} x {j} = {i * j}", end="\t")

print()

Question 9: Write a `for` loop that calculates the factorial of a number.

number = 6

factorial = 1

for i in range(1, number + 1):

factorial *= i

print(f"The factorial of {number} is {factorial}")

Question 10: Write a `for` loop that generates and prints a list of Fibonacci numbers up to a

certain limit (100).

a, b = 0, 1
while a <= 100:

print(a)

a, b = b, a + b

You might also like