Okay, so after the whole incident two years ago with a really terrible computer science GTA teaching C++ in the summer I have decided to give programming another chance. But this time I am teaching myself Python at my own pace and I am currently using an intro book. And I have a question for you Python experts on a simple question.

My question is, why do i have to use the (10, 110, 10) on my code? I am currently learning Python 3.6 on Mac.
for i in range(10, 110, 10)

I had this originally:

    # Programming Exercise 5:
    # 5.) Modify the convert.py program (Section 2.2) so that it computes and prints
    #     a table of Celsius temperatures and the Fahrenheit equivalents every 10
    #     degrees from 0°C to 100°C.
    #
    #-------------------------------------------------------------------------------

    # convert.py
    #   A program to convert Celsius temps to Fahrenheit
    # by: Renee Yaldoo

    def main():
        print("This program computes and prints a table of Celsius temperatures")
        print("and the Fahrenheit equivalents every 10 degrees from 0°C to 100°C.")
        for i in range(10):
            celsius = i
            fahrenheit = 9/5 * celsius + 32

            print(str(celsius)+"\t"+str(fahrenheit))

        input("Press the <Enter> key to quit.")

    main()

But this would be my output when the program would run:

This program computes and prints a table of Celsius temperatures
and the Fahrenheit equivalents every 10 degrees from 0°C to 100°C.
0 32.0
1 33.8
2 35.6
3 37.4
4 39.2
5 41.0
6 42.8
7 44.6
8 46.4
9 48.2
Press the <Enter> key to quit.

Can someone here please explain why I need to use (10, 110, 10)? I haven't gotten too far into the book I am using (Python Programming 3rd Edition by John Zelle). I'm only on Chapter 2 right now. Thank you for taking the time reading and responding to this. Happy Holidays, and Merry Christmas everyone! :)

Recommended Answers

All 9 Replies

Well, range(10) contains the numbers 0 1 2 3 4 5 6 7 8 9 while range(10, 110, 10) contains 10 20 30 40 50 60 70 80 90 100 which is whats the program purports to convert.

Right but I'm still trying to wrap my head around of why I need that 110 and the extra 10 in the parenthesis. I'm not seeing how it causes the code to go 10 20 30 40 50 60 70 80 90 100.

INABIAF. It's not a bug. It's a feature. As much as it has been "explained" as to how this makes programming easier I still consider it a failing of the language. Range generates numbers starting at the first number and proceeding by the increment up to but not including the second number. So in order to get the numbers from 1 to 10 you have to use range(1,11). I always favour clarity over everything else so this design choice offends me.

That's where we completely disagree. I think it is one of the best design choices in the python language.

In mathematics, when a range is specified as (a,b) it is referred to as an open range and does not include the endpoints. When it is specified as [a,b] it is a closed range and does include the endpoints. A programming language should be consistent. When I see (a,b) I expect (and rightly so) to see consistent behaviour. A desired feature of a language is that the expected behaviour should be the actual behaviour. I accept that this is the way Python was designed. I maintain my opinion that it is wrong, no matter how convenient that behaviour is.

Perhaps they should have allowed range(a,b), range[a,b], range(a,b] and range[a,b). That would be consistent with mathematical definitions and would still allow the convenience for coding.

There are many programming tricks that are convenient but are still strongly discouraged because they are lack clarity. But this is probably not the thread to hold this debate (again).

commented: Endpoints. Gotta love 'em. +11

Even though I am clearly debating ^_^

Gribouillis, how does range(10, 110, 10) contain 10 20 30 40 50 60 70 80 90 100? I'm not seeing this :(

You can have a look at this link
Click Here

Basically, I think, range(10,110,10) is starting from 10, add 10 at every step, up to 110 excluding 110 itself.

Thank you so much charis89! Now I see what is going on inside the parameters. Thank you again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.