-3

I am working with numpy. I encountered this line of code.

a = (1.,80.,5.)

What does this mean? At some other line, I found

aList = np.arange(a[0], a[1]+a[2], a[2])

Note: np is namespace assigned from numpy.

  • 1
    Have a read of https://docs.python.org/3/tutorial/datastructures.html this will help to understand the basics of lists, dictionaries, tuples, and sets – jhole89 Sep 08 '16 at 15:17

5 Answers5

2

For the first code segment you are creating a tuple with 3 numbers 1, 80 and 5 in this.

a=(1.,80.,5.)
1.0, 80.0, 5.0)

In the second code segment you are arranging a list with evenly spaced values from 1 to 81 (because you are adding a1 and a2) with intevals of 5.

np.arange(a[0], a[1]+a[2], a[2]) 
array([  1.,   6.,  11.,  16.,  21.,  26.,  31.,  36.,  41.,  46.,  51.,
        56.,  61.,  66.,  71.,  76.,  81.])

From the numpy help

numpy.arange ([start, ]stop, [step, ]dtype=None)

Return evenly spaced values within a given interval.

Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an ndarray rather than a list.

EDIT As a.smiet pointed out the code creates a tuple and not a list. There are differences between the two as pointed out here

Community
  • 1
  • 1
zglin
  • 2,891
  • 2
  • 15
  • 26
  • Actually the first segment creates a tuple, not a list. And the second line creates evenly spaced values from 1 to 81 and not 80... – a.smiet Sep 08 '16 at 14:37
1

a is a tuple of floats. A tuple is a kind of structure that is kinda like a list, but is immutable (i.e. you cannot modify any of its components once it has been created). But, like a list it can be indexed.

In theory, some tuples have special names, for example a tuple of 2 is called a pair, a tuple of 3 is called a triplet etc (people don't necessarily call them that, but it helps a bit more to understand what a tuple is about). Because it's immutable, conceptually it is thought of more as a unique object, rather than as a collection of ones; for this reason it can also be validly used as a key to a dictionary (as opposed to lists which cannot).

To create a tuple, you create a comma-separated sequence of objects inside parentheses, i.e. () (as opposed to brackets, i.e. [] that you would to create a list).

As for floats, the float 3.0 can also be written 3. for short.

The numpy.arange function then creates a range by calling it using the components of the tuple as arguments. In your particular case, it will create a range of numbers from 1 to 80+5, at increments of 5.

A very cool use of tuples is that they can be expanded into a sequence of arguments to a function. e.g. if you had a tuple a = (1.,10.,2.), and you wanted to call numpy.arange(a[0], a[1], a[2]), you could just do numpy.arange(*a) instead.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
0

First line is just a tuple.

Second line is using the np.arange method which returns venly spaced values within a given interval:

np.arange(start, stop, step)

The parameters you have are using the tuple, a. Where a[0] = 1 and a[1] = 80 so on...

turnip
  • 2,246
  • 5
  • 30
  • 58
0
a = (1.,80.,5.)

Creates a tuple of 3 floats (1.0, 80.0 and 5.0).

aList = np.arange(a[0], a[1]+a[2], a[2])

Created this list:

[  1.   6.  11.  16.  21.  26.  31.  36.  41.  46.  51.  56.  61.  66.  71. 76.  81.]

Which, according to http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html means thats 1.0 is a start, 85.0 (which is 80+5) is an end and 5.0 is a step (parameters of the function) for creating spaced values.

grael
  • 657
  • 2
  • 11
  • 26
0

For the first one, it is a tuple of 3 items:

>>> a = (1.,80.,5.)
>>> a
(1.0, 80.0, 5.0)

For the second one, it generates a list (start: 1.0, end: 80.0 + 5.0, step: 5.0):

>>> a_list = numpy.arange(a[0], a[1]+a[2], a[2])
>>> a_list
array([  1.,   6.,  11.,  16.,  21.,  26.,  31.,  36.,  41.,  46.,  51.,
        56.,  61.,  66.,  71.,  76.,  81.])
ettanany
  • 19,038
  • 9
  • 47
  • 63