Course Content
Python Loops Tutorial
Python Loops Tutorial
Range() in a for Loop 1/2
Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:
But that would be incredibly time-consuming!
If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!
To tackle this challenge, we'll employ the for
loop in conjunction with the range()
function!
Take a look at the following code:
# Printing all numbers from 0 to 100 for i in range(101): print(i)
How does the code work?
The range()
function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).
The format of the range()
function is: range(start, end, step)
.
If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range()
function.
Look at the code below:
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
Swipe to show code editor
Print numbers from -1 to -5, follow these steps:
- Configure the
for
loop to display decreasing numbers from-1
to-5
. - Print each number within the loop.
Thanks for your feedback!
Range() in a for Loop 1/2
Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:
But that would be incredibly time-consuming!
If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!
To tackle this challenge, we'll employ the for
loop in conjunction with the range()
function!
Take a look at the following code:
# Printing all numbers from 0 to 100 for i in range(101): print(i)
How does the code work?
The range()
function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).
The format of the range()
function is: range(start, end, step)
.
If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range()
function.
Look at the code below:
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
Swipe to show code editor
Print numbers from -1 to -5, follow these steps:
- Configure the
for
loop to display decreasing numbers from-1
to-5
. - Print each number within the loop.
Thanks for your feedback!
Range() in a for Loop 1/2
Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:
But that would be incredibly time-consuming!
If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!
To tackle this challenge, we'll employ the for
loop in conjunction with the range()
function!
Take a look at the following code:
# Printing all numbers from 0 to 100 for i in range(101): print(i)
How does the code work?
The range()
function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).
The format of the range()
function is: range(start, end, step)
.
If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range()
function.
Look at the code below:
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
Swipe to show code editor
Print numbers from -1 to -5, follow these steps:
- Configure the
for
loop to display decreasing numbers from-1
to-5
. - Print each number within the loop.
Thanks for your feedback!
Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:
But that would be incredibly time-consuming!
If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!
To tackle this challenge, we'll employ the for
loop in conjunction with the range()
function!
Take a look at the following code:
# Printing all numbers from 0 to 100 for i in range(101): print(i)
How does the code work?
The range()
function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).
The format of the range()
function is: range(start, end, step)
.
If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range()
function.
Look at the code below:
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
Swipe to show code editor
Print numbers from -1 to -5, follow these steps:
- Configure the
for
loop to display decreasing numbers from-1
to-5
. - Print each number within the loop.