## Python Loop Exercises with Solution – for loop(), while loop()

Python Loop Exercises: For loop() and while loop() is used to iterate over each element or data depending upon the condition satisfied. While on the other side to control the flow of a program, we have control flow statements i.e. if, if-else statements in Python.

In most cases, we use loops and control flow statements together in our program. Looping and control flow statements are the backbone of any programming language. So, it is necessary to have a good understanding and command over these concepts.

The main idea behind solving these questions is to make your concept more clear and improve logical thinking of how to approach a problem. We are going to cover conditional statements i.e if, if-else, for loop(), range(), while loop() etc.

For reference ### Loop Exercises in Python:

We will solve 15 loop programming exercises in python with a solution & detailed code explanation.

### Exercise 1: Write a program in Python to display the Factorial of a number.

Hint 1
Input : 5

Expected output
Factorial is 120

Hint 1
Input : 6

Expected output
Factorial is 720

```# Factorial of a number
n = int(input("Enter number: ")) # 5
if(n == 0 or n < 0):
print("Value of n should be greater than 1")
else:
fact = 1
while(n):
fact *= n
n = n-1
print(f"Factorial is {fact} ")

# output
# Factorial is 120```

### Exercise 2: Write a program in Python to reverse a word.

Hint 1
Input a word to reverse : python

Expected output
Result: nohtyp

```# WAP to reverse a string
str = input("Input a word to reverse: ")

for i in range(len(str) - 1, -1, -1):
print(str[i], end="")
print("\n")

# output
# nohtyp```

### Exercise 3: Write a Python program to reverse a number.

Hint 1
Input a number to reverse : 43521

Expected output
Result: 12534

```# Reverse a number
n = int(input("Enter number: "))
rev = 0

while(n != 0):
rem = n % 10
rev = rev * 10 + rem
n = n // 10

print(rev)

# output
# 12534```

### Exercise 4: Write a program to print n natural number in descending order using a while loop.

Hint 1
Enter a range : 10

Expected output
Result: 10  9  8  7  6  5  4  3  2  1

```# N natural number
n = int(input("Enter range: ")) #10
while(n!=0):
print(n, end=" ")
n = n - 1

# output
# 10 9 8 7 6 5 4 3 2 1```

### Exercise 5: Write a program to display the first 7 multiples of 7.

Hint 1
Use if and for loop with break

Expected output
Result: 0  7  14  21  28  35  42  49

```# First 7 numbers multiple of 7
count = 0
for i in range(200):
if i%7 == 0:
print(i,end=" ")
count = count+1
if count == 8:
break

# output
# 0 7 14 21 28 35 42 49```

### Exercise 6: Write a program that appends the square of each number to a new list.

Hint 1
Given x = [2,3,4,5,6,7,8]

Expected output
Result: [4, 9, 16, 25, 36, 49, 64]

```#Appending square to a new list
x = [2,3,4,5,6,7,8]
z = []
for i in range(len(x)):
z.append(x[i]**2)
print("Result: ",z)

# output
# Result:  [4, 9, 16, 25, 36, 49, 64]```

### Exercise 7: WAP to separate positive and negative number from a list.

Hint 1
Given x = [23, 4, -6, 23, -9, 21, 3, -45, -8]

Expected output
Result:
Positive: [23, 4, 23, 21, 3] Negative: [-6, -45, -9, -8]

```# Program to separate +ve and -ve
x = [23,4,-6,23,-9,21,3,-45,-8]
pos = []
neg = []
for i in range(len(x)):
if x[i] < 0:
neg.append(x[i])
else:
pos.append(x[i])
print("Positive numbers are: ",pos)
print("Negative numbers are: ",neg)

# output
# Positive numbers are:  [23, 4, 23, 21, 3]
# Negative numbers are:  [-6, -9, -45, -8]```

### Exercise 8: Write a program that appends the type of elements from a list.

Hint 1
Given x = [23, ‘Python’, 23.98]

Expected output
Result:
[<class ‘int’>,<class ‘str’>,<class ‘float’>]

```# Append a list with types of elements
n = [23, 'Python',23.98]
x = []
for i in range(len(n)):
x.append(type(n[i]))
print(n)
print(x)

# output
# [23, 'Python', 23.98]
#[<class 'int'>,<class 'str'>,<class 'float'>]```

### Exercise 9: Write a program to filter even and odd number from a list.

Hint 1
Given x = [10, 23, 24, 35, 65, 78, 90]

Expected output
Even numbers: [10, 24, 78, 90] Odd numbers: [23, 35, 65]

```# WAP to filter even and odd number form a list
x = [10,23,24,35,65,78,90]
eve = []
odd = []
for i in range(len(x)):
if x[i] % 2 == 0:
eve.append(x[i])
else:
odd.append(x[i])
print("Even numbers are: ",eve)
print("Odd numbers are: ",odd)

# output
# Even numbers are:  [10, 24, 78, 90]
# Odd numbers are:  [23, 35, 65]```

### Exercise 10: Write a program to fetch only even values from a dictionary.

Hint 1
dic = {‘val1’:10, ‘val2’:20, ‘val3’:23, ‘val4’:22 }

Expected output
Result : 10 20 22

```# Fetch only even values form a dictionary and append to a list
dic = {'val1':10, 'val2':20, 'val3':23, 'val4':22 }
for i in dic.values():
if i % 2 ==0:
print(i,end=" ")
else:
pass

# output
# 10  20  22```