First and last digits

Python Practice › Loop Programs › Program 20

First and last digits

EasyLoop Programs · 20 of 90

Write a Python program to find the first and last digits of a number.

Input
74351
Output
First digit: 7
Last digit: 1

Approach

  1. The last digit is n % 10; remove digits until one is left for the first digit.

Solution

Python
n = 74351last = n % 10first = nwhile first >= 10:    first //= 10print("First digit:", first)print("Last digit:", last)
Output
First digit: 7
Last digit: 1

Line by line

Lines 4-5

Keep dropping the last digit until a single digit remains.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Count the digits of a number

Python Practice › Loop Programs › Program 19

Count the digits of a number

EasyLoop Programs · 19 of 90

Write a Python program to count the digits of a number.

Input
908172
Output
Digits: 6

Approach

  1. Divide by 10 until the number becomes 0, counting the steps.

Solution

Python
n = 908172count = 0while n > 0:    n //= 10    count += 1print("Digits:", count)
Output
Digits: 6

Line by line

Line 4

Each //= 10 removes one digit.

Tip

len(str(abs(n))) gives the same answer for non-zero numbers.

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 n

Python Practice › Loop Programs › Program 18

Sum of odd numbers 1 to n

EasyLoop Programs · 18 of 90

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

Input
n = 20
Output
Sum of odd numbers: 100

Approach

  1. Loop from 1 with a step of 2.

Solution

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

The sum of the first k odd numbers is k², so for n = 20 (ten odd numbers) the answer is 100.

Learn the concept

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

More loop programs

← All 90 Loop Programs

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