Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

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

2 min read


Problem Definition

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

Understanding the For Loop

In Python, the for loop is used to iterate over a sequence of elements, executing a set of statements for each item in the sequence. The loop continues until all items in the sequence have been processed. It is widely used to perform repetitive tasks efficiently.

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 times the loop has 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 that is iterating over numbers from 1 to 10.

The for loop is used to iterate through the range of numbers from 1 to 10 (inclusive). The range() function generates a sequence of numbers, starting from the first argument (1) up to, but not including, the second argument (11), 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 11.

Conclusion

Congratulations! You have successfully created a Python program that uses a for loop to print numbers from 1 to 10. The for loop is a powerful construct that allows you to efficiently iterate through sequences of elements in Python.

This program serves as a foundation for understanding loops and their utility in Python.

You can further enhance the program by adding additional logic or modifying the range to print numbers within different intervals.

Keep practicing, and you will soon become proficient in Python programming.


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read