NumPy Introduction and Array Creation Practice Questions with Solutions

NumPy is a powerful Python library used for numerical computing. It provides support for arrays and mathematical operations, making data processing faster and more efficient. In this practice set, you’ll learn the basics of NumPy by creating arrays and working with simple array operations. NumPy Introduction and Array creation practice questions with solutions help to build concepts.


1. Python Program to Import NumPy

Problem Statement

Write a Python program to import the NumPy library and print its version.

Python Solution

import numpy as np

print(np.__version__)

Sample Output

2.3.1

Note: The version number may be different depending on the installed NumPy version.

Explanation

The import numpy as np statement imports the NumPy library using the alias np. The __version__ attribute displays the installed version of NumPy.

Concepts Covered

  • Import NumPy
  • NumPy Version
  • __version__

2. Python Program to Create a One-Dimensional NumPy Array

Problem Statement

Write a Python program to create a one-dimensional NumPy array containing the numbers 10, 20, 30, 40, and 50.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers)

Sample Output

[10 20 30 40 50]

Explanation

The np.array() function converts a Python list into a NumPy array. A one-dimensional array stores elements in a single row.

Concepts Covered

  • np.array()
  • One-Dimensional Array
  • NumPy Array

3. Python Program to Create a Two-Dimensional NumPy Array

Problem Statement

Write a Python program to create a two-dimensional NumPy array.

Python Solution

import numpy as np

numbers = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(numbers)

Sample Output

[[1 2 3]
 [4 5 6]]

Explanation

A two-dimensional array contains rows and columns. It is commonly used to represent tables and matrices.

Concepts Covered

  • Two-Dimensional Array
  • Rows and Columns
  • np.array()

4. Python Program to Create a Three-Dimensional NumPy Array

Problem Statement

Write a Python program to create a three-dimensional NumPy array.

Python Solution

import numpy as np

numbers = np.array([
    [
        [1, 2],
        [3, 4]
    ]
])

print(numbers)

Sample Output

[[[1 2]
  [3 4]]]

Explanation

A three-dimensional array is an array that contains one or more two-dimensional arrays. It is useful for storing complex datasets.

Concepts Covered

  • Three-Dimensional Array
  • Multi-Dimensional Array

5. Python Program to Check the Type of a NumPy Array

Problem Statement

Write a Python program to check the type of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30])

print(type(numbers))

Sample Output

<class 'numpy.ndarray'>

Explanation

Every NumPy array is an object of the numpy.ndarray class. The type() function is used to check the object’s data type.

Concepts Covered

  • numpy.ndarray
  • type()
  • NumPy Array Object

6. Python Program to Find the Number of Dimensions of a NumPy Array

Problem Statement

Write a Python program to print the number of dimensions of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([[10, 20], [30, 40]])

print(numbers.ndim)

Sample Output

2

Explanation

The ndim attribute returns the number of dimensions of a NumPy array. A one-dimensional array returns 1, a two-dimensional array returns 2, and so on.

Concepts Covered

  • ndim
  • Number of Dimensions
  • NumPy Arrays

7. Python Program to Create a NumPy Array with a Specific Data Type

Problem Statement

Write a Python program to create an integer NumPy array using the dtype parameter.

Python Solution

import numpy as np

numbers = np.array([1, 2, 3, 4], dtype="int32")

print(numbers)
print(numbers.dtype)

Sample Output

[1 2 3 4]
int32

Explanation

The dtype parameter specifies the data type of the array elements. It helps optimize memory usage and ensures consistent data storage.

Concepts Covered

  • dtype
  • Integer Data Type
  • NumPy Array

8. Python Program to Create a NumPy Array of Floating-Point Numbers

Problem Statement

Write a Python program to create a NumPy array containing floating-point numbers.

Python Solution

import numpy as np

numbers = np.array([10.5, 20.7, 30.9])

print(numbers)

Sample Output

[10.5 20.7 30.9]

Explanation

NumPy automatically detects the data type based on the values provided. Since all elements are decimal numbers, the array is created with a floating-point data type.

Concepts Covered

  • Float Array
  • Decimal Values
  • NumPy Arrays

9. Python Program to Create a NumPy Array of Strings

Problem Statement

Write a Python program to create a NumPy array containing string values.

Python Solution

import numpy as np

languages = np.array(["Python", "Java", "C++"])

print(languages)

Sample Output

['Python' 'Java' 'C++']

Explanation

NumPy arrays can also store string values. All elements in the array belong to the same string data type.

Concepts Covered

  • String Array
  • NumPy Arrays

10. Python Program to Create an Empty NumPy Array

Problem Statement

Write a Python program to create an empty NumPy array containing five elements.

Python Solution

import numpy as np

numbers = np.empty(5)

print(numbers)

Sample Output

[1.136e-313 0.000e+000 6.923e-310 6.923e-310 2.371e-322]

Note: The values shown above are random memory values. Your output may be different.

Explanation

The np.empty() function creates an array without initializing its elements. It is faster than other array creation methods because it does not assign default values.

Concepts Covered

  • np.empty()
  • Empty Array
  • Memory Allocation

Chapter Summary

In this chapter, you learned the fundamentals of NumPy and how to create different types of arrays. You also explored array dimensions, data types, and commonly used functions for beginners. These concepts provide the foundation for learning advanced NumPy operations.


Key Takeaways

  • NumPy is a powerful library for numerical computing in Python.
  • Use import numpy as np to import the NumPy library.
  • The np.array() function creates NumPy arrays from Python lists.
  • NumPy supports one-dimensional, two-dimensional, and multi-dimensional arrays.
  • The ndim attribute returns the number of dimensions of an array.
  • The dtype parameter specifies the data type of array elements.
  • The np.empty() function creates an array without initializing its values.

Frequently Asked Questions (FAQs)

1. What is NumPy in Python?

NumPy is an open-source Python library used for numerical computing. It provides fast and efficient support for arrays, matrices, and mathematical operations.


2. Why is NumPy faster than Python lists?

NumPy arrays store data in contiguous memory and are optimized using low-level programming languages like C, making operations much faster than Python lists.


3. How do I install NumPy?

You can install NumPy using the following command:

pip install numpy

4. What is a NumPy array?

A NumPy array is a collection of elements of the same data type stored efficiently in memory. It is the core data structure of the NumPy library.


5. What is the difference between a Python list and a NumPy array?

Python lists can store different data types and are more flexible, while NumPy arrays store the same data type and provide much faster mathematical operations.


6. What is the use of the ndim attribute in NumPy?

The ndim attribute returns the number of dimensions in a NumPy array. It helps identify whether an array is 1D, 2D, or multi-dimensional.


7. When should I use NumPy?

You should use NumPy when working with numerical data, scientific computing, data analysis, machine learning, artificial intelligence, or large datasets that require high-performance calculations.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top