To decide or to control the flow of a program, we have branching and Looping techniques in Python. To perform decision making, we use the **if-else** statement in Python. To iterate over a sequence of elements we use `for`

loop, and when we want to iterate a block of code repeatedly as long as the condition is true we use the `while`

loop.

We often use a loop, and if-else statement in our program, so a good understanding of it is necessary. This Python loop exercise aims to help Python developers to **learn and practice if-else conditions, for loop, range() function, and while loop. **All questions are tested on **Python 3.**

**Further Reading**:

- Explore All Python Exercises and Python Quizzes to practice Python
- Also, try to solve a Python flow control and loop Quiz

**What included in this Python loop exercise?**

The exercise contains **18** questions and solutions provided for each question. This loop **coding** exercise is nothing but Python for loop and while loop assignments to solve, where you can solve and practice different looping techniques programs, questions, problems, and challenges.

This Python loop exercise covers questions on the following **topics**:

- Python for loop and while loop
- Python nested if-else and nested loop
- Break, continue and pass statement

When you complete each question, you get more familiar with the if-else conditions, for loop, and while loop. Let us know if you have any alternative solutions. It will help other developers.

**Use** Online Code Editor **to solve exercise questions**.

#### Exercise Question 1: Print First 10 natural numbers using while loop

**Expected output:**

0 1 2 3 4 5 6 7 8 9 10

#### Exercise Question 2: Print the following pattern

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

#### Exercise Question 3: Accept number from user and calculate the sum of all number between 1 and given number

For example user given **10** so the output should be **55**

#### Exercise Question 4: Print multiplication table of given number

For example `num = 2`

so the output should be

2 4 6 8 10 12 14 16 18 20

#### Exercise Question 5: Given a list iterate it and display numbers which are divisible by 5 and if you find number greater than 150 stop the loop iteration

`list1 = [12, 15, 32, 42, 55, 75, 122, 132, 150, 180, 200]`

**Expected output:**

15 55 75 150

#### Exercise Question 6: Given a number count the total number of digits in a number

For example, the number is **75869**, so the output should be **5**.

#### Exercise Question 7: Print the following pattern using for loop

5 4 3 2 1 4 3 2 1 3 2 1 2 1 1

#### Exercise Question 8: Reverse the following list using for loop

`list1 = [10, 20, 30, 40, 50]`

**Expected output:**

50 40 30 20 10

#### Exercise Question 9: Display -10 to -1 using for loop

**Expected output:**

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1

#### Exercise Question 10: Display a message “Done” after successful execution of for loop

For example, the following loop will execute without any error.

for i in range(5): print(i)

So the** Expected output should be:**

0 1 2 3 4 Done!

#### Exercise Question 11: Python program to display all the prime numbers within a range

*Note*: A Prime Number is a whole number that cannot be made by multiplying other whole numbers

**Examples**:

- 6 is not a Prime Number because it can be made by 2×3 = 6
- 37 is a Prime Number because no other whole numbers multiply together to make it.

**Given**:

```
start = 25
end = 50
```

**Expected output:**

Prime numbers between 25 and 50 are: 29 31 37 41 43 47

#### Exercise Question 12: Display Fibonacci series up to 10 terms

**Expected output:**

Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34

#### Exercise Question 13: Write a loop to find the factorial of any number

The factorial (symbol: !) means to multiply all whole numbers from our chosen number down to 1.

**For example**: calculate the factorial of 5

5! = 5 × 4 × 3 × 2 × 1 = 120

**Expected output:**

120

#### Exercise Question 14: Reverse a given integer number

**Given**:

76542

**Expected output:**

24567

#### Exercise Question 15: Use a loop to display elements from a given list which are present at even positions

**Given:**

`my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]`

**Expected output:**

20 40 60 80 100

#### Exercise Question 16: Display the cube of the number up to a given integer

**Given**:

`input_number = 6`

**Expected output:**

Current Number is : 1 and the cube is 1 Current Number is : 2 and the cube is 8 Current Number is : 3 and the cube is 27 Current Number is : 4 and the cube is 64 Current Number is : 5 and the cube is 125 Current Number is : 6 and the cube is 216

#### Exercise Question 17: Find the sum of the series 2 +22 + 222 + 2222 + .. n terms

**Given**:

`number_of_terms = 5`

So series will become **2 + 22 + 222 + 2222 + 22222**

**Expected output:**

24690

#### Exercise Question 18: Print the following pattern

* * * * * * * * * * * * * * * * * * * * * * * * *