25 Python Loop Coding Questions

25 Python Loop Coding Questions

25 Python Loop Coding Questions along with Explanations for each. Let's get started ↓

  1. Print numbers from 1 to 10 using a for loop:

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.

  1. Calculate the sum of numbers from 1 to 10 using a for loop:

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.

  1. Print the elements of a list using a for loop:

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.

  1. Calculate the product of elements in a list using a for loop:

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.

  1. Print even numbers from 1 to 10 using a for loop:

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.

  1. Print numbers in reverse from 10 to 1 using a for loop:

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.

  1. Print characters of a string using a for loop:

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.

  1. Find the largest number in a list using a for loop:

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.

  1. Find the average of numbers in a list using a for loop:

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.

  1. Print all uppercase letters in a string using a for loop:

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.

  1. Count the number of vowels in a string using a for loop:

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.

  1. Print a pattern of stars using nested for loops:

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.

  1. Calculate factorial of a number using a while loop:

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.

  1. Find the first occurrence of a number in a list using a while loop:

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.

  1. Calculate the sum of numbers from 1 to 100 using a while loop:

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.

  1. Find all prime numbers between 1 and 50 using nested for and if:

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.

  1. Print numbers divisible by 3 or 5 from 1 to 20 using a for loop:

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.

  1. Print a list of squares of numbers from 1 to 5 using a list comprehension:

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.

  1. Print the Fibonacci sequence up to the 10th term using a while loop:

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.

  1. Find the common elements in two lists using a for loop:

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.

  1. Print numbers in a list until a negative number is encountered using a while loop:

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.

  1. Print numbers from 1 to 5, except 3 using a for loop and continue statement:

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.

  1. Print numbers from 1 to 10. If a number is divisible by 4, stop the loop using a for loop and break 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.

  1. Print numbers from 1 to 10. If a number is even, skip it using a for loop and else clause:

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.

  1. Print numbers from 1 to 10. If a number is even, break the loop using a for loop and else clause:

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.

Anurag Jelwal

Attended Guru Gobind Singh Govt Polytechnic Education Society Cheeka (Kaithal)

6mo

It's a great collection of questions that help me to understand the loops. Thank you

i am from class 9 it is helpful for me

Vishal Mishra

Student at University of Mumbai

1y

its really help full thank you 🙂

To view or add a comment, sign in

More articles by Mrityunjay Pathak

  • Bias and Variance and Its Trade Off

    There are various ways to evaluate a machine-learning model. Bias and Variance are one such way to help us in parameter…

  • Machine Learning Mathematics🔣

    Machine Learning is the field of study that gives computers the capability to learn without being explicitly…

  • How to Modify your GitHub Profile Readme File as your Portfolio

    What if you don't have a personal portfolio website? No worries! You can transform your GitHub README.md into a…

    4 Comments
  • Data Science Resources

    Are you starting your journey into the world of Data Science? Here's a curated list of top resources to master various…

  • 25 Python Sets Questions with Solution

    25 Python Sets Coding Questions along with Explanations for each. Let's get started ↓ Question 1: Write a Python…

  • 25 Python Tuple Questions with Solution

    25 Python Tuple Coding Questions along with Explanations for each. Let's get started ↓ Question 1: Find the length of a…

  • 25 Python Dictionary Questions and Solutions

    25 Python Dictionary Coding Questions along with Explanations for each. Let's get started ↓ Question 1: Create an empty…

  • 25 Python List Questions with Solution

    25 Python List Coding Questions along with Explanations for each. Let's get started ↓ Question: Given a list nums, find…

    2 Comments
  • 25 Python String Questions with Solution

    25 Python Strings Coding Questions along with Explanations for each. Let's get started ↓ Write a Python program to…

    3 Comments
  • 25 Basic Python I/O Coding Questions

    25 Basic Python I/O Coding Questions along with Explanations for each. Let's get started ↓ 1.

    2 Comments

Insights from the community

Others also viewed

Explore topics