Python Practice › Loop Programs
Python Loop Programs with Solutions
90 loop programs from counting and digit tricks to star and letter patterns. They practise for, while, range(), break, continue and nested loops; every solution is explained and shows its real output.
Before you start
Read the Loops: for & while lesson first if the topic is new to you.
All programs
- #1Numbers divisible by 7 and multiples of 5
Write a Python program to find the numbers between 1500 and 2700 (both included) that are divisible by 7 and are multiples of 5.Easy - #2Star pattern that grows then shrinks
Write a Python program to print a star pattern that grows from 1 to 5 stars and shrinks back to 1, using nested loops.Easy - #3Build a string character by character
Write a Python program that adds each character of a word from the user to an empty string.Easy - #4Count even and odd numbers
Write a Python program to count the even and odd numbers in a series.Easy - #5Print 0 to 6 except 3 and 6
Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.Easy - #6Fibonacci series up to 20
Write a Python program to print the Fibonacci series from 0 to 20.Easy - #7FizzBuzz from 1 to 30
Write a Python program that prints 1 to 30, but “Fizz” for multiples of 3, “Buzz” for multiples of 5 and “FizzBuzz” for multiples of both.Easy - #8Convert uppercase letters to lowercase
Write a Python program that converts all uppercase letters in a word to lowercase, using a loop.Easy - #9Count letters and digits
Write a Python program that counts the letters and digits in a string.Easy - #10Letter O pattern
Write a Python program to print the letter ‘O’ with stars.Medium - #11Natural numbers 1 to n with while
Write a Python program to print the natural numbers from 1 to n using a while loop.Easy - #12Natural numbers n to 1 with while
Write a Python program to print the natural numbers from n down to 1 using a while loop.Easy - #13Print A to Z with chr()
Write a Python program to print the alphabet from A to Z using a for loop andchr()(chr(65) is ‘A’).Easy - #14Even numbers from 1 to 100
Write a Python program to print all even numbers from 1 to 100.Easy - #15Odd numbers from 1 to 100
Write a Python program to print all odd numbers from 1 to 100.Easy - #16Sum of 1 to n
Write a Python program to find the sum of all natural numbers from 1 to n.Easy - #17Sum of even numbers 1 to n
Write a Python program to find the sum of all even numbers from 1 to n.Easy - #18Sum of odd numbers 1 to n
Write a Python program to find the sum of all odd numbers from 1 to n.Easy - #19Count the digits of a number
Write a Python program to count the digits of a number.Easy - #20First and last digits
Write a Python program to find the first and last digits of a number.Easy - #21Sum of the first and last digits
Write a Python program to find the sum of the first and last digits of a number.Easy - #22Sum of digits
Write a Python program to calculate the sum of the digits of a number.Easy - #23Product of digits
Write a Python program to calculate the product of the digits of a number.Easy - #24Reverse a number
Write a Python program to read a number and print its reverse.Easy - #25Palindrome number with a loop
Write a Python program to check whether a number is a palindrome using a loop.Easy - #26Frequency of each digit
Write a Python program to find how often each digit appears in an integer.Medium - #27Number in words
Write a Python program that prints each digit of a number as a word.Medium - #28Power using a loop
Write a Python program to calculate base raised to a power using a for loop.Easy - #29Factors of a number
Write a Python program to find all the factors of a number.Easy - #30Factorial with a loop
Write a Python program to calculate the factorial of a number using a loop.Easy - #31Prime check with a loop
Write a Python program to check whether a number is prime using a loop.Easy - #32Primes from 1 to n
Write a Python program to print all prime numbers from 1 to n.Easy - #33Sum of primes from 1 to n
Write a Python program to find the sum of all prime numbers from 1 to n.Easy - #34Prime factors
Write a Python program to find all prime factors of a number.Medium - #35Armstrong number with a loop
Write a Python program to check whether a number is an Armstrong number (153 = 1³ + 5³ + 3³) using a loop.Easy - #36Armstrong numbers from 1 to n
Write a Python program to print all Armstrong numbers from 1 to n.Medium - #37Perfect number check
Write a Python program to check whether a number is perfect: the sum of its factors (excluding itself) equals the number, like 28 = 1 + 2 + 4 + 7 + 14.Easy - #38Perfect numbers from 1 to n
Write a Python program to print all perfect numbers from 1 to n.Medium - #39Fibonacci series of n terms
Write a Python program to print the first n terms of the Fibonacci series.Easy - #40Multiplication table
Write a Python program to print the multiplication table of a number.Easy - #41Letter T pattern
Write a Python program to print the letter ‘T’ with stars: two full rows of 9 stars and a 3-star stem in the middle.Medium - #42Number diamond pattern
Write a Python program to print rows of numbers that grow from 1 to 5 numbers (1 to 15) and then shrink, counting back down (14 to 5).Hard - #43Letter A pattern
Write a Python program to print the letter ‘A’ with stars.Medium - #44Star pyramid
Write a Python program to print a pyramid of stars with 5 rows (1, 3, 5, 7, 9 stars).Easy - #45Count even numbers 1 to 100
Write a Python program to count the even numbers from 1 to 100.Easy - #46Count odd numbers 1 to 100
Write a Python program to count the odd numbers from 1 to 100.Easy - #47Keep only the digits of the input
Write a Python program that reads text and adds each digit to an empty list as a number.Easy - #48Characters of a string into a list
Write a Python program that adds every character of an input string to an empty list.Easy - #49Taxi bill by distance
Write a Python program to calculate a bill: first 5 km at Rs 15/km, next 20 km at Rs 12/km, after that Rs 10/km.Medium - #50Electricity bill with slabs
Write a Python program to calculate an electricity bill: first 50 units Rs 0.50, next 100 units Rs 0.75, next 100 units Rs 1.25, above 250 units Rs 1.50, plus a 17% surcharge.Medium - #51Sum of odd numbers 1 to 100
Write a Python program to calculate the sum of all odd numbers from 1 to 100.Easy - #52Numbers from 0 to 100 divisible by 5
Write a Python program to find the numbers from 0 to 100 divisible by 5.Easy - #53Right triangle of stars
Write a Python program to print a right triangle of stars with 5 rows.Easy - #54Inverted right triangle of stars
Write a Python program to print an inverted right triangle of stars with 5 rows.Easy - #55Number triangle 1, 22, 333
Write a Python program to print row i with the number i repeated i times, for i from 1 to 5, using nested loops.Easy - #56Fibonacci numbers up to 55
Write a Python program to print the first 11 Fibonacci numbers (0 to 55).Easy - #57Validate a password
Write a Python program to check a password: at least one lowercase letter, one uppercase letter, one digit, one of $ # @, and 5 to 15 characters long.Medium - #58Does a string contain a digit?
Write a Python program to check whether a string contains a digit.Easy - #59Star triangle up and down
Write a Python program to print stars growing from 1 to 5 and back to 1.Easy - #60Table of 5 with a for loop
Write a Python program to print the multiplication table of 5 with a for loop.Easy - #61First 20 natural numbers
Write a Python program to print the first 20 natural numbers with a for loop, separated by commas.Easy - #62Display numbers from a list
Write a Python program to display the numbers of a list separated by commas.Easy - #63Each character on a new line
Write a Python program to print each character of a string on a new line.Easy - #64Even numbers 1-10 into a list
Write a Python program that adds the even numbers from 1 to 10 into an empty list.Easy - #65Odd numbers 1-10 into a list
Write a Python program that adds the odd numbers from 1 to 10 into an empty list.Easy - #66Print the keys of a dictionary
Write a Python program to print the keys of a dictionary using a loop.Easy - #67Print the values of a dictionary
Write a Python program to print the values of a dictionary, capitalised.Easy - #68Print keys and values
Write a Python program to print the keys and values of a dictionary.Easy - #69First 20 natural numbers with while
Write a Python program to print the first 20 natural numbers using a while loop.Easy - #70Table of 2 with a while loop
Write a Python program to print the table of 2 using a while loop.Easy - #71Sum of the first 10 numbers with while
Write a Python program to find the sum of the first 10 natural numbers using a while loop.Easy - #72Product of the first 10 numbers
Write a Python program to find the product of the first 10 natural numbers.Easy - #731 to 10 except 5 and 6 with while
Write a Python program to print 1 to 10 except 5 and 6 using a while loop.Easy - #74Days of the week except Sunday
Write a Python program to print the days of the week except Sunday using a while loop.Easy - #75Count special characters
Write a Python program to count the special characters (not letters, digits or spaces) in a string.Easy - #76Copy even numbers to a new list
Write a Python program to copy the even numbers from a list into an empty list.Easy - #77Copy odd numbers to a new list
Write a Python program to copy the odd numbers from a list into an empty list.Easy - #78Copy special characters to a new list
Write a Python program to copy the special characters from a list into an empty list.Easy - #79Last element with a while loop
Write a Python program to print the last element of a list using a while loop.Easy - #80Stop at 6 with a while loop
Write a Python program to print 1 to 10 but stop when the number is 6, using a while loop.Easy - #81Stop at 6 with a for loop
Write a Python program to print 1 to 10 but stop when the number is 6, using a for loop.Easy - #82Count characters in a file
Write a Python program to count the characters in a file (letters, digits, punctuation and spaces, not counting newlines).Easy - #83Count digits in a file
Write a Python program to count the digits in a file.Easy - #84Count uppercase letters in a file
Write a Python program to count the uppercase letters in a file.Easy - #85Count special characters in a file
Write a Python program to count the special characters in a file.Easy - #86Sort a list with loops
Write a Python program to sort a list using loops (withoutsort()).Medium - #87Copy and sort descending
Write a Python program to copy elements from one list into an empty list and print them in descending order.Easy - #88Maximum number in a list
Write a Python program to find the largest number in a list using a loop.Easy - #89Uppercase letter diamond
Write a Python program to print the letters A to Y in rows of 1, 2, 3, 4, 5, 4, 3, 2, 1 letters.Medium - #90Lowercase letter diamond
Write a Python program to print the letters a to y in rows of 1, 3, 5, 7, 5, 3, 1 letters.Medium