Python Practice › Loop Programs › Program 61
First 20 natural numbers
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
- Join the numbers with commas.
Solution
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.