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])
Answer from randyjp on Stack Overflow
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-range.html
Python range() - Stanford Computer Science
The python range(n) function creates a collection of numbers on the fly, like 0, 1, 2, 3 .. n-1. The numbers extend up to, but not including the n, UBNI. The numbers produced by range() are perfect for indexing into collections likes strings and lists.
Discussions

Using range() in accessing elements of an array
outcomes1000[range(0, i)] selects the first i elements of outcomes1000, though that's a very weird way of doing it. Typically, you'd just use slicing, so outcomes1000[:i]. Also, a better way to calculate the cumulative mean rather than with a list comprehension is with np.cumsum: averages = np.cumsum(outcomes1000) / np.arange(1, 1001) More on reddit.com
๐ŸŒ r/learnpython
5
1
April 25, 2022
python - Finding range of a numpy array elements - Stack Overflow
I have a NumPy array of size 94 x 155: a = [1 2 20 68 210 290.. 2 33 34 55 230 340.. .. .. ... ... .... .....] I want to calculate the range of each row, so that I get 94 ranges... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Python] How you would get a range when working with arrays.
r[1] is the 2nd element in the r tuple. In other words, it's the second dimension of x. In this case, the number 4: >>> x = numpy.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) >>> x 7: array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) >>> x.shape 8: (3L, 4L) so this line: for num in range(1,r[1]) is iterating through the columns of x More on reddit.com
๐ŸŒ r/learnprogramming
6
2
January 26, 2018
python - How do I create a list with numbers between two values? - Stack Overflow
How do I create a list of numbers between two values? For example, a list between 11 and 16: [11, 12, 13, 14, 15, 16] More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
H2K Infosys
h2kinfosys.com โ€บ home โ€บ python tutorials โ€บ mastering range of numbers in array with numpy: arange & linspace simplified
Mastering Range of Numbers in Array with Numpy: arange & linspace Simplified
August 13, 2026 - The linspace function in Python, part of the NumPy library, is used to generate an array of evenly spaced numbers over a specified range.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ range-to-a-list-in-python
range() to a list in Python - GeeksforGeeks
September 22, 2026 - In Python, the range() function generates a sequence of numbers and returns a range object. A range object is iterable but is not a list.
๐ŸŒ
Real Python
realpython.com โ€บ python-range
Python range(): Represent Numerical Ranges โ€“ Real Python
2 weeks ago - In Python, the range() function generates a sequence of numbers, often used in loops for iteration. By default, it creates numbers starting from 0 up to but not including a specified stop value. You can also reverse the sequence with reversed().
Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ how-to-use-numpy-arange
NumPy arange(): How to Use np.arange() โ€“ Real Python
March 18, 2026 - NumPy is the fundamental Python library for numerical computing. Its most important type is an array type called ndarray. NumPy offers a lot of array creation routines for different circumstances. arange() is one such function based on numerical ranges.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-select-a-range-of-numbers-in-an-array-in-Python
How to select a range of numbers in an array in Python - Quora
Answer (1 of 2): [code]import numpy as np a = np.arange(1, 11) #Creating an array of 10 elements print(a) [/code]Now if you want elements from index 2 to 5(remember indexing starts from 0), so basically we want elements 3, 4, 5 and 6. For this use: [code]a[2:6] [/code] > a[ index you want to sta...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_range.asp
Python range() Function
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.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python range() function: float, list, for loop examples
Python range() Function: Float, List, For loop Examples
October 7, 2026 - Python gives an error as the range() function does not support floating-point numbers for start, stop, and step. In this example, we will use an array of numbers and see how to iterate the array inside a for-loop using range().
๐ŸŒ
ThePythonGuru
thepythonguru.com โ€บ python-builtin-functions โ€บ range
Python range() function - ThePythonGuru.com
At its simplest, it accepts an integer and returns a range object (a type of iterable). In Python 2, the range() returns a list which is not very efficient to handle large data.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ range-to-a-list-in-python
range() to a list in Python - GeeksforGeeks
April 17, 2025 - The simplest and most efficient way to convert a range object into a list is by using the list() constructor. ... This method is fast, concise, and highly readable. List comprehension provides another efficient and Pythonic way to convert a ...
๐ŸŒ
w3resource
w3resource.com โ€บ python โ€บ built-in-function โ€บ range.php
Python range() function - w3resource
April 18, 2026 - Python range() function: The range() function is used to get a sequence of numbers, starting from 0 by default, and increments by 1 by default, and ends at a specified number.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [python] how you would get a range when working with arrays.
r/learnprogramming on Reddit: [Python] How you would get a range when working with arrays.
January 26, 2018 -

I am taking a coding class with python for one of my grad school classes (Bio background). We are working in Spyder3

import numpy

x = numpy.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

print(x[0,2])

r=x.shape
for num in range(1,r[1]):
    print(x[num-1,:])

print("Done!")

The above is the line of code my professor gave us in class. For the life of my I can't figure out where the (1,r[1]) comes from, I understand that r is referring back to what the variable "r" stands for but why are we using 1 to r[1] for the range? I know it stands for column and row of the matrix, the matrix is a 3,4. I hope if this is enough information, if not I will update!

๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.arange.html
numpy.arange โ€” NumPy v2.5 Manual
>>> np.arange(0, 5, 0.5, dtype=np.int_) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) >>> np.arange(-3, 3, 0.5, dtype=np.int_) array([-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]) In such cases, the use of numpy.linspace should be preferred. The built-in range generates Python built-in integers that have arbitrary size, while numpy.arange produces numpy.int32 or numpy.int64 numbers.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-arrange-in-python
numpy.arange() in Python - GeeksforGeeks
February 14, 2026 - numpy.arange() function creates an array of evenly spaced values within a given interval. It is similar to Python's built-in range() function but returns a NumPy array instead of a list.