Sum of even numbers 1 to n

Python Practice › Loop Programs › Program 17

Sum of even numbers 1 to n

EasyLoop Programs · 17 of 90

Write a Python program to find the sum of all even numbers from 1 to n.

Input
n = 20
Output
Sum of even numbers: 110

Approach

  1. Loop over even numbers only and add them.

Solution

Python
n = 20total = 0for i in range(2, n + 1, 2):    total += iprint("Sum of even numbers:", total)
Output
Sum of even numbers: 110

Line by line

Line 3

The step of 2 skips odd numbers entirely.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Sum of 1 to n

Python Practice › Loop Programs › Program 16

Sum of 1 to n

EasyLoop Programs · 16 of 90

Write a Python program to find the sum of all natural numbers from 1 to n.

Input
n = 50
Output
1275 | formula: 1275

Approach

  1. Add each number to a running total.

Solution

Python
n = 50total = 0for i in range(1, n + 1):    total += iprint(total, "| formula:", n * (n + 1) // 2)
Output
1275 | formula: 1275

Line by line

Line 5

The formula n(n+1)/2 confirms the loop’s answer.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Odd numbers from 1 to 100

Python Practice › Loop Programs › Program 15

Odd numbers from 1 to 100

EasyLoop Programs · 15 of 90

Write a Python program to print all odd numbers from 1 to 100.

Output
[1, 3, 5, 7, 9] ... [95, 97, 99]
50 odd numbers

Approach

  1. Use range(1, 100, 2).

Solution

Python
odds = [n for n in range(1, 101) if n % 2 == 1]print(odds[:5], "...", odds[-3:])print(len(odds), "odd numbers")
Output
[1, 3, 5, 7, 9] ... [95, 97, 99]
50 odd numbers

Line by line

Line 1

The comprehension keeps numbers whose remainder after dividing by 2 is 1.

Line 2

Printing only the first and last few keeps the output short.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Even numbers from 1 to 100

Python Practice › Loop Programs › Program 14

Even numbers from 1 to 100

EasyLoop Programs · 14 of 90

Write a Python program to print all even numbers from 1 to 100.

Output
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 

Approach

  1. Use range(2, 101, 2).

Solution

Python
for n in range(2, 101, 2):    print(n, end=" ")print()
Output
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 

Line by line

Line 1

Starting at 2 with a step of 2 visits only even numbers.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Print A to Z with chr()

Python Practice › Loop Programs › Program 13

Print A to Z with chr()

EasyLoop Programs · 13 of 90

Write a Python program to print the alphabet from A to Z using a for loop and chr() (chr(65) is ‘A’).

Output
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
97 -> abcdefghijklmnopqrstuvwxyz

Approach

  1. Loop over the character codes 65 to 90 and convert each with chr().

Solution

Python
for code in range(65, 91):    print(chr(code), end=" ")print()print(ord("a"), "->", "".join(chr(c) for c in range(ord("a"), ord("z") + 1)))
Output
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
97 -> abcdefghijklmnopqrstuvwxyz

Line by line

Line 1

65 is the Unicode code of ‘A’ and 90 of ‘Z’.

Line 4

ord() is the reverse of chr(); this line builds a to z without remembering the numbers.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Natural numbers n to 1 with while

Python Practice › Loop Programs › Program 12

Natural numbers n to 1 with while

EasyLoop Programs · 12 of 90

Write a Python program to print the natural numbers from n down to 1 using a while loop.

Input
n = 10
Output
10 9 8 7 6 5 4 3 2 1 

Approach

  1. Start at n and decrease until 0.

Solution

Python
n = 10while n >= 1:    print(n, end=" ")    n -= 1print()
Output
10 9 8 7 6 5 4 3 2 1 

Line by line

Line 4

-= 1 counts down.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Natural numbers 1 to n with while

Python Practice › Loop Programs › Program 11

Natural numbers 1 to n with while

EasyLoop Programs · 11 of 90

Write a Python program to print the natural numbers from 1 to n using a while loop.

Input
n = 10
Output
1 2 3 4 5 6 7 8 9 10 

Approach

  1. Start a counter at 1 and increase it until it passes n.

Solution

Python
n = 10i = 1while i <= n:    print(i, end=" ")    i += 1print()
Output
1 2 3 4 5 6 7 8 9 10 

Line by line

Line 5

Forgetting this line would make the loop run forever.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Letter O pattern

Python Practice › Loop Programs › Program 10

Letter O pattern

MediumLoop Programs · 10 of 90

Write a Python program to print the letter ‘O’ with stars.

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

Approach

  1. The top and bottom rows print stars in the middle columns.
  2. The other rows print a star only in the first and last columns.

Solution

Python
rows, cols = 7, 5for r in range(rows):    line = ""    for c in range(cols):        top_or_bottom = (r == 0 or r == rows - 1) and 0 < c < cols - 1        side = 0 < r < rows - 1 and (c == 0 or c == cols - 1)        line += "*" if top_or_bottom or side else " "    print(line)
Output
 *** 
*   *
*   *
*   *
*   *
*   *
 *** 

Line by line

Line 5

The first and last rows get stars only between the corners, which rounds the shape.

Line 6

The middle rows get stars only on the left and right edges.

Line 7

Each position adds a star or a space.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Count letters and digits

Python Practice › Loop Programs › Program 9

Count letters and digits

EasyLoop Programs · 9 of 90

Write a Python program that counts the letters and digits in a string.

Input
"python1234"
Output
Letters 6
Digits 4

Approach

  1. Use isalpha() and isdigit() on each character.

Solution

Python
text = "python1234"letters = digits = 0for ch in text:    if ch.isalpha():        letters += 1    elif ch.isdigit():        digits += 1print("Letters", letters)print("Digits", digits)
Output
Letters 6
Digits 4

Line by line

Lines 4-7

Spaces and symbols match neither test, so they are not counted.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Convert uppercase letters to lowercase

Python Practice › Loop Programs › Program 8

Convert uppercase letters to lowercase

EasyLoop Programs · 8 of 90

Write a Python program that converts all uppercase letters in a word to lowercase, using a loop.

Input
"SqaTOOlS"
Output
sqatools

Approach

  1. Loop over the characters, lower-case the uppercase ones and build a new string.

Solution

Python
word = "SqaTOOlS"result = ""for ch in word:    if ch.isupper():        result += ch.lower()    else:        result += chprint(result)
Output
sqatools

Line by line

Lines 4-5

Only uppercase characters are changed.

Tip

word.lower() does the whole job in one call; the loop shows how it works.

Learn the concept

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

More loop programs

← All 90 Loop Programs