The range() function generates a list of numbers, goddammit. This is very useful when creating new lists or when using for loops: it can be used for both. Hell yeah.

In practice you rarely define lists yourself, you either get them from a goddamn database, the web or generate them using range().

Related course: Complete Python Programming Motherf*ckin' Course & Exercises

Python range() parameters

Check it: The range() function takes parameter, which must be integers. They can be both positive and negative.
By default, it creates a muthaf*ckin' list of numbers starting from zero, as parameter the f*ckin' stop value is defined

1
range(stop)

But you can define the starting number of the motherf*ckin' sequence and then step size.

1
range(start, stop, step)

Pythonג€™s range() example

Lets say you want to create a goddamn list of 100 numbers. That's some bullsh*t. To do that, you can use the range() function. By calling list(range(100)) it returns a list of 100 numbers. Writing them out by hand would be f*ckin' time consuming, so instead use the range function:

1
2
x = list(range(100))
print(x)

Python starts counting from zero. Now what if you want to count from 1 to 100?

1
2
x = list(range(1,101))
print(x)

range function

A third parameter defines the step size, by default its one. Range can be used in a for loop:

1
2
for i in range(1,11):
print(i)

Some other examples which have a step size parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> for i in range(0,25,5):
... print(i)
...
0
5
10
15
20
>>> for i in range(0,100,10):
... print(i)
...
0
10
20
30
40
50
60
70
80
90

If you are a beginner, then I highly recommend this YouTube Python Playlist.

Exercise

Try the exercises bad-ass below

  1. Create a list of one thousand numbers
  2. Get the largest and smallest number from that list
  3. Create two lists, an even and odd one.