Loop Programs
for and while. Digits, patterns, Fibonacci. You will get tired of range() and that is the point.
25 programs · open one, then the next
The matching chapter is Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Every topic is listed on Python exercises.
Start with Print Numbers 1–100- 01Print Numbers 1–100BeginnerCount from 1 through 100 with a Python for loop. A first range exercise explains the excluded stop, required indentation, and the sample’s abbreviated output.Open example →py# Program to print numbers from 1 to 100 for i in range(1, 101): print(i)
- 02Print Even Numbers in RangeBeginnerGenerate positive even integers directly instead of filtering every number. A beginner Python range exercise explains a step of two and an inclusive input limit.Open example →py# Program to print even numbers up to N n = int(input("Enter upper limit: "))
- 03Print Odd Numbers in RangeBeginnerStart at one and move by two to print odd Python integers. New programmers can predict the final row for even or odd limits and spot an empty forward range.Open example →py# Program to print odd numbers up to N n = int(input("Enter upper limit: "))
- 04Sum of DigitsBeginnerPeel digits off 1234 and add them with a Python while loop. Beginner steps connect % 10, // 10, abs, and an accumulator while preserving the original number.Open example →py# Program to find the sum of digits of a number num = int(input("Enter an integer: "))
- 05Reverse Digits of a NumberBeginnerRebuild 1234 as 4321 with Python arithmetic rather than string slicing. First-year traces cover place shifting, negative signs, and disappearing leading zeroes.Open example →py# Program to reverse digits of a number num = int(input("Enter an integer: "))
- 06Calculate Factorial (Loop)BeginnerMultiply 1 through N to calculate a Python factorial. A beginner loop lesson explains the starting value one, 0!, negative-input checks, and growing integers.Open example →py# Program to calculate factorial using a loop n = int(input("Enter a non-negative integer: "))
- 07Fibonacci SeriesBeginnerKeep just two values to print N Fibonacci terms in Python. Beginners trace tuple unpacking and learn why a term count is different from a numeric upper limit.Open example →py# Program to print Fibonacci series up to N terms n = int(input("Enter number of terms: "))
- 08Palindrome Number CheckBeginnerReverse a Python integer and compare it with the original to test a palindrome. A first while-loop application includes zero and an explicit negative-sign rule.Open example →py# Program to check palindrome number num = int(input("Enter an integer: "))
- 09Count Digits in a NumberBeginnerCount decimal digits by shrinking a Python integer with // 10. This beginner while-loop exercise ignores the sign and gives zero its proper one-digit count.Open example →py# Program to count digits in a number num = int(input("Enter an integer: "))
- 10GCD of Two Numbers (Loop)BeginnerReduce 54 and 24 to their GCD with Python’s Euclidean loop. A beginner pair-by-pair trace explains remainder updates, tuple unpacking, zero inputs, and signs.Open example →py# Program to find GCD of two numbers using Euclidean algorithm a = int(input("Enter first integer: ")) b = int(input("Enter second integer: "))
- 11LCM of Two Numbers (Loop)BeginnerUse a Euclidean GCD to calculate a Python least common multiple. Beginners preserve original inputs, apply the product identity, and guard the both-zero case.Open example →py# Program to find LCM of two numbers using GCD a = int(input("Enter first integer: ")) b = int(input("Enter second integer: "))
- 12Print Prime Numbers in RangeBeginnerSearch a Python interval for primes using a fresh divisor test per candidate. First-year loops practice covers square-root bounds, flags, continue, and break.Open example →py# Program to print prime numbers in a range start = int(input("Enter start of range: ")) end = int(input("Enter end of range: "))
- 13Count Prime Numbers in RangeBeginnerCount primes without building a result list in Python. Beginners separate the total from each candidate’s flag and trace inclusive endpoints and empty intervals.Open example →py# Program to count prime numbers in a range start = int(input("Enter start of range: ")) end = int(input("Enter end of range: "))
- 14Check Composite NumberBeginnerOne proper factor proves a Python integer composite. A beginner search explains the square-root boundary, early stopping, and why one is not a prime fallback.Open example →py# Program to check if a number is composite n = int(input("Enter an integer: "))
- 15Power of 2 CheckerBeginnerKeep dividing a Python integer by two until it is odd. New programmers test the remaining value, include 2⁰, and see why zero needs a guard before the loop.Open example →py# Program to check if a number is a power of 2 n = int(input("Enter a positive integer: "))
- 16Print Number PatternBeginnerPrint rows 1, then 1 2, then 1 2 3 with Python nested loops. Beginner steps separate row and column work and show exactly where the newline belongs.Open example →py# Program to print a right-angled number triangle rows = int(input("Enter number of rows: "))
- 17Print Star PatternBeginnerDraw a growing Python star triangle without an inner loop. A first pattern exercise uses string repetition and connects each row index with its visible width.Open example →py# Program to print a right-angled star pattern rows = int(input("Enter number of rows: "))
- 18Armstrong Numbers in RangeBeginnerSearch a Python interval for powered-digit matches, including every single digit. Beginners reset the sum and digit count for each Armstrong-number candidate.Open example →py# Program to print Armstrong numbers in a range start = int(input("Enter start of range: ")) end = int(input("Enter end of range: "))
- 19Strong Numbers in RangeBeginnerFind 1, 2, and 145 in a Python factorial-digit search. First-year nested-loop practice resets each candidate and avoids falsely including zero in the results.Open example →py# Program to print strong numbers in a range import math
- 20Sum of Series 1/1 + 1/2 + … + 1/NBeginnerAdd 1/1 through 1/N with Python true division and a running float total. Beginners trace reciprocal terms and see why floor division would destroy the series.Open example →py# Program to compute sum of series 1/1 + 1/2 + ... + 1/N n = int(input("Enter a positive integer N: "))
- 21Print Multiplication SeriesBeginnerCollect Python multiplication terms and print them on one line. A beginner list-and-join exercise keeps separators between terms instead of after the last one.Open example →py# Program to print multiplication series for a number num = int(input("Enter a number: ")) limit = int(input("Enter limit: "))
- 22Print Pyramid PatternBeginnerCenter Python star rows by shrinking spaces and growing odd widths. A beginner pattern walkthrough derives both formulas and checks the three-row output.Open example →py# Program to print a pyramid star pattern rows = int(input("Enter number of rows: "))
- 23Print Inverted Pyramid PatternBeginnerTurn a Python star pyramid upside down by reversing row traversal. Beginners follow a negative range step while keeping the same padding and width formulas.Open example →py# Program to print an inverted pyramid star pattern rows = int(input("Enter number of rows: "))
- 24Print Floyd's TriangleBeginnerKeep a Python counter running across triangle rows instead of restarting at one. A beginner nested-loop trace explains row lengths and newline placement.Open example →py# Program to print Floyd's triangle rows = int(input("Enter number of rows: "))
- 25Print Pascal's TriangleIntermediateProduce Pascal coefficients with exact Python integer updates. First-year nested-loop practice resets each row to one and traces the next-value recurrence.Open example →py# Program to print Pascal's triangle rows = int(input("Enter number of rows: "))