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.

Advertisement
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().

Advertisement

Learn the concept

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

More loop programs

← All 90 Loop Programs