Stop at 6 with a while loop

Python Practice › Loop Programs › Program 80

Stop at 6 with a while loop

EasyLoop Programs · 80 of 90

Write a Python program to print 1 to 10 but stop when the number is 6, using a while loop.

Output
1
2
3
4
5

Approach

  1. break leaves the loop.

Solution

Python
n = 1while n <= 10:    if n == 6:        break    print(n)    n += 1
Output
1
2
3
4
5

Learn the concept

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

More loop programs

← All 90 Loop Programs

Last element with a while loop

Python Practice › Loop Programs › Program 79

Last element with a while loop

EasyLoop Programs · 79 of 90

Write a Python program to print the last element of a list using a while loop.

Input
["s", "q", "a", "t", "o", "o", "l", "s"]
Output
s | shortcut: s

Approach

  1. Move an index to the end of the list, then read that item.

Solution

Python
c = ["s", "q", "a", "t", "o", "o", "l", "s"]i = 0while i < len(c) - 1:    i += 1print(c[i], "| shortcut:", c[-1])
Output
s | shortcut: s

Line by line

Lines 3-4

The loop stops on the last valid index.

Line 5

c[-1] is the normal way to read the last item.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Copy special characters to a new list

Python Practice › Loop Programs › Program 78

Copy special characters to a new list

EasyLoop Programs · 78 of 90

Write a Python program to copy the special characters from a list into an empty list.

Input
["s", "2", "4", "6", "a", "@", "!", "%", "#"]
Output
['@', '!', '%', '#']

Approach

  1. Keep items that are not letters or digits.

Solution

Python
a = ["s", "2", "4", "6", "a", "@", "!", "%", "#"]b = []for ch in a:    if not ch.isalnum():        b.append(ch)print(b)
Output
['@', '!', '%', '#']

Learn the concept

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

More loop programs

← All 90 Loop Programs

Copy odd numbers to a new list

Python Practice › Loop Programs › Program 77

Copy odd numbers to a new list

EasyLoop Programs · 77 of 90

Write a Python program to copy the odd numbers from a list into an empty list.

Input
[3, 8, 5, 0, 2, 7]
Output
[3, 5, 7]

Approach

  1. Append numbers with remainder 1.

Solution

Python
a = [3, 8, 5, 0, 2, 7]b = [n for n in a if n % 2 == 1]print(b)
Output
[3, 5, 7]

Learn the concept

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

More loop programs

← All 90 Loop Programs

Copy even numbers to a new list

Python Practice › Loop Programs › Program 76

Copy even numbers to a new list

EasyLoop Programs · 76 of 90

Write a Python program to copy the even numbers from a list into an empty list.

Input
[2, 3, 5, 76, 9, 0, 16]
Output
[2, 76, 0, 16]

Approach

  1. Append numbers with remainder 0.

Solution

Python
a = [2, 3, 5, 76, 9, 0, 16]b = []for n in a:    if n % 2 == 0:        b.append(n)print(b)
Output
[2, 76, 0, 16]

Learn the concept

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

More loop programs

← All 90 Loop Programs

Count special characters

Python Practice › Loop Programs › Program 75

Count special characters

EasyLoop Programs · 75 of 90

Write a Python program to count the special characters (not letters, digits or spaces) in a string.

Input
"@sqa#tools!!"
Output
4

Approach

  1. A special character is not alphanumeric and not a space.

Solution

Python
text = "@sqa#tools!!"count = 0for ch in text:    if not ch.isalnum() and not ch.isspace():        count += 1print(count)
Output
4

Learn the concept

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

More loop programs

← All 90 Loop Programs

Days of the week except Sunday

Python Practice › Loop Programs › Program 74

Days of the week except Sunday

EasyLoop Programs · 74 of 90

Write a Python program to print the days of the week except Sunday using a while loop.

Output
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Approach

  1. Walk through a list with an index and skip “Sunday”.

Solution

Python
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]i = 0while i < len(days):    if days[i] != "Sunday":        print(days[i])    i += 1
Output
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Learn the concept

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

More loop programs

← All 90 Loop Programs

1 to 10 except 5 and 6 with while

Python Practice › Loop Programs › Program 73

1 to 10 except 5 and 6 with while

EasyLoop Programs · 73 of 90

Write a Python program to print 1 to 10 except 5 and 6 using a while loop.

Output
1
2
3
4
7
8
9
10

Approach

  1. Increase the counter before continue, otherwise the loop never ends.

Solution

Python
n = 0while n < 10:    n += 1    if n in (5, 6):        continue    print(n)
Output
1
2
3
4
7
8
9
10

Line by line

Line 3

Incrementing at the top of the loop means continue cannot skip it.

Watch out

Putting n += 1 after the continue would make the loop get stuck at 5 forever.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Product of the first 10 numbers

Python Practice › Loop Programs › Program 72

Product of the first 10 numbers

EasyLoop Programs · 72 of 90

Write a Python program to find the product of the first 10 natural numbers.

Output
3628800

Approach

  1. Start at 1 and multiply by each number.

Solution

Python
product = 1for n in range(1, 11):    product *= nprint(product)
Output
3628800
Note

This is 10! (10 factorial).

Learn the concept

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

More loop programs

← All 90 Loop Programs

Sum of the first 10 numbers with while

Python Practice › Loop Programs › Program 71

Sum of the first 10 numbers with while

EasyLoop Programs · 71 of 90

Write a Python program to find the sum of the first 10 natural numbers using a while loop.

Output
55

Approach

  1. Add a counter to a total until it passes 10.

Solution

Python
i, total = 1, 0while i <= 10:    total += i    i += 1print(total)
Output
55

Learn the concept

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

More loop programs

← All 90 Loop Programs