25 Python Loop Coding Questions
25 Python Loop Coding Questions along with Explanations for each. Let's get started ↓
for num in range(1, 11):
print(num)
Explanation: The for loop iterates over the range(1, 11) which generates numbers from 1 to 10 (inclusive) and prints each number.
sum_numbers = 0
for num in range(1, 11):
sum_numbers += num
print(sum_numbers)
Explanation: The for loop iterates over the range(1, 11) and adds each number to the sum_numbers variable.
my_list = [1, 2, 3, 4, 5]
for element in my_list:
print(element)
Explanation: The for loop iterates over each element in the my_list and prints them one by one.
my_list = [2, 3, 4, 5]
product = 1
for num in my_list:
product *= num
print(product)
Explanation: The for loop iterates over each element in the my_list and multiplies them together, updating the product variable.
for num in range(2, 11, 2):
print(num)
Explanation: The for loop iterates over the range(2, 11, 2) which generates even numbers from 2 to 10 (inclusive) and prints each even number.
for num in range(10, 0, -1):
print(num)
Explanation: The for loop iterates over the range(10, 0, -1) which generates numbers in reverse from 10 to 1 (inclusive) and prints each number.
my_string = "Hello"
for char in my_string:
print(char)
Explanation: The for loop iterates over each character in the my_string and prints them one by one.
my_list = [3, 9, 1, 6, 2, 8]
largest = my_list[0]
for num in my_list:
if num > largest:
largest = num
print(largest)
Explanation: The for loop iterates over each element in the my_list, compares it with the current value of largest, and updates largest if the current element is larger.
my_list = [4, 7, 9, 2, 5]
total = 0
for num in my_list:
total += num
average = total / len(my_list)
print(average)
Explanation: The for loop iterates over each element in the my_list and calculates the sum of all elements. The average is then calculated by dividing the sum by the number of elements in the list.
my_string = "Hello World"
for char in my_string:
if char.isupper():
print(char)
Explanation: The for loop iterates over each character in the my_string and checks if it is uppercase using the isupper() method. If it's uppercase, it is printed.
my_string = "Hello World"
vowels = "AEIOUaeiou"
count = 0
for char in my_string:
if char in vowels:
count += 1
print(count)
Explanation: The for loop iterates over each character in the my_string and checks if it is a vowel by comparing it with the characters in the vowels string.
for i in range(5):
for j in range(i + 1):
print("*", end="")
print()
Explanation: The outer for loop iterates from 0 to 4, and the inner for loop prints stars in increasing order for each iteration of the outer loop.
Recommended by LinkedIn
num = 5
factorial = 1
while num > 0:
factorial *= num
num -= 1
print(factorial)
Explanation: The while loop multiplies the factorial by num and decrements num by 1 until num becomes 0.
my_list = [3, 8, 2, 7, 4]
target = 7
index = 0
while index < len(my_list):
if my_list[index] == target:
break
index += 1
else:
index = -1
print(index)
Explanation: The while loop iterates through the my_list and checks if each element is equal to the target. If found, it breaks out of the loop, otherwise, it continues. The else block is executed if the loop completes without finding the target.
num = 1
sum_numbers = 0
while num <= 100:
sum_numbers += num
num += 1
print(sum_numbers)
Explanation: The while loop iterates from 1 to 100, adding each number to the sum_numbers variable.
for num in range(2, 51):
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
Explanation: The outer for loop iterates from 2 to 50, and the inner for loop checks for divisibility of num by numbers from 2 to num - 1. If it is divisible by any number, the inner loop is broken, otherwise, the else block is executed, and the number is printed.
for num in range(1, 21):
if num % 3 == 0 or num % 5 == 0:
print(num)
Explanation: The for loop iterates from 1 to 20 and checks if each number is divisible by 3 or 5. If true, it prints the number.
squares = [num**2 for num in range(1, 6)]
print(squares)
Explanation: The list comprehension generates squares of numbers from 1 to 5 and stores them in the squares list.
a, b = 0, 1
count = 0
while count < 10:
print(a, end=" ")
a, b = b, a + b
count += 1
Explanation: The while loop calculates and prints the Fibonacci sequence by updating a and b variables in each iteration.
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
common_elements = []
for element in list1:
if element in list2:
common_elements.append(element)
print(common_elements)
Explanation: The for loop iterates over elements in list1 and checks if they are also present in list2. If so, it adds them to the common_elements list.
my_list = [1, 4, 6, 8, 10, -3, 5, 7]
index = 0
while my_list[index] >= 0:
print(my_list[index])
index += 1
Explanation: The while loop iterates through the my_list until a negative number is encountered, printing each non-negative number.
for num in range(1, 6):
if num == 3:
continue
print(num)
Explanation: The for loop iterates from 1 to 5 and skips the number 3 using the continue statement.
for num in range(1, 11):
print(num)
if num % 4 == 0:
break
Explanation: The for loop iterates from 1 to 10 and prints each number. If the number is divisible by 4, the loop is terminated using the break statement.
for num in range(1, 11):
if num % 2 == 0:
continue
print(num)
else:
print("Loop completed successfully!")
Explanation: The for loop iterates from 1 to 10 and skips even numbers using the continue statement. The else clause is executed after the loop completes successfully.
for num in range(1, 11):
if num % 2 == 0:
break
print(num)
else:
print("Loop completed successfully!")
Explanation: The for loop iterates from 1 to 10 and breaks the loop if it encounters an even number.
Attended Guru Gobind Singh Govt Polytechnic Education Society Cheeka (Kaithal)
6moIt's a great collection of questions that help me to understand the loops. Thank you
learning python
11moi am from class 9 it is helpful for me
Student at University of Mumbai
1yits really help full thank you 🙂