Table of 5 with a for loop

Python Practice › Loop Programs › Program 60

Table of 5 with a for loop

EasyLoop Programs · 60 of 90

Write a Python program to print the multiplication table of 5 with a for loop.

Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Approach

  1. Loop i from 1 to 10.

Solution

Python
for i in range(1, 11):    print(f"5 x {i} = {5 * i}")
Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Star triangle up and down

Python Practice › Loop Programs › Program 59

Star triangle up and down

EasyLoop Programs · 59 of 90

Write a Python program to print stars growing from 1 to 5 and back to 1.

Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Approach

  1. Build the list of row sizes 1..5..1 and print each.

Solution

Python
n = 5sizes = list(range(1, n + 1)) + list(range(n - 1, 0, -1))for s in sizes:    print("* " * s)
Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Line by line

Line 2

The two ranges give 1, 2, 3, 4, 5, 4, 3, 2, 1.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Does a string contain a digit?

Python Practice › Loop Programs › Program 58

Does a string contain a digit?

EasyLoop Programs · 58 of 90

Write a Python program to check whether a string contains a digit.

Input
"Python123", "Python"
Output
Python123 True
Python False

Approach

  1. Loop until a digit is found; use for ... else for the “not found” case.

Solution

Python
for text in ["Python123", "Python"]:    for ch in text:        if ch.isdigit():            print(text, True)            break    else:        print(text, False)
Output
Python123 True
Python False

Line by line

Line 5

break stops at the first digit.

Lines 6-7

The else runs only when the loop ended without finding one.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Validate a password

Python Practice › Loop Programs › Program 57

Validate a password

MediumLoop Programs · 57 of 90

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.

Input
"Abc@1234", "abc123", "Ab1$"
Output
Abc@1234 -> Valid password
abc123 -> Invalid: needs an uppercase letter, needs one of $#@
Ab1$ -> Invalid: length must be 5-15

Approach

  1. Loop over the characters once, recording which kinds were seen.
  2. Check all rules at the end and list any that failed.

Solution

Python
def check(password):    has_lower = has_upper = has_digit = has_special = False    for ch in password:        if ch.islower():            has_lower = True        elif ch.isupper():            has_upper = True        elif ch.isdigit():            has_digit = True        elif ch in "$#@":            has_special = True    problems = []    if not 5 <= len(password) <= 15:        problems.append("length must be 5-15")    if not has_lower:        problems.append("needs a lowercase letter")    if not has_upper:        problems.append("needs an uppercase letter")    if not has_digit:        problems.append("needs a digit")    if not has_special:        problems.append("needs one of $#@")    return "Valid password" if not problems else "Invalid: " + ", ".join(problems) for pw in ["Abc@1234", "abc123", "Ab1$"]:    print(pw, "->", check(pw))
Output
Abc@1234 -> Valid password
abc123 -> Invalid: needs an uppercase letter, needs one of $#@
Ab1$ -> Invalid: length must be 5-15

Line by line

Line 2

Four flags start as False.

Lines 3-11

One pass over the password sets the flags.

Lines 12-22

Each rule adds a readable reason when it fails, so the user knows what to fix.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Fibonacci numbers up to 55

Python Practice › Loop Programs › Program 56

Fibonacci numbers up to 55

EasyLoop Programs · 56 of 90

Write a Python program to print the first 11 Fibonacci numbers (0 to 55).

Output
0 1 1 2 3 5 8 13 21 34 55

Approach

  1. Build a list starting [0, 1] and append the sum of the last two.

Solution

Python
fib = [0, 1]while len(fib) < 11:    fib.append(fib[-1] + fib[-2])print(*fib)
Output
0 1 1 2 3 5 8 13 21 34 55

Line by line

Line 3

fib[-1] and fib[-2] are the last two values.

Line 4

*fib passes the items separately, so they print with spaces.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Number triangle 1, 22, 333

Python Practice › Loop Programs › Program 55

Number triangle 1, 22, 333

EasyLoop Programs · 55 of 90

Write a Python program to print row i with the number i repeated i times, for i from 1 to 5, using nested loops.

Output
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

Approach

  1. An inner loop prints the row number i times.

Solution

Python
for i in range(1, 6):    for _ in range(i):        print(i, end=" ")    print()
Output
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Inverted right triangle of stars

Python Practice › Loop Programs › Program 54

Inverted right triangle of stars

EasyLoop Programs · 54 of 90

Write a Python program to print an inverted right triangle of stars with 5 rows.

Output
* * * * * 
* * * * 
* * * 
* * 
* 

Approach

  1. Count rows down from 5 to 1.

Solution

Python
for i in range(5, 0, -1):    print("* " * i)
Output
* * * * * 
* * * * 
* * * 
* * 
* 

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Right triangle of stars

Python Practice › Loop Programs › Program 53

Right triangle of stars

EasyLoop Programs · 53 of 90

Write a Python program to print a right triangle of stars with 5 rows.

Output
* 
* * 
* * * 
* * * * 
* * * * * 

Approach

  1. Row i prints i stars.

Solution

Python
for i in range(1, 6):    print("* " * i)
Output
* 
* * 
* * * 
* * * * 
* * * * * 

Line by line

Line 2

Repeating “* ” i times builds row i.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Numbers from 0 to 100 divisible by 5

Python Practice › Loop Programs › Program 52

Numbers from 0 to 100 divisible by 5

EasyLoop Programs · 52 of 90

Write a Python program to find the numbers from 0 to 100 divisible by 5.

Output
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

Approach

  1. Keep numbers with n % 5 == 0.

Solution

Python
print([n for n in range(0, 101) if n % 5 == 0])
Output
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Sum of odd numbers 1 to 100

Python Practice › Loop Programs › Program 51

Sum of odd numbers 1 to 100

EasyLoop Programs · 51 of 90

Write a Python program to calculate the sum of all odd numbers from 1 to 100.

Output
2500

Approach

  1. Loop over odd numbers and add them.

Solution

Python
total = 0for n in range(1, 101, 2):    total += nprint(total)
Output
2500

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs