Hey everyone, I welcome you all to this new module.
So in this video I want to discuss about something known as arrays.
Now, up till now in this course, you must have learned the basics of Python.
Along with that, you must have learned about different data structures which are present in Python,
like the inbuilt data structure where we learned about list sets, tuple dictionaries as well as their
real world use cases.
Okay.
Now, many of you might have heard about arrays because they are the basic data structure in languages
such as C plus, plus and Java.
Now Python is written on top of C plus plus and the list which you studied that has actually been extended
on top of the properties which a basic array provide.
So it is very important to understand what is an array, so that we can see why we needed a list in
Python as well as going forward, we will be learning about many data structures those data structures,
many of them are in a way made so that we can cover up the shortcomings of array.
Okay, so let us see that once everyone an array is a collection of elements stored in contiguous memory
location.
So that's the main definition.
Array is a collection of elements stored in a contiguous memory location.
So contiguous means contiguous here.
And what does that effectively mean is this.
So we have a memory space in this let's say this space start from 100 okay.
So each and everything is stored in memory.
Or you declare in Python that actually gets assigned a memory or an address where it is stored.
Okay.
Now on this address, let's say each element, let's suppose you are storing integer in that.
So let's say we support for here or let's say nine here.
Then this particular element is going to take up, say, four bytes.
So the next one is going to start at 104.
When we talk about data structures and some complex programming, we have to think in terms of these
addresses and memory.
Okay.
So going forward we will be learning about each and every complexity involved in a very good way so
that you have a proper idea that why do we need to understand that.
Now an array also elements of array are of same data type.
So unlike in list where you were adding different different types.
So you were able to add in list a integer, then you can add a float.
Then you can add a string.
So just to help you revise, let me go back.
Go once to the VS code and let us try to understand that.
For example, if I am defining an L one, which is a list here.
Now in this list I can append, let's say an integer.
Then I can append A float and I can append a string as well.
If I print L1, it's not going to give me any error.
It's going to just show me each and every thing.
So let me just print it.
Yeah.
11.5 and Python right.
They are of different type.
Unlike this in an array all the elements are of same data type.
So they should be having the exact same data type.
If you are trying to show integer you can just store an integer.
If float then just the float.
And of course if string then that can just be stored above all array.
Also allow for efficient access and manipulation of data.
So just like we are able to use the indexing in a list okay, so I can just easily write L1 of say zero,
it's going to give me one.
So it's a quick access right.
Efficient access.
And I can change it as well.
So manipulation can also be done.
The same thing we can do on array as well.
Now list in Python is actually extended on the basic C plus plus array.
And the reason it is very important to understand arrays as well in Python is to make sure that you
are aware that all the data structures, everything which you are going to study forward, why those
has to be created.
What were the shortcomings in my initial arrays, which many languages support?
Okay, so that's the basic idea behind arrays.
Now let us talk about few disadvantages which we can see an array gives and which list was able to help
us out.
So the very first one is that array is of fixed size.
And their property I would rather not take them as disadvantage.
Uh array is actually of fixed size.
So whenever you are creating an array we have to specify the overall size which we are, which we want.
So let's say if you are saying that hey I want an array of size five.
Then you cannot store more than five elements in this array.
Okay.
And you also specify the type.
So it is homogeneous in nature.
Homogeneous in nature.
What does that mean.
Effectively it if it is storing integer then it will just store integer.
If it is storing a float then just the float.
If string then just the string.
Okay.
So that is something which is the property of my arrays.
Now moving forward let us see about list as well.
So in Python we use list instead of arrays as they provide more flexibility.
Now you must have seen that we don't have any such or any such overall shortcoming or any such negatives
in list, right.
So we use list in Python instead of arrays because they provide more flexibility.
However, Python provides ability to use arrays via libraries such as numpy and array.
So numpy is a very famous library for mathematical computation.
Computation, which we normally learn in data analytics or data science.
So it is not required here honestly.
And it is also a library.
Now one thing you have to understand is that yes list is in the back end being written on top of arrays,
the basic arrays of C plus plus.
But it is not a built in data type in Python.
So what do I mean by built in data type or data structure?
The one which Python provides by default.
Now if we want to use anything which is not provided by default, then we have to import that from somewhere.
Okay, just like we are importing someone else code okay, we can use someone else's code from any website.
We can use a full data structure or full code which is defining something into our overall code as well.
So this is something which is importing.
So we import many libraries while we are coding, so that we can make sure that we can expand and make
our code more flexible.
So just to showcase that to you, everyone, let us go back once more to VS code.
Now if I write help here, it's going to open a help window.
So let me just run this.
Yeah.
See.
So this is a help window which is created.
Let me just clear everything.
One minute.
Let us just run this again.
Yeah.
So here it is saying that okay.
We have not closed the help.
So let me just now run it.
Great everyone.
So now you will see that help has opened.
Let me zoom out in this I can write a built in.
Okay.
I think it built ins.
Yeah.
So built in is actually all the.
It's a module which provides direct access to all built in identifiers of Python.
Okay, so all the things which you have learned actually will be defined here.
So if I just zoom out a little bit more you will see that exceptions like these are the few things which
you are going to learn.
But let us go where you have an idea.
So you will see here, right?
See dictionary.
Is there complex?
Is there integer bool list set.
But arrays are not provided by Python by default.
So for that what we have to do we have to make sure that we import array.
So there is a import statement in through which you can import an array and then use that.
Now this is again a library in Python through which you can create arrays right.
So let us just create them.
Array equals array dot array.
And now in this particular function what can I provide I can provide two things the type which I want
and what is the initial value to begin with.
So for example let's say I write this I and one 12345.
Okay.
If I just print this now, it's an array.
So let me just remove this help.
Great.
Now you will be able to see print.
When I print array it is saying that hey it's an array of integer type and having the value 12345.
In a very similar sense I can just do array of zero equals ten and I can print it again.
So easy access and manipulation right.
So see now it has been changed to ten.
Everyone right now many of you might be thinking what is this I hear.
So this is the data type just like you have learned about integer float string complex.
All these are the data types which are built in in Python.
Now for array you have to specify this data type in the form of C plus plus code.
Okay.
So again you have to not remember anything.
I will just paste the whole thing here so that you can refer to it.
Just give me a second.
So yeah let us just paste the table through which you can get an idea that what are the default data
types.
Because these things have been made a lot easier for us in Python.
But going forward we have to make sure that we have an idea about these things as well.
So if you will pay attention on your screen, everyone, these are the type codes.
So I use a I which is a sign integer.
And the size minimum size in bytes is two.
Now these are just the data type which are defined in C plus plus.
And because Python is in top of C so Python is able to understand them.
Now you don't have to remember that, but you should have an idea that what exactly arrays are, because
that is the basic thing on which my list is created.
Now going forward, we will be using arrays or lists Anonymously.
The reason for that is both, in a way, are helping me do the same thing.
We will also be implementing the list, this inbuilt list which you are seeing moving forward directly
from R code.
So I have made sure that in this course you have a proper holistic idea that how things are defined
instead of just directly using them.
Because that idea is very important.
We don't have to master the just python, we have to master the overall concept.
So tomorrow, if in any other language also you are coding, you feel very comfortable right now.
Let us just quickly compare, list and erase every one and we will continue on this discussion going
forward.
Just remember one thing that they will be now used interchangeably, because the basic definition,
or the idea of both of them is same, that they are helping me to store continuous element.
Okay, now let us see what are the differences in them.
So these are the array versus list.
The very first thing is the data type.
Now in array as we have seen it is homogeneous, so everything is of the same type and we are defining
the type when we are defining our arrays in list.
Nothing like this is there.
Okay.
Now I will discuss how we can get to this as well.
Okay.
So we discussed two major property or shortcoming.
You can talk about array which is the fixed size and that it is homogeneous.
Just one type is allowed.
We will see when we will implement our own list that how we are able to bypass that particular thing
okay.
And how list are actually made using array in a similar way.
But for the time being you have to just treat them same.
Now the next thing is flexibility.
So it is of fixed size.
List are dynamic.
They can grow and shrink okay depending on how many things you are storing in that performance.
More memory efficient for larger data.
So for large data list arrays give a very good performance whereas list is normally slower okay.
And when you are making very complex programs, very complex applications.
This small speed can also have a role to play.
Okay, great.
Now usage in Python we can use either through array or numpy, or we create them built in.
So that's the basic idea everyone.
That's all about arrays and list.
Going forward you will face many questions in which we will be asking you that, hey, you have to sort
an array or a list.
You will just take it as a list and work on that.
Just the property.
You have to remember that if I am asking you to sort, then it will have all the integers or let's say
all just the strings.
Okay, so this is the basic idea behind array and list.
Now it will become more clear as we will discuss, do some questions moving forward so that we get the
full holistic idea that how list are built on top of arrays, and why arrays are the basic data structure
in every language like C plus plus Java and in Python.
How we are very fortunate.
And plus we can use list for each of our similar use case.
Okay so great everyone.
Now let's meet in the next video.