It is because the input data is string. So it flows to else. Convert it into integer then all will be fine.

>>> number = raw_input("> ")
>>> type(number)
<type 'str'>
int(number) converts to <type 'int'>

Please use:

if int(number) in range(1, 6):
    print "You entered a number in the range of 1 to 5"
elif int(number) in range(6, 11):
    print "You entered a number in the range of 6 to 10"
else:
    print "Your number wasn't in the correct range"
Answer from thegauraw on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_func_range.asp
Python range() Function
Python Examples Python Compiler ... ... The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-range-function
Python range() function - GeeksforGeeks
The range() function in Python is used to generate a sequence of integers within a specified range.
Published   March 10, 2026
Discussions

python - Using in range(..) in an if-else statment - Stack Overflow
You should also use if 1 < number ... is within a range. ... For the sake of completeness, the in operator may be used as a boolean operator testing for attribute membership. It is hence usable in an if..else statement (see below for the full documentation extract). When using the in operator, (e.g obj in container) the interpreter first looks if container has a __contains__ method. If it does not but if the container defines the __iter__ method, python will then ... More on stackoverflow.com
🌐 stackoverflow.com
python - What is the meaning of 'for _ in range() - Stack Overflow
_ is just a normal variable name in Python. By convention it means “I don’t care about this value.” for _ in range(20): do_something() just runs the loop 20 times, ignoring the counter. It’s the same as using i or x, but _ makes it clear you won’t use the loop variable. 2026-03-29T16:06:39.227Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with ... More on stackoverflow.com
🌐 stackoverflow.com
for loop - How does the Python's range function work? - Stack Overflow
When I'm teaching someone programming (just about any language) I introduce for loops with terminology similar to this code example: ... The Python range() function simply returns or generates a list of integers from some lower bound (zero, by default) up to (but not including) some upper bound, ... More on stackoverflow.com
🌐 stackoverflow.com
What is underscore _ in for _ in range(n)? can I use _ in other places?
Yes. That’s exactly what it is. It’s used by convention to show we won’t actually be using the variable. #recommend because we don’t use ‘_’ for _ in range(3): print(“Hello”) #not recommended since we use ‘_’ but valid code for _ in range(3): print(_) # same as above but recommend since we use x for x in range(3): print(x) More on reddit.com
🌐 r/learnpython
38
24
November 4, 2024
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-range.html
Python range() Function
The most common form is range(n), given integer n returns a numeric series starting with 0 and extending up to but not including n, e.g. range(6) returns 0, 1, 2, 3, 4, 5. With Python's zero-based indexing, the contents of a string length 6, are at index numbers 0..5, so range(6) will produce ...
🌐
docs.python.org
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.4 documentation
February 27, 2026 - For example, chr(97) returns the string 'a', while chr(8364) returns the string '€'. This is the inverse of ord(). The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16).
🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
November 24, 2024 - Usually, the numbers are consecutive, but you can also specify that you want to space them out. You can create ranges by calling range() with one, two, or three arguments, as the following examples show:
Find elsewhere
🌐
Coursera
coursera.org › tutorials › python-range
How to Use Range in Python | Coursera
Use start, stop, and step arguments with range() to write a program that returns 100, 75, 50, 25. ... To get the required output, you'd need to set your start value to 100, your stop value to 0, and your step argument to -25. ... Using a negative step in range lets you iterate through a decrementing series. If you want to loop over the series in reverse order, you can use Python’s built-in reversed() function.
🌐
freeCodeCamp
freecodecamp.org › news › python-range-function-explained-with-code-examples
Python range() Function – Explained with Code Examples
October 6, 2021 - ▶ Consider the following example where you call range() with 5 as the argument. And you loop through the returned range object using a for loop to get the indices 0,1,2,3,4 as expected. for index in range(5): print(index) #Output 0 1 2 3 4 · If you remember, all iterables in Python follow zero-indexing.
🌐
Mimo
mimo.org › glossary › python › range-function
Python range() Function [Python Tutorial]
Using range() to iterate over indices rather than elements avoids creating additional lists and optimizes memory. Particularly in large lists of large objects, accessing items by index can be much more efficient with range(). ... Python 2 has a built-in function called xrange() to create ranges.
🌐
freeCodeCamp
freecodecamp.org › news › python-for-loop-for-i-in-range-example
Python For Loop - For i in Range Example
March 30, 2021 - The range() function provides a sequence of integers based upon the function's arguments. Additional information can be found in Python's documentation for the range() function. ... The start argument is the first value in the range.
🌐
DataCamp
datacamp.com › tutorial › python-range-function
Python Range() Function Tutorial | DataCamp
September 16, 2024 - If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course. The range() function returns a sequence of numbers and is immutable, meaning its value is fixed. The range() function takes one or at most three arguments, ...
🌐
Python Morsels
pythonmorsels.com › range
Python's range() function - Python Morsels
January 14, 2025 - Let's talk about Python's range function. ... >>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> for n in numbers: ... print(n) ... 1 2 3 4 5 6 7 8 9 10 · But that could get pretty tedious. Imagine if we were working with 100 numbers...
Top answer
1 of 6
54

A "for loop" in most, if not all, programming languages is a mechanism to run a piece of code more than once.

This code:

for i in range(5):
    print i

can be thought of working like this:

i = 0
print i
i = 1
print i
i = 2
print i
i = 3
print i
i = 4
print i

So you see, what happens is not that i gets the value 0, 1, 2, 3, 4 at the same time, but rather sequentially.

I assume that when you say "call a, it gives only 5", you mean like this:

for i in range(5):
    a=i+1
print a

this will print the last value that a was given. Every time the loop iterates, the statement a=i+1 will overwrite the last value a had with the new value.

Code basically runs sequentially, from top to bottom, and a for loop is a way to make the code go back and something again, with a different value for one of the variables.

I hope this answered your question.

2 of 6
25

When I'm teaching someone programming (just about any language) I introduce for loops with terminology similar to this code example:

for eachItem in someList:
    doSomething(eachItem)

... which, conveniently enough, is syntactically valid Python code.

The Python range() function simply returns or generates a list of integers from some lower bound (zero, by default) up to (but not including) some upper bound, possibly in increments (steps) of some other number (one, by default).

So range(5) returns (or possibly generates) a sequence: 0, 1, 2, 3, 4 (up to but not including the upper bound).

A call to range(2,10) would return: 2, 3, 4, 5, 6, 7, 8, 9

A call to range(2,12,3) would return: 2, 5, 8, 11

Notice that I said, a couple times, that Python's range() function returns or generates a sequence. This is a relatively advanced distinction which usually won't be an issue for a novice. In older versions of Python range() built a list (allocated memory for it and populated with with values) and returned a reference to that list. This could be inefficient for large ranges which might consume quite a bit of memory and for some situations where you might want to iterate over some potentially large range of numbers but were likely to "break" out of the loop early (after finding some particular item in which you were interested, for example).

Python supports more efficient ways of implementing the same semantics (of doing the same thing) through a programming construct called a generator. Instead of allocating and populating the entire list and return it as a static data structure, Python can instantiate an object with the requisite information (upper and lower bounds and step/increment value) ... and return a reference to that.

The (code) object then keeps track of which number it returned most recently and computes the new values until it hits the upper bound (and which point it signals the end of the sequence to the caller using an exception called "StopIteration"). This technique (computing values dynamically rather than all at once, up-front) is referred to as "lazy evaluation."

Other constructs in the language (such as those underlying the for loop) can then work with that object (iterate through it) as though it were a list.

For most cases you don't have to know whether your version of Python is using the old implementation of range() or the newer one based on generators. You can just use it and be happy.

If you're working with ranges of millions of items, or creating thousands of different ranges of thousands each, then you might notice a performance penalty for using range() on an old version of Python. In such cases you could re-think your design and use while loops, or create objects which implement the "lazy evaluation" semantics of a generator, or use the xrange() version of range() if your version of Python includes it, or the range() function from a version of Python that uses the generators implicitly.

Concepts such as generators, and more general forms of lazy evaluation, permeate Python programming as you go beyond the basics. They are usually things you don't have to know for simple programming tasks but which become significant as you try to work with larger data sets or within tighter constraints (time/performance or memory bounds, for example).

[Update: for Python3 (the currently maintained versions of Python) the range() function always returns the dynamic, "lazy evaluation" iterator; the older versions of Python (2.x) which returned a statically allocated list of integers are now officially obsolete (after years of having been deprecated)].

🌐
W3Schools
w3schools.com › python › python_range.asp
Python range
The built-in range() function returns an immutable sequence of numbers, commonly used for looping a specific number of times. This set of numbers has its own data type called range.
🌐
Python.org
discuss.python.org › python help
For loop with undersore - Python Help - Discussions on Python.org
December 7, 2023 - Hello, Can anyone tell me what this type of for loop means for _ in range(n):
🌐
Reddit
reddit.com › r/learnpython › what does "for i in range" mean in python?
r/learnpython on Reddit: What does "for i in range" mean in Python?
September 9, 2019 -

So, I had a question where I was supposed to find the sum of the first natural numbers using Python.

Here's the problem:

Write a program to find the sum of the cubes of the first n natural numbers, where the value of n is provided by the user.

And this is the code that my professor used to allow the interpreter to produce the result:

= int(input("Enter a number:"))

sum = 0

for i in range (n+1): sum = sum + i*i*i

# for i in range starts as a loop, and then tries to get to (n + 1), whatever that may be

print("the sum of the first", n, "integers is", sum)

However, I can't seem to understand what "for i in range (n + 1): sum = sum + i * i * i" means. In other words, I don't understand what role this part of the code is doing to produce the result. Especially, I don't understand what the role of "for i in range (n + 1)" is doing. Does anyone mind explaining this to me?

Top answer
1 of 6
23
Use a 'code block' in the 'new' reddit to paste code so we can see indents which are vital to python! for i in range(n + 1): sum = sum + i * i * i when you say 'for i in range(n + 1)' you are creating a variable called 'i' and setting it equal to the first value in the second part of the line called range(). every time the loop loops, i becomes the next value in the second part of the line (in this case range()) Check this code out to understand it: my_list = ['potato', 'pineapple', 'strawberry', 'banana', 'orange'] for var in my_list: #instead of 'i' i used 'var' you can use any name you want, since you are creating the variable. var is = to a value in my_list, and will go to the next value every time the loop loops. This will run a total of 5 times, because there are 5 items in the list we are looping through (my_list) print(var) now put that in your terminal/whatever and see the output, itll look like this: potato pineapple strawberry banana orange its the same with range() -- example: range(5) just means every number between 0 and 5, including 0 but not 5. so range(5) has 5 items, 0, 1, 2, 3 and 4. our loop should run 5 times: for i in range(5): print(i) you should see the output: 0 1 2 3 4 if you ever want to know more about certain parts of python, google it, for example: 'python range()' will give you tons of results that are helpful.
2 of 6
2
It means that you're looping through the function body n times. So, for i in range(n) means that you're going to do something n times. For example: for i in range(10): print(n) means you're going to print the numbers 0 to 9, because the range function goes from 0 to n-1 if you provide a single argument, and from a to b if you provide two, as in range(a, b)