Saturday, May 18, 2019

Abhijeet Pal: Python Program To Print Numbers From 1 to 10 Using For Loop

Problem Definition

Create a Python program to print numbers from 1 to 10 using a for loop.

Solution

In programming, Loops are used to repeat a block of code until a specific condition is met. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Also, we are going to use one of Python’s built-in function range(). This function is extensively used in loops to control the number of types loop have to run. In simple words range is used to generate a sequence between the given values.

For a better understanding of these Python, concepts it is recommended to read the following articles.

Program

for i in range(1, 11):
    print(i)

Output

1
2
3
4
5
6
7
8
9
10

Explanation

The for loop prints the number from 1 to 10 using the range() function here i is a temporary variable which is iterating over numbers from 1 to 10.

It’s worth mentioning that similar to list indexing in range starts from 0 which means range( j )will print sequence till ( j-1) hence the output doesn’t include 6.

The post Python Program To Print Numbers From 1 to 10 Using For Loop appeared first on Django Central.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...