VOOZH about

URL: https://www.geeksforgeeks.org/python/basics-of-numpy-arrays/

⇱ Basics of NumPy Arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Basics of NumPy Arrays

Last Updated : 16 Jun, 2026

NumPy stands for Numerical Python and is used for handling large, multi-dimensional arrays and matrices. Unlike Python's built-in lists NumPy arrays provide efficient storage and faster processing for numerical and scientific computations. It offers functions for linear algebra and random number generation making it important for data science and machine learning.

Types of Array

1. One Dimensional Array: stores elements in a single row. It is the simplest type of NumPy array and is commonly used to represent a sequence of values.

👁 maximum_sum_in_circular_array_such_that_no_two_elements_are_adjacent_using_dp_
One Dimensional Array

Example: The following example creates a one-dimensional NumPy array from a Python list.


Output
List: [1, 2, 3, 4]
Numpy Array: [1 2 3 4]
<class 'list'>
<class 'numpy.ndarray'>

2. Multi-Dimensional Array: stores data in two or more dimensions. The most common type is a two-dimensional array, which organizes data into rows and columns.

👁 ARRAY-2
Two Dimensional Array

Example: The following example creates a two-dimensional NumPy array from multiple Python lists.


Output
[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]

Explanation:

  • Each inner list represents a row in the array.
  • np.array() combines the lists into a two-dimensional NumPy array.

Parameters of a Numpy Array

1. Axis: represents a dimension of an array.

Axis 0 -> Rows (first dimension)
Axis 1 -> Columns (second dimension)
Axis 2 -> Third dimension in a 3D array

For example, a two-dimensional array has Axis 0 and Axis 1.

2. Shape: indicates the number of elements along each dimension. It is returned as a tuple.


Output
(5, 3)

Explanation: array contains 5 rows and 3 columns. Therefore, arr.shape returns (5, 3)

3. Rank: number of dimensions (axes) it has.

  • A one-dimensional array has rank 1.
  • A two-dimensional array has rank 2.
  • A three-dimensional array has rank 3.

4. Data Type (dtype): property specifies the type of data stored in an array, such as integers, floating-point numbers, or strings.


Output
Data type of array 1: int64
Data type of array 2: float64

Different Ways of Creating Numpy Array

1. numpy.array(): creates a NumPy array from Python sequences such as lists or tuples.


Output
[3 4 5 5]

2. numpy.fromiter(): creates a one-dimensional array from an iterable object.


Output
['G' 'e' 'e' 'k' 's' 'f' 'o' 'r' 'g' 'e' 'e' 'k' 's']

3. numpy.arange(): returns evenly spaced values within a specified range.


Output
[ 1. 3. 5. 7. 9. 11. 13. 15. 17. 19.]

4. numpy.linspace(): returns a specified number of evenly spaced values between two limits.


Output
[ 3 6 10]

5. numpy.empty(): creates an array of a given shape without initializing its values.


Output
[[ 3130309 21840 0]
 [ 0 540950078 1920216673]
 [1886613089 677737327 1918962217]
 [ 679043442 539767131 857746482]]

6. numpy.ones(): creates an array filled with ones.


Output
[[1 1 1]
 [1 1 1]
 [1 1 1]
 [1 1 1]]

7. numpy.zeros(): creates an array filled with zeros.


Output
[[0 0 0]
 [0 0 0]
 [0 0 0]
 [0 0 0]]
Comment
Article Tags:
Article Tags: