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

Table of 2 with a while loop

Python Practice › Loop Programs › Program 70

Table of 2 with a while loop

EasyLoop Programs · 70 of 90

Write a Python program to print the table of 2 using a while loop.

Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Approach

  1. Use a counter from 1 to 10.

Solution

Python
i = 1while i <= 10:    print(f"2 x {i} = {2 * i}")    i += 1
Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Learn the concept

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

More loop programs

← All 90 Loop Programs

First 20 natural numbers with while

Python Practice › Loop Programs › Program 69

First 20 natural numbers with while

EasyLoop Programs · 69 of 90

Write a Python program to print the first 20 natural numbers using a while loop.

Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

Approach

  1. Count from 1 to 20.

Solution

Python
n = 1while n <= 20:    print(n, end=" ")    n += 1print()
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

Learn the concept

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

More loop programs

← All 90 Loop Programs

Print keys and values

Python Practice › Loop Programs › Program 68

Print keys and values

EasyLoop Programs · 68 of 90

Write a Python program to print the keys and values of a dictionary.

Input
{"name": "virat", "sports": "cricket"}
Output
name Virat
sports Cricket

Approach

  1. Loop over items() to get both at once.

Solution

Python
player = {"name": "virat", "sports": "cricket"}for key, value in player.items():    print(key, value.capitalize())
Output
name Virat
sports Cricket

Line by line

Line 2

items() yields (key, value) pairs that unpack into two variables.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Print the values of a dictionary

Python Practice › Loop Programs › Program 67

Print the values of a dictionary

EasyLoop Programs · 67 of 90

Write a Python program to print the values of a dictionary, capitalised.

Input
{"name": "virat", "sports": "cricket"}
Output
Virat
Cricket

Approach

  1. Loop over values().

Solution

Python
player = {"name": "virat", "sports": "cricket"}for value in player.values():    print(value.capitalize())
Output
Virat
Cricket

Learn the concept

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

More loop programs

← All 90 Loop Programs