0% found this document useful (0 votes)
5 views6 pages

Complete Python For Loops Practice

The document contains practice exercises for Python 'for' loops, including tasks such as printing numbers, calculating sums, generating multiplication tables, finding prime numbers, and implementing FizzBuzz. Each question includes code examples, comments explaining the logic, and the expected output. It serves as a comprehensive guide for learning and practicing Python loop constructs.

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)
5 views6 pages

Complete Python For Loops Practice

The document contains practice exercises for Python 'for' loops, including tasks such as printing numbers, calculating sums, generating multiplication tables, finding prime numbers, and implementing FizzBuzz. Each question includes code examples, comments explaining the logic, and the expected output. It serves as a comprehensive guide for learning and practicing Python loop constructs.

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/ 6

Python 'for' Loops Practice with Comments and Outputs

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

for i in range(1, 6):

print(i)

Comments: This loop iterates from 1 to 5 and prints each number in the range.

Output:

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)

Comments: Using a step of 2 ensures only even numbers are printed.

Output:

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

Comments: The loop adds each number in the range to the total_sum variable.

Output:

5050

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}")

Comments: The loop uses formatted strings to generate the multiplication table.

Output:

5 x 1 = 5

5 x 2 = 10

...

5 x 10 = 50

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)

Comments: This nested loop checks divisibility of each number to determine if it's prime.

Output:

11

... up to 47

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)
Comments: The loop uses conditional statements to print special words for multiples.

Output:

Fizz

Buzz

... up to 20

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)

Comments: Similar to the FizzBuzz problem but uses 'Foo' and 'Bar' for custom outputs.

Output:

Foo
4

Bar

... up to 100

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

Comments: This nested loop generates a full multiplication table for numbers 1 to 5.

Output:

1 x 1 = 1 1 x 2 = 2 ... 1 x 5 = 5

2 x 1 = 2 2 x 2 = 4 ... 2 x 5 = 10

...

5 x 1 = 5 ... 5 x 5 = 25

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}")


Comments: The loop multiplies all integers from 1 to the specified number.

Output:

720

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

Comments: The loop generates Fibonacci numbers by updating two variables iteratively.

Output:

... up to 89

You might also like