The range
function in Python is a built-in function that allows you to generate a sequence of numbers. It is often used in for loops to iterate over a sequence of numbers.
Here is an example of how to use the range
function:
Â
# Print the numbers 0 to 9
for i in range(10):
print(i)
Â
Certainly! Here are 10 examples of using the range function in Python:
Generating a sequence of numbers from 0 to 9:
for i in range(10):
print(i)
Generating a sequence of numbers from 3 to 9:
for i in range(3, 10):
print(i)
Generating a sequence of numbers from 0 to 9 with a step size of 2:
for i in range(0, 10, 2):
print(i)
for i in range(9, -1, -1):
print(i)
Generating a sequence of numbers from -5 to 5:
for i in range(-5, 6):
print(i)
Generating a sequence of numbers from -10 to -1:
for i in range(-10, 0):
print(i)
Generating a sequence of numbers from -10 to -1 with a step size of 3:
for i in range(-10, 0, 3):
print(i)
Generating a sequence of numbers from 1 to 10 with a step size of 0.5:
for i in range(1, 11, 0.5):
print(i)
Generating a sequence of numbers from 0 to 1 with a step size of 0.1:
for i in range(0, 1.1, 0.1):
print(i)
print(i)
Â
Create a list using range function
The range function is a built-in function in Python that allows you to generate a sequence of numbers. You can use it to create a list of numbers like this:
numbers = list(range(10))
print(numbers)
This will create a list of numbers from 0 to 9. You can also specify a starting and ending number for the range, like this:
numbers = list(range(1, 11))
print(numbers)
numbers = list(range(1, 11))
print(numbers)
This will create a list of numbers from 1 to 10. You can also specify a step value, which determines the increment between the numbers in the sequence. For example:
numbers = list(range(0, 20, 2))
print(numbers)
Â