When you call range() with two arguments, the first argument is the starting number, and the second argument is the end (non-inclusive). So you're starting from len(list_of_numbers), which is 5, and you're ending at 6. So it just prints 5.
To get the results you want, the starting number should be 0, and the end should be len(list_of_numbers)+1. If you call it with one argument, that's the end, and 0 is the default start. So use
for i in range(len(list_of_numbers)+1):
or if you want to pass the start explicitly:
for i in range(0, len(list_of_numbers)+1):
Answer from Barmar on Stack Overflowpython - how to specify range if we are taking an array in for loop - Stack Overflow
Understanding Range() function in python. [Level: Absolute Beginner]
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
language design - What is the reason python uses range in for loops? - Software Engineering Stack Exchange
When you call range() with two arguments, the first argument is the starting number, and the second argument is the end (non-inclusive). So you're starting from len(list_of_numbers), which is 5, and you're ending at 6. So it just prints 5.
To get the results you want, the starting number should be 0, and the end should be len(list_of_numbers)+1. If you call it with one argument, that's the end, and 0 is the default start. So use
for i in range(len(list_of_numbers)+1):
or if you want to pass the start explicitly:
for i in range(0, len(list_of_numbers)+1):
range gives you and iterator between (start, end) end not included.
So in your case the iterator is (start=len(list_of_numbers), end=6).
Since len(list_of_numbers) = 5, this translates to range(5,6) which is 1 element, 5, since 6 is excluded.
https://docs.python.org/3/library/functions.html#func-range
There's various routes that you can take. Assuming you want to print the first 3 elements of your array, here's two:
array=[1,2,3,4,5]
1) Slice the array
# start at index 0 and go until index 2
for record in array[0:3]:
print(record)
2) Use a variable to iterate over the array
# range(3) = [0, 1, 2]
for i in range(3):
print(array[i])
In this code part-
array=[1,2,3,4,5]
for record in range(0,2,array):
print(record)
The range function takes 3 parameters. First one is starting index,second one is ending index and the third one is the gap in which you want the numbers to be. So,going by an example-
for i in range(0,5,2): print(i)
This code will print 0,2,4 as answer. The range function will start from 0 and will return every 2nd number till 4(not 5,because 5 is excluded).
Your code gives an error because you have mentioned an array as your 3rd parameter but it is expecting an integer
for record in range(0,2,len(array)):
print(record)
You could try this,but it will only return 0 because essentially this for loop simplifies to-
for record in range(0,2,5)
So after returning the starting number it will try to get the next 5th number which you dont have(as 2nd parameter is 2). Hope this clarifies.
I first thought that it just returns numbers and you can do a lot of stuff with those numbers but now I am seeing that it is being used to run a loop a specific number of times. It's just not making sense to me why this works, like I can't intuitively understand it.
Tho I have started using in my code, an explanation on why it was developed this way would help.
Note that range() is not actually part of the python language; it is a function. Having range be a function means you can plug any other function into a "for in" loop, including functions that don't increase monotonically, functions that lazy-execute, and functions that replace range() with something that better satisfies your personal sensibilities.
In other words, python's way affords you tremendous flexibility to do it however you want.
Your example is not exactly a fair one. It describes a scenario that would be very rare in practice. For a dice roll, it is simply
for x in range(6)
There are multiple different ways to approach this answer (bold emphasis mine):
What is the reason python uses range in for loops?
This is not a for loop. It is a foreach loop. I.e. it is not a loop that loops over a pre-defined set of loop indices, it is an iterator that iterates over the elements of a collection.
In particular, in
for e in [2, 3, 5, 7, 11, 13, 17]:
print(e)
The result will not be
0
1
2
3
4
5
6
but
2
3
5
7
11
13
17
What is the reason python uses range in for loops?
It doesn't. It uses an arbitrary expression. More precisely, an arbitrary expression that evaluates to an iterator or to something that can be implicitly converted to an iterator (such as an iterable):
class MyIterator:
def __init__(self):
self.counter = -1
self.lost = [4, 8, 15, 16, 23, 42]
def __next__(self):
self.counter += 1
if self.counter == 6:
raise StopIteration
return self.lost[self.counter]
class MyIterable:
def __iter__(self):
return MyIterator()
my_iterable = MyIterable()
for num in my_iterable:
print(num)
# 4
# 8
# 15
# 16
# 23
# 42
Is there some philosophical reasoning behind why python uses this syntax
Yes: It is more general and thus makes the language simpler. The BASIC for loop can do one thing and one thing only: loop over a pre-defined set of loop indices. In fact, it is even more limited than that, because there are further restrictions on the loop indices: they need to be monotonically increasing or decreasing with a fixed step size.
If you want the indices to be non-monotonic, you need a new language construct. If you want the indices to have varying step sizes, you need a new language construct. If you want to iterate over the elements of a collection, you need a new language construct.
With Python's foreach loop, you can simply have a function that generates indices in whatever order you want, and loop over those. You can iterate over the elements of any arbitrary collection, and note that "collection" is interpreted very broadly.
Actually, you can iterate over the elements of any arbitrary iterator. An iterator can be something very general, and it doesn't even have to be finite, e.g. "all prime numbers".
As I have shown above, it is very easy to create your own custom iterators and iterables. It is in fact even more easy using generator functions:
def my_generator():
yield 4
yield 8
yield 15
yield 16
yield 23
yield 42
for num in my_generator():
print(num)
# 4
# 8
# 15
# 16
# 23
# 42
And even more easy with generator expressions.
If Python is supposed to be more human readable than most languages, in this case particular case it seems to be worse than say Sinclair BASIC.
If you are looping over loop indices in Python (or any modern language, really), you are doing it wrong.
You should be using higher-level iterators instead, such as reduce (you may also know this one under the name fold or more general Catamorphism), accumulate (you may also know this one under the name scan or prefix-sum), cycle, chain, groupby, or product. Or, you should be using list / set / dictionary comprehensions, generator expressions, or algorithms and data structures supplied by the standard library or third-party libraries.
So I'm very new to python. And I've been following a YouTube to learn python. I understood everything except for his last exercise. Shown below
for row in range(5): for col in range(5): print(col, end=' ') print()
This prints out
01234 01234 01234 01234 01234
I just don't understand how the numbers get arranged in that order. Can someone please explain in layman terms, how it works..otherwise I can not proceed to the next step. Thank youu