For loop 1 to 10 code examples in Python

avatar

Borislav Hadzhiev

Last updated: Jul 3, 2022

banner

Photo from Unsplash

For loop 1 to 10 code examples in Python #

Use the range() class to loop from 1 to 10 in a for loop, e.g. for num in range(1, 11):. The range class takes start (inclusive) and stop (exclusive) arguments and enables us to loop a specific number of times in for loops.

main.py
# 👇️ for loop 1 to 10 (including 10) for num in range(1, 11): print(num) # 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list(range(1, 11))) # --------------------------------------- # 👇️ for loop 1 to 10 (excluding 10) for num in range(1, 10): print(num) # 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9] print(list(range(1, 10)))

We used the range() class to loop from 1 to 10 in a for loop.

Note that the first parameter of the range() class (the start value) is inclusive, whereas the stop value is exclusive.

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
for num in range(10): print(num) # 👇️ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(list(range(10)))
The example shows that if the start argument is omitted, it defaults to 0 and if the step argument is omitted, it defaults to 1.

Note that the range() class returns a range object, not a list.

main.py
# 👇️ range(0, 10) print(range(10)) # 👇️ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(list(range(10)))

If you need to convert the range object to a list, pass it to the list() class.

If values for the start and stop parameters are provided, the start value is inclusive, whereas the stop value is exclusive.

main.py
# 👇️ for loop 1 to 10 (including 10) for num in range(1, 11): print(num) # 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list(range(1, 11)))

If the value for the stop parameter is lower than the value for the start parameter, the range will be empty.

main.py
for num in range(10, 1): print(num) print(list(range(10, 1))) # 👉️ []
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.