Sinisterly
Introduction to Python Range() Function - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: Introduction to Python Range() Function (/Thread-Introduction-to-Python-Range-Function)

Pages: 1 2 3


Introduction to Python Range() Function - Ex094 - 07-17-2013

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:

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


Introduction to Python Range() Function - Ex094 - 07-17-2013

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:

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


RE: Introduction to Python Range() Function - Psycho_Coder - 07-17-2013

Short and nice tutorial. But you should have make it a bit more explanatory with other functions like xrange() and explaining how them them in different ways and shortening the code as much as possible.

Have a look at this :- http://www.hackcommunity.com/Thread-Contest-HC-Devs-Official-Project-Euler?pid=146161#pid146161
I am talking about something like the above.

Since you are giving codes so why not execute them in an online compiler like codepad and then give the link also. To be true very less users do actually run these and do. So if you give the links of the executed code then they will atleast see them live and understand the working.

Thank you,
Sincerely,
Psycho_Coder


RE: Introduction to Python Range() Function - Psycho_Coder - 07-17-2013

Short and nice tutorial. But you should have make it a bit more explanatory with other functions like xrange() and explaining how them them in different ways and shortening the code as much as possible.

Have a look at this :- http://www.hackcommunity.com/Thread-Contest-HC-Devs-Official-Project-Euler?pid=146161#pid146161
I am talking about something like the above.

Since you are giving codes so why not execute them in an online compiler like codepad and then give the link also. To be true very less users do actually run these and do. So if you give the links of the executed code then they will atleast see them live and understand the working.

Thank you,
Sincerely,
Psycho_Coder


RE: Introduction to Python Range() Function - Ex094 - 07-17-2013

(07-17-2013, 06:59 AM)Psycho_Coder Wrote: Short and nice tutorial. But you should have make it a bit more explanatory with other functions like xrange() and explaining how them them in different ways and shortening the code as much as possible.

Have a look at this :- http://www.hackcommunity.com/Thread-Contest-HC-Devs-Official-Project-Euler?pid=146161#pid146161
I am talking about something like the above.

Since you are giving codes so why not execute them in an online compiler like codepad and then give the link also. To be true very less users do actually run these and do. So if you give the links of the executed code then they will atleast see them live and understand the working.

Thank you,
Sincerely,
Psycho_Coder

I don't use xrange because of it's lazy evaluation and also as I use Python 3.3, xrange has been replaced with range, so xrange is now range. Thank you for your reply


RE: Introduction to Python Range() Function - Ex094 - 07-17-2013

(07-17-2013, 06:59 AM)Psycho_Coder Wrote: Short and nice tutorial. But you should have make it a bit more explanatory with other functions like xrange() and explaining how them them in different ways and shortening the code as much as possible.

Have a look at this :- http://www.hackcommunity.com/Thread-Contest-HC-Devs-Official-Project-Euler?pid=146161#pid146161
I am talking about something like the above.

Since you are giving codes so why not execute them in an online compiler like codepad and then give the link also. To be true very less users do actually run these and do. So if you give the links of the executed code then they will atleast see them live and understand the working.

Thank you,
Sincerely,
Psycho_Coder

I don't use xrange because of it's lazy evaluation and also as I use Python 3.3, xrange has been replaced with range, so xrange is now range. Thank you for your reply


RE: Introduction to Python Range() Function - Deque - 07-17-2013

You should use xrange because of the lazy evaluation. Biggrin
But you have a point, if Python 3 replaced range with it. They made a good decision there.
Maybe you should make clear which Python version you are talking about in your tutorial.

Thanks for this tutorial.


RE: Introduction to Python Range() Function - Deque - 07-17-2013

You should use xrange because of the lazy evaluation. Biggrin
But you have a point, if Python 3 replaced range with it. They made a good decision there.
Maybe you should make clear which Python version you are talking about in your tutorial.

Thanks for this tutorial.


RE: Introduction to Python Range() Function - Ex094 - 07-17-2013

(07-17-2013, 08:56 AM)Deque Wrote: You should use xrange because of the lazy evaluation. Biggrin
But you have a point, if Python 3 replaced range with it. They made a good decision there.
Maybe you should make clear which Python version you are talking about in your tutorial.

Thanks for this tutorial.

Hehehe, Thanks Deque. Added the Python version


RE: Introduction to Python Range() Function - Ex094 - 07-17-2013

(07-17-2013, 08:56 AM)Deque Wrote: You should use xrange because of the lazy evaluation. Biggrin
But you have a point, if Python 3 replaced range with it. They made a good decision there.
Maybe you should make clear which Python version you are talking about in your tutorial.

Thanks for this tutorial.

Hehehe, Thanks Deque. Added the Python version