Table of Contents

Print First Five Natural Numbers | Pictoblox

Example Description
Learn how to use Python to print the first five natural numbers & what we do if we are asked to print the first 100,000 natural numbers?

Introduction:

In this Python code snippet, we will use a simple program to print the first five natural numbers.

Code:

#Print first five natural numbers
print(1)
print(2)
print(3)
print(4)
print(5)

Logic:

  1. The code uses the print() function to display the first five natural numbers.
  2. The numbers are printed one by one in ascending order, starting from 1 and ending at 5.
What should we do if we are asked to print the first 100,000 natural numbers?

Writing 100,000 print statements would not be an efficient solution. It would be tedious and not the best way to do the task. Writing a program having a loop or repetition is a better solution.

The program logic is given below:
1. Take a variable, say count, and set its value to 1.
2. Print the value of count.
3. Increment the variable (count += 1).
4. Repeat steps 2 and 3 as long as count has a valueless than or equal to 100,000 (count <= 100,000).

Output:

>> 1

>> 2

>> 3

>> 4

>> 5