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

Print the keys of a dictionary

Python Practice › Loop Programs › Program 66

Print the keys of a dictionary

EasyLoop Programs · 66 of 90

Write a Python program to print the keys of a dictionary using a loop.

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

Approach

  1. Looping over a dict gives its keys.

Solution

Python
player = {"name": "virat", "sports": "cricket"}for key in player:    print(key)
Output
name
sports

Learn the concept

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

More loop programs

← All 90 Loop Programs

Odd numbers 1-10 into a list

Python Practice › Loop Programs › Program 65

Odd numbers 1-10 into a list

EasyLoop Programs · 65 of 90

Write a Python program that adds the odd numbers from 1 to 10 into an empty list.

Output
[1, 3, 5, 7, 9]

Approach

  1. Append each odd number.

Solution

Python
odds = []for n in range(1, 11):    if n % 2 == 1:        odds.append(n)print(odds)
Output
[1, 3, 5, 7, 9]

Learn the concept

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

More loop programs

← All 90 Loop Programs

Even numbers 1-10 into a list

Python Practice › Loop Programs › Program 64

Even numbers 1-10 into a list

EasyLoop Programs · 64 of 90

Write a Python program that adds the even numbers from 1 to 10 into an empty list.

Output
[2, 4, 6, 8, 10]

Approach

  1. Append each even number.

Solution

Python
evens = []for n in range(1, 11):    if n % 2 == 0:        evens.append(n)print(evens)
Output
[2, 4, 6, 8, 10]

Learn the concept

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

More loop programs

← All 90 Loop Programs

Each character on a new line

Python Practice › Loop Programs › Program 63

Each character on a new line

EasyLoop Programs · 63 of 90

Write a Python program to print each character of a string on a new line.

Input
"Sqatools"
Output
S
q
a
t
o
o
l
s

Approach

  1. Loop over the string; print() adds a newline after each character.

Solution

Python
for ch in "Sqatools":    print(ch)
Output
S
q
a
t
o
o
l
s

Learn the concept

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

More loop programs

← All 90 Loop Programs

Display numbers from a list

Python Practice › Loop Programs › Program 62

Display numbers from a list

EasyLoop Programs · 62 of 90

Write a Python program to display the numbers of a list separated by commas.

Input
[1, 5, 8, 0, 4]
Output
1,5,8,0,4

Approach

  1. Print each number with end="," except after the last one.

Solution

Python
numbers = [1, 5, 8, 0, 4]for i, n in enumerate(numbers):    end = "," if i < len(numbers) - 1 else "\n"    print(n, end=end)
Output
1,5,8,0,4

Line by line

Line 3

Every item except the last is followed by a comma.

Learn the concept

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

More loop programs

← All 90 Loop Programs

First 20 natural numbers

Python Practice › Loop Programs › Program 61

First 20 natural numbers

EasyLoop Programs · 61 of 90

Write a Python program to print the first 20 natural numbers with a for loop, separated by commas.

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

Approach

  1. Join the numbers with commas.

Solution

Python
print(",".join(str(n) for n in range(1, 21)))
Output
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

Line by line

Line 1

join needs strings, so each number is converted with str().

Learn the concept

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

More loop programs

← All 90 Loop Programs