BM Balmiki Mandal 04 May 2023 8 views 0 comments Python

"Python with NumPy Tutorial: Introduction to Arrays"

Python with NumPy Tutorial: NumPy Introduction to Arrays

NumPy is a powerful python library used for scientific computing applications. It allows for the manipulation of large, multi-dimensional arrays, and has a wide variety of mathematical functions that can be used to operate on these arrays. In this tutorial, we will introduce NumPy and look at some basic operations that can be performed on NumPy arrays.

What is an Array?

An array is a collection of items, all of the same type, stored in a specific order. In the case of a NumPy array, the items are all of the same type (e.g., integers, floats, etc.). Arrays can be used to store data efficiently and can be manipulated easily, making them ideal for use in mathematical and scientific computing. There are two main types of arrays in NumPy: 1-dimensional and n-dimensional.

1-dimensional Arrays

The first type of array we will look at is a 1-dimensional array. This is essentially a list of items all of the same type. For example, we could create a 1-dimensional array of integers using the following code:

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

This array has 5 elements, each an integer. We can access individual elements of the array by indexing into it. For example, to access the third element we would write:

a[2] # returns 3

We can also access ranges of elements in the array by slicing it. For example, to access the first three elements we would write:

a[0:3] # returns [1,2,3]

n-dimensional Arrays

The other type of array supported by NumPy is an n-dimensional array. These arrays have multiple dimensions and can contain any type of element. For example, we could create a 3-dimensional array using the following code:

b = np.array([[[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]],
               [[10, 11, 12],
                [13, 14, 15],
                [16, 17, 18]]])

This array has 3 dimensions of 3x3 elements, all of which are integers. We can access elements in this array by indexing into it. For example, to access the element at row 0, column 1, depth 0 we would write:

b[0,1,0] # returns 4

We can also use slicing to access ranges of elements in the array. For example, to access the first three elements of depth 1 we would write:

b[:,:,1] # returns [[10,11,12],[13,14,15],[16,17,18]]

Conclusion

In this tutorial, we have introduced NumPy arrays and looked at some basic operations that can be performed on them. We have seen how to create 1-dimensional and n-dimensional arrays and how to access individual elements or ranges of elements from them. With this knowledge, we can begin to explore the more advanced features that NumPy has to offer.

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

What is an Application Programming Interface (API)?

What is an API? An API (Application Program Interface) is a set of protocols, tools, and subroutines used for...

BM Balmiki Mandal 06 Jan 2024
Understand the Difference Between Interpreted and Compiled Stage

What is the Difference Between Interpreted and Compiled Programming Languages? Compiled and interpreted langu...

BM Balmiki Mandal 28 Nov 2023
Executing Python Code: A Comprehensive Guide to Source Code Execution

Exploring the Internal Mechanisms of Python Programming Python is an interpreted language, which means it doe...

BM Balmiki Mandal 28 Nov 2023