Hello readers, in this module we are going to learn Data Types and Shape of Array in NumPy. Also feel free to read my previous blogs on Software Testing, Basics of Python and Java, etc. to get more insightful technical content. Before going to this module suggest you go with Part 1 and Part 2 to know more about Numerical Python. Click here to read more.
What is Data Type?
Data Types are different forms of values stored in a variable. For Example – A value can be a string, integer, float, Boolean and more.
In NumPy we have some additional data types and we can refer to the data type with that character. For Example, here are a list of references including data type:
- i - refers to integer
- b – refers to Boolean
- u - refers to unsigned integer
- f - refers to float
- c- refers to complex float
- M - refers to datetime
- M - refers to timedelta
- O - refers to object
- S - refers to String
- U - refers to Unicode string.
Now, how will you check what data type of an array?
In NumPy we have a unique property called dtype that returns the data type of an array.
import numpy arr=numpy.array([1,2,3,4]) print(arr.dtype)
So this is how you check the type of data. Similarly we can find this with multiple data types.
The shape of an Array:
In NumPy, we have a special attribute called shape which returns a tuple with each index belonging to their corresponding elements.
import numpy arr=numpy.array([[1,2,3,4],[5,6,7,8]]) print(arr.shape)
Note - It simply says we have 2 dimensions and each dimension has 4 elements inside it.
Similarly, you can use as many as dimensions you want and you can get it using the shape function.
So that we have learnt data types and shape, let's learn more in the final module and you will be able to get a basic idea of how NumPy works with basic functions. Keep exploring and Keep Learning more.
By Ahamed Basha N
LetsUpgrade Elite Team Member.