chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Introduction to Python Range() Function filter_list
Author
Message
Introduction to Python Range() Function #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:

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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

Introduction to Python Range() Function #2
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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Introduction to Python Range() Function #3
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-Cont...#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
[Image: OilyCostlyEwe.gif]

Reply

RE: Introduction to Python Range() Function #4
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-Cont...#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
[Image: OilyCostlyEwe.gif]

Reply

RE: Introduction to Python Range() Function #5
(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-Cont...#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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Introduction to Python Range() Function #6
(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-Cont...#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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Introduction to Python Range() Function #7
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.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Introduction to Python Range() Function #8
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.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Introduction to Python Range() Function #9
(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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Introduction to Python Range() Function #10
(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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply






Users browsing this thread: 1 Guest(s)