Python For and While Loop
Python For and While Loop
Intermediate Level
11.Use a for loop with range() to calculate the sum of numbers from 1 to 100.
12.Use a while loop to calculate the factorial of a given number.
13.Write a loop to find the largest number in a list [45, 67, 23, 89, 12].
14.Use a for loop to print a multiplication table of 5.
15.Use a while loop to reverse a given number (e.g., 12345 → 54321).
16.Use nested for loops to print a 5x5 star (*) pattern.
17.Write a while loop to count the number of digits in a given number.
18.Write a for loop to iterate over a dictionary and print keys and values
19.Use a while loop with a break statement to stop when a condition is met.
20.Use a for loop with an else block to print a message when the loop ends normally.
Challenging Level
21.Write a for loop to find all prime numbers between 1 and 50.
22.Write a while loop that generates Fibonacci numbers less than 100.
23.Use a nested loop to print a right-angled triangle pattern of numbers.
24.Use a while loop with continue to skip printing multiples of 3 from 1 to 20.
25.Use a for loop with enumerate() to print index and value for a list.
26.Use a for loop to flatten a nested list [[1,2],[3,4],[5,6]].
27.Use a while loop to simulate a simple login attempt with 3 tries.
28.Write a for loop that uses zip() to iterate over two lists simultaneously.
29.Write a while loop to find the GCD (greatest common divisor) of two numbers.
30.Use a for loop with a list comprehension to create a list of squares from 1 to 10.
Advanced Level
31.Use a for loop to implement bubble sort on a list.
32.Use a while loop to simulate a basic ATM that continues until the balance is zero.
33.Write nested loops to print a pyramid pattern with numbers
34.Write a loop to count vowels and consonants in a string.
35.Use a for loop to read lines from a file and print them.
36.Write a while loop that finds the square root of a number using the
Newton-Raphson method.
37.Use nested for loops to generate all pairs (i, j) where i and j are from 1 to 3
38.Use a for loop to check if a string is a palindrome.
39.Write a while loop to simulate rolling a dice until you get a six.
40.Combine for and while loops to solve a small maze represented as a 2D grid.