Home Python Page 7 - Python 101 (part 3): A Twist In The Tail

# Within Range() - Python

Find out more about adding flow control to your Python programswith the "for" and "while" loops, see how the range() function can be usedto generate number ranges, and learn all about list objects. And bring anold flame along for the ride.

Rating:  / 4
June 13, 2001

SEARCH DEV SHED

TOOLS YOU CAN USE
While on the topic of the "for" loop, it's worth mentioning the range()function, a built-in Python function whose sole raison d'etre is to returna range of numbers, given a starting and ending point. This range is alwaysreturned as a list - and as you'll see, this can combine quite effectivelywith the "for" loop in certain situations.
```
>>> range(30,40)
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
>>>
```
You can omit the first argument to have Python generate a range from 0 tothe specified end point.
```
>>> range(40)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
>>>
```
You can skip certain numbers in the range by adding an optional "step"argument (by default, this is 1).
```
>>> range (25,500,25)
[25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375,
400, 425, 450, 475]
>>> range (100,1,-10)
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
>>>
```
What does this have to do with anything? Well, the range() function, incombination with the "for" loop, can come in handy when you need to performa series of actions a specified number of times,
```
>>> for x in range(1,12):
...     print "It is now", x, "o'clock"
...
It is now 1 o'clock
It is now 2 o'clock
It is now 3 o'clock
It is now 4 o'clock
It is now 5 o'clock
It is now 6 o'clock
It is now 7 o'clock
It is now 8 o'clock
It is now 9 o'clock
It is now 10 o'clock
It is now 11 o'clock
>>>
```
or to generate indices (corresponding to list elements) in a "for" loop.
```
>>> flavours = ["Strawberry", "Blueberry", "Blackcurrant", "Pineapple",
"Mango", "Grape", "Orange", "Banana"]
>>> for temp in range(2,5): print flavours[temp]
...
Blackcurrant
Pineapple
Mango
>>>
```
Here's a more interesting example.
```
#!/usr/bin/python
# get a number
num = input("Gimme a number: ")
# for loop
for count in range(2,num):
# if factor exists, num cannot be prime!
if num % count == 0:
print num, "is not a prime number."
break
else:
# if we get this far, num is prime!
print num, "is a prime number."
```
This is a simple piece of code to test whether or not a number is prime. A"for" loop is used, in conjunction with the range() function, to divide theuser-specified number (num) by all numbers within the range 2 to (num). Ifno factors are found, it implies that the number is a prime number.

Here's what it looks like:
```
Gimme a number: 23
23 is a prime number.
Gimme a number: 45
45 is not a prime number.
Gimme a number: 11
11 is a prime number.
Gimme a number: 111
111 is not a prime number.
```
As with the "while" loop, Python allows you to add an "else" clause to a"for" loop; it is executed only if the loop is completed withoutencountering a "break" statement even once.

 >>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire