#26 Python Tutorial for Beginners | Array in Python

Sharing buttons:


welcome back aliens my name is Ivan 20

and let's get into this series on Python

in this video we're going to talk about

arrays now we have worked with least we

have work with tuples and now we know

how all those things works so if you

want to have a list of values with you

of course you can go with least or if

you want to make it constant you can go

for tuple

now while added them now in anak we need

to have all the values of same time so

we can have the same list with integer

float and string in one list but that's

not the case with array so a days are

similar to list but with one difference

that you need to have all the values of

same time so if you say it's an int

array should be of int array if you say

it is floated it all the value should be

float there's one advantage here and is

in Python they don't have specific size

they don't have a fixed size which means

you can expand you can shrink it example

let's say you have five values and if

you think hey we want 10 the end user

can expand it and if you feel hey we

have 10 values but we need only four

values now we can shrink it so it is

quite flexible to work with and it

offers you certain methods to work with

example if you want to add an image you

can use append if you want to find a

particular element you can use other

methods in fact we have a vertical as

index using which you can get the index

number of a particular value Oh hold on

we'll do it we'll do that in the

practical video here now basically when

you will be using array so think about

this let's say if you have a list of

students right and then in that so every

should you will have a marks so let's

say we have one subject which is let's

say Python itself so it's a language and

you have a subject in your college and

of course a view will be having a box so

if you have ten students in a class you

you need to create ten different

variables like marks one marks two

mouths three or let's say if you have

hundred students need to create 100

variables now instead of doing that we

can create an array right now how do we

do that

so in order to use array we will be

using array but then the problem is we

will not be getting this array by

default right okay so we need to import

a module called as a date to use this so

how would you do that you will say


add it right so this is up to use I have

to say import

and in this module array we have a

function called as Adi so we have to say

Eddie dot added this is how you create

an ID I know it's a lot of edits there

so if you don't want to use this edit

every time you can also use array so you

can say import Eddie as a so instead of

using and every time you can say a dot

added this one way so again you can use

edit or add it by inputting array or you

can say input array as arr which is

you're using Eliza and then you can say

a RR dot Eddie now if you if you just

want to avoid everything you can simply

use we have done that before right so if

you want to work with modules if you

want to import modules you can say from

add a input now you can specify the

functions you want to work with or you

can simply specify stars so star means

you want to work with all the functions

right now once you once you have this

function array now let's see how do you

get an adding so I want to create a day

off let's label not go for Mark's here

let's go for girls

now valence is basically values okay so

it's it's short form for values now I

want to create an array with with the

name of Val's so you have to say adding

now we have to specify two things the

first thing that you specify is the type

and the second time second you have to

mention is the values

now why time because in adding all the

values should be of same type right so

if you say hey we have int added you

have to mention that it's an Inc added

if it is a float adding up to mention

that how do you do that okay if you have

to you some keywords right example we

can say ain't float but that that is not

how it works here so you have to use a

type code now what it means every time

will have a unique code okay example for

integer in fact anything integers as

well we have different types of integers

we have bite we have long and we have

eight when you work with different

language like C C++ Java we have these

types there's right we have bite

we have long we have int same way in


in Python we also have int now depends

upon you know the moment you change the

type it will change the size in the

memory example if you want to say hey I

want a smaller integer you can say bite

because by a small integer it takes only

one byte there are the one is normal

which which we'll take two bites and

then we have long integer which will

take four bites now what if you want to

have float values for that we it will

take any four bytes so it is f float

which is which will take four bytes in

fact double as well so we have double

float which is which will give you more

memory of eight bytes

what if you want to go for character for

that you have to use U which is Unicode

right and it takes two bytes okay

because you need Unicode need two bytes

to store data okay that works now the

question is what if you want to have

unsigned integer nobody is an unsigned I

signed so let's say if you if you're

going for non integer it goes from a

negative range to a positive positive

range right what did you don't want to

store a negative value in that case you

will be going for unsigned integer

because it starts with 0 and ends at a

particular value so we will not be

considering negative numbers here right

and you can see the table we have all

the time so if you want to work with

unsigned integer you have to use capital

I if you want to use sign integer which

is long one you can use small L right so

you can you can defer this table and

we'll be using that here so I want to

create a day of integers and the way you

can do that is by saying aye

because you are using an integer array

and you can give up comma and you have

to mention a square bracket because

that's how you represent arrays now in

this square bracket you can mention

values I will go for simple values here

just to you know make it simple so I

will say five nine eight four and two

simple values we got five values here

and once you've got this add you can

print it and how do we print it it's

very easy you will say print and you

will print the values here will say

Val's now for this code you can see

record an array it's so simple right we

can't you can see this printing array of

type int and then you got the values

okay now let me just do a twist here

what if if I put a different value

example here instead of having eight let

me say eight point five now you can see

this is where it all starts and if I

love this code you can see record an

error Heather says integer argument

expected got float can you see that here

so this is the taller we have right when

you say it is integers and if you are

trying to put float that will not work

so of course you have to go for integers

can I have negative values of course we

can have because small I means

signed integer which stands to negative

value to positive values

we'll work let me just run this code so

it works you can have negative values

but what if I say capital I which means

you cannot have negative values now and

the moment is on this code you can see

we got an error it says cannot convert

negative value to unsigned int right to

this important remember so when you tell

an array you have to make sure that you

specify a proper type code okay now once

we have done with that in a day as we

are working with a DS now we can use

certain functions example if I say

values dot so enough market function is

buffer info now before info will give

you this size of the adding right so if

I say okay we were supposed to change

this I small I saw that with me under

this code and you can see we call it

something now it is did something so

this is a tuple here with two two things

the first one is the address so this is

the address of your array and the second

parameter there is actually your size

which is fine right if I change the

number of elements here if I remove two

of course you will be getting four and

is now 74 right so that's how you can

work with before info in fact we have

some other methods as well I would say

value start in fact we also have type

code which will print the type of the

code you're working with which is int in

this case so you can say this I which is

integer this was the property here now

what if you want to add a value you can

use append what if you want to remove a

value you can use remove you if you want

to reverse the IDL added you can do that

here example let's try so if I said

reverse and if I print the values and if

I save 10 because reverse doesn't

written value so if you want to work

with reverse you have to save Val's dot

reverse and you can see first you got

the adding it will reverse the area it

will give you the value so if I say

shift f10

you got the value which is array and

then two phone so you can see all the

values got reversed but the point is

what if I don't want to print all the

values as it is I want to print the

values one by one is it possible of

course why this is added and when you

say a date is similar to list so we can

use index numbers right index number

start with 0 right so we can use index

number to print one value so I would say

values off and I want to say 0 now when

I say values of 0 it will print the

first value right in this case it is -

okay why - because we are the

buzzing it right because after he was

saying to will come first let's remove

that and because I don't want to reverse

now let's let's say run and you can see

we got five if you change this value to

let's say let's say one it will print

nine but I want to print all the values

not just one or two values right in this

case we will be using a loop your

favorite loop you can work with while

loop you can work with for loop your

wish okay here I will go for look for no

because for loop makes sense when you

know this range so it's saying for I in

when in in fact I'll be using range I

will start with the range which go till

four or five because we have five values

right so range will start with zero it

will end that four and every time this

loop runs I have to say I now there are

two things which are missing in this

loop the first one is the colon right

and the second thing is this print

belongs to a for loop so you have to

give a tab a proper indentation right

and now let's run this code and you got

the value which is five nine minus eight

four and two so it's so simple so this

is how you do print all these values

here so this is fun white so you can

played all the values one by one it's

like we have one more way so if you

don't so what is happening here is the

value of I is changing from 0 to 4

every time you long this school it will

say values of 0 values of one values of

two but what if you don't know this

length of it because here we know it's

five you can see that but right what if

you don't know the length or if you want

to make it more dynamic in that case you

can say length of Val's so length of

valves will give you the length and you

can pass that length in range right so

indirectly you know at one time it will

be range five right because size is five

and it works now this this is one

another way of printing all what you can

do is instead of saying range you can

directly save Val's

so I will say for e even I works but

then I would say for e in Val and I can

drag the print the value of e so fall is

very dynamic you know so sometime you

can use for with range which will go for

index values or here it will die agree

go for the values so in Val's we have

five nine minus eight four nine two so

it will fetch the value one by one so II


left four to five then evil therefore to

nine then you will refer to -8 so your

choice the way you want to work with it

74 on this code it still works okay so

this this thing is working now can I

walk with characters because we have

seen how to work with integers we can

also use float here can I work with

characters of course you can so what if

you do is instead of saying I you have

to say you which is Unicode you can say

characters and here instead of having

numbers we should be using characters I

would say single quote a single code E

and then single code

I so we have AE i and then i find on

this code you can see we got the same

values so yes you can work with unique

codes as well

look at this thing works no what do I do

is I will just undo it and I will go

back to et just because I want to do

some one small operation okay so what I

want is I want to create a new adding ok

with the same values example I will just

I will just take positive values now 5 9

8 4 2 I want to create a new array with

the same values is it possible of course

you can do it right you can say a new

array here I will say new array and

equal to you can see the same thing you

can say array and then I then comma you

can put the same values but they should

doing that what you can do is you can

say a D

bracket and in this bracket you can

mention hey I don't know the type C when

you know the type is I you can do it

here when you know the values you can

type it here but sometimes you want to

copy it right maybe you're getting this

values from somewhere else in that case

you cannot simply type the values right

so in that case you can say I don't know

the type just take the type from Val's

and shop saying oh you're simply saying

type code from Val and then the values

now I donate the values so you can see

now we have a different syntax here try

to understand this so what we are saying

is hey I don't know the values but take

the values from Val's so they devalues a

now able that wasn't one that anytime a

for a in Wells so what will happen now

is it will so so this for loop will take

one one value from Val's and it will

assign to the array so it will say hey I

got five assigned I got nine assigned so

that one day which is getting up is

coming in to a and then

just getting assigned to the new array

just to prove my point I will ensure

printing the Bastile print a new added

here let's run this boot and you can see

record the values so the all the array

values are coming into new adding so

syntax is first of all good to mention

hey I don't need the type of it get the

type from the old array which is Val's

and then the second one where we are

mentioning here is the loop so we are

saying hey I don't know the values what

you can do is take one one value from

Val's and you can do that with the I'll

go for look right so you're saying for a

in Val's and then it will fetch one more

value we are assigning it to be added it


in fact you can do one more thing you

should of assign the same value what if

you want to assign the square of it

example I want to assign 2581 and so on

I want to assign the square of a number

in that case you can simply say a into a

so you're simply creating a square of a


let's run this code and you can see

record values we got 25 81 6469 4 so you

are getting this square of a number

so it's so simple to do all the stuff in

Python you know if you do want to do the

same thing in other languages you have

to write multiple lines of code and

that's why we say you know in Python you

can do multiple things in less number of

lines now what if you want to print this

values using while loop again we have

done with for it so we know how it works

can use while loop here of course we can

you know a lot of people have this

confusion with fall and while you can

use while loop here as well no but the

thing is for while loop you have to use

three steps right you have to use

initialization you have to check for the

condition and you have to do increment

agreement by yourself so let's do that

so we say I is equal to zero if you have

to check for the conditions I will say

while I less than length of newarray

because I don't know the length of

newarray : and then here you will say

print the value print new array and then

we have to use index values so we have

to say new array I and once you have

printer you have to do increment or

decrement because here we have to use

increments I will say I plus equal to

one so the thing is you can do it but

the only thing is you have to do it

manually and you can see it works and I

know in this case for loop makes more

sense right because you don't have to

initialize you don't have to increment

decrement and you don't even have to

check whether

conditions right what you can use while

loop so don't about the bow to Eddie's

in fact we have so many things to work

with Alice now so in this video we have

talked about the basics of adding how

can you create an array and how can you

print the values how can you create a

different area from from the existing

addy so that's it from this video I hope

you enjoyed this series let me know in

the comment section do click the like

button if you enjoy any times for

watching everyone bye bye