Introduction to Python Range() Function 07-17-2013, 06:23 AM
#1

In this tutorial we will be learning the usage of the Range() function in Python because I didn't explain this in my python tutorial so here it is.

I'll be using Python 3.3

Sometimes we require to generate a list of numbers for a particular work. For example, you require a list of numbers from 1 to 15 so that from them you can pick out the numbers divisible by 5 only. For that we use the Range function, the syntax of range function is:

or

Simple as that, So for our program it would be like:

You can also just input the end point of your range like:

In this way you’ll get all the numbers till 15

The range function is also useful when you want to iterate over a sequence or list of numbers, Like:

The output will be > 1 2 3 4 5 6 7 8 9 10 11 12 13 14

You see that the output is in the form of list of numbers in the range 1 to 15

Lets get little a little higher, We’ll solve the Problem 1 of Project Euler, The problem is to state the sum of all the multiples of 3 or 5 below 1000.

Here’s our pseudo code:

Explanation: First we shall iterate over a list of numbers ranging from 1 to 1000 (We will use the range function), then we shall assign conditional statements to check whether the number is divisible by 3 or 5. Here we shall use the OR operator because the problem states that “multiples of 3 OR 5“. If the number is divisible then it will be added to the variable assigned and if not then it will neglect that value. In the end the program will print the sum of all the multiples

Here’s our python implementation:

And the answer comes out exact to be 233168

And that’s it, I hope this tutorial helps!

Regards,

Ex094

I'll be using Python 3.3

Sometimes we require to generate a list of numbers for a particular work. For example, you require a list of numbers from 1 to 15 so that from them you can pick out the numbers divisible by 5 only. For that we use the Range function, the syntax of range function is:

Code:

`range(start, end)`

or

Code:

`range(end)`

Simple as that, So for our program it would be like:

Code:

`range(1, 15)`

You can also just input the end point of your range like:

Code:

`range(15)`

In this way you’ll get all the numbers till 15

The range function is also useful when you want to iterate over a sequence or list of numbers, Like:

Code:

`for num in range(1, 15):`

print(num)

The output will be > 1 2 3 4 5 6 7 8 9 10 11 12 13 14

You see that the output is in the form of list of numbers in the range 1 to 15

Lets get little a little higher, We’ll solve the Problem 1 of Project Euler, The problem is to state the sum of all the multiples of 3 or 5 below 1000.

Here’s our pseudo code:

Code:

`generate numbers in range 1 to 1000`

if the number is divisible by 3 'or' is divisible by 5

add it to the variable sum

else

neglect that value

print the sum of the values

Explanation: First we shall iterate over a list of numbers ranging from 1 to 1000 (We will use the range function), then we shall assign conditional statements to check whether the number is divisible by 3 or 5. Here we shall use the OR operator because the problem states that “multiples of 3 OR 5“. If the number is divisible then it will be added to the variable assigned and if not then it will neglect that value. In the end the program will print the sum of all the multiples

Here’s our python implementation:

Code:

`sum = 0`

for i in range(1, 1000):

if i % 3 == 0 or i % 5 == 0:

sum += i

print("The sum of multiples of 3 or 5 between 1 to 1000 is: %d" % sum)

And the answer comes out exact to be 233168

And that’s it, I hope this tutorial helps!

Regards,

Ex094