Python range() function

The range() function gives you a bunch of numbers. An ordered bunch of numbers. Simple series like 1, 2, 3, 4.

# A typical use of range. This gives you 0, 1, 2
range(3)

Now, the output type of the range() function is a range object. Don’t get confused, the output of range() is always a bunch of numbers.

The range() function and for loops are like best buddies in Python. One creates a sequence of numbers, and the other loops through it.

range() syntax

range() examples

print(*(i for i in range(3)))
0
1
2
range(3) -> 0, 1, 2


range(10)[-1]
9


print(*(fruits[i] for i in range(len(fruits))))
apple
banana
cherry
date
* unpacks the items


<div style="margin-bottom: 10px;display: flex; justify-content: center; align-items: center;""> print(*(i for i in range(10, 0, -2)))
<div style="display: flex; justify-content: center; align-items: center;">
    <div style="padding: 5px; border: 2px solid #00796b; border-radius: 5px; background-color: #ffffff; margin: 3px;">
        <span style="font-size: 1.2em; color: #000;">10</span>
    </div>
    <div style="padding: 5px; border: 2px solid #00796b; border-radius: 5px; background-color: #ffffff; margin: 3px;">
        <span style="font-size: 1.2em; color: #000;">8</span>
    </div>
    <div style="padding: 5px; border: 2px solid #00796b; border-radius: 5px; background-color: #ffffff; margin: 3px;">
        <span style="font-size: 1.2em; color: #000;">6</span>
    </div>
    <div style="padding: 5px; border: 2px solid #00796b; border-radius: 5px; background-color: #ffffff; margin: 3px;">
        <span style="font-size: 1.2em; color: #000;">4</span>
    </div>
    <div style="padding: 5px; border: 2px solid #00796b; border-radius: 5px; background-color: #ffffff; margin: 3px;">
        <span style="font-size: 1.2em; color: #000;">2</span>
    </div>
</div>
Backwards using minus number

</div>


list( range( 5))
[0,1,2,3,4]
print(*(fruits[i] for i in range(len(fruits))))