declare

#4 Python Tutorial for Beginners | Variables in Python



Sharing buttons:

[Music]

welcome back aliens my name is Devin 20

and let's continue with this series on

Python in fact in the earlier videos we

have talked about what is Python we have

we have installed two software's and we

have also worked with certain operations

right we had fun in the last session now

in this video where I talk about what is

variable but first of all why do we need

one let's try something here so what I

will do is I will go back to my previous

example you know we were adding two and

three so we have done with that right we

are saying two plus three now if you

remember in Matz what we do is we of

course we work with numbers but then

when it the moment you start with

algebra you have this concept of using x

and y so what we do is we say let X is

equal to five can you do that here so we

have X equal to 2 so I'm storing this

value 2 in a variable called as X and

that's a new word now what is variable

so basically variable is a container

where you can put your values example we

are storing to here in a container and

the name of that container is X of

course you can give anything doesn't

matter you can say ABC you can say a box

you can give any name right so we can

say X equal to and the moment I say

enter now this is the first time when

you say enter and you have not got any

output right so in the earlier the

prompts what we did is when we said any

of any operation it was giving you some

output here we have not got any output

it's just an assignment operation so in

assignment what we do is we have a

variable name equal to our value so we

are assigning to to X ok that works so

can i you can I do this now can I say X

plus 3 yes right because in X we have a

value which is 2 oh it works you can see

we got 5 that works can I store 3 as

well let's try so I will say Y is equal

to 3 now we have two variables x and y

so we have two boxes and now I will say

X plus y oh it works right it works so

we can say X has two y as 3 and we are

adding them ok so that's why we can we

can use them but why they're police

variables now normally when you say

variable this term simply means you can

change the value can be changed here can

I say

equal to nine can I change the value

from two to nine let's try and then when

I say enter oh it works that there was

no error but now if I try to do this if

I say X plus y X will have a value which

is two or nine that's a question because

we are changing it but there is no error

enter oh you got 12 that means the value

of x now is 9 okay so we can change the

value and now we can justify that what

has variables right so what is variable

it's a box but it can have a value now

do we need to define the type of the

variable you don't need to so the moment

I assign a value it you can it

automatically takes whatever value you

are assigning so in this case it is

integer now that let's do this thing

what if I say X and if I say enter what

it will do so the moment you say X and

enter it will print the value of x in

this case which is 9 right what if I use

a variable ABC now of course we have not

defined this variable right and we are

still trying to use it now we can say

Enter key got an error the other is it

says name error ABC name ABC is not

defined and that's why we have not

defined ABC yet so if you argue if

you're using a variable which is not

defined so it will of course give you an

error now I will do one more thing I

will say X plus 10 so of course it will

give you 19 now what if I want to add 19

with y I mean something this is

something in this way I would say 19

plus y but then if you think about this

19 this is the output of the previous

operation right now if you want to use

the output of the previous operation we

can use underscore so underscore

represents the output of the previous

operation and if I say enter you can see

record 22 so it is 19 plus 3 which is 22

so they won this works right so this is

amazing you know there are so many

things you can you can work with this so

we can use variables for for numbers

right so we can use it for float all

integer what about string can I use

names with can I use variables with

string or the string variable let's try

so I will put a variable as name and I

will use a name as YouTube and if I say

interro it works and if I print name we

got YouTube so it is perfectly working

so we can use string as well so example

I can say nay

plus you two blocks so you can you can

see the car due to block so it is name

which is YouTube plus rocks okay this p1

this work that's perfectly fine

in fact let me just do an experiment can

I say name and space dogs okay this is

not working so you have to make sure

that you put plus there because if you

have plus then put then then only it

will try to interpret that but there's

this one thing which is very fancy here

you know so we talk about YouTube it's a

string right and it's a combination of

characters that means even if I want to

fetch one character it should be paper

we should be able to do that right and

yes we can do that so if I say name and

if I want to fetch one character how

will I do that so let's say if I have a

YouTube here and YouTube has seven

letters or seven characters if you when

you break it down it will create an

array or you can say collection of

characters the numbering starts with

zero because normally in computer's

numbering always starts with zero so

here if you want to fetch it we have to

use a square bracket and you have to

mention 0 so we are specifying hey I

want to fetch the first letter of names

in this case it is y how it works so now

I will fetch the last one which is 6

which is e in this case and we got e

that works now I will fetch let's say 8

see we don't have eight we don't have

nine characters why do we only have

seven characters so the first one is 0

the last one is 6 how about 8 the moment

you do that it will give you an error

it says index error string index out of

range of course you are going out of

your limit right so you cannot do that

ok can I use negative numbers let's try

I will use minus 1 so I want to I want

your guess here what you think it what

it will do come on think about it

ok so let me just ain't any time let's

see what happens so when I say enter you

got e ok so the answer is when you say

minus number it will start from right to

left

ok so it will it will start with the

ending so we just a in this case that

means if I say minus 2 it will give you

the second last data which is B that's

right and if I say minus 7 which will be

the first one right which is why oh it

was it goes in reverse order now now

what if I want to print to gag does not

want to

so let's say I want to print Y and O so

in this case I will say name and I will

start with 0 of course we want to print

0 the first character and the second guy

to write that means we will give 2 now

this to doesn't means number of

characters it means the ending so I want

to start with 0 and 1 so first two

characters and you can see regard to y 0

the same thing if I do it for 1 colon

full so come on guess so 1 means second

character right so it will start with O

and it will end at 3 because you cannot

include 4 so it will it is exclusive of

fold so it is 1 which is o U and T oh it

works we got out yes yes it works

now what if I specify only one and colon

if I don't specify the ending what

happens in this case it will start with

1 and it will come it will go till the

end okay that has well so if you don't

specify the ending part it will go in

that way what if I specify the ending

part but not the starting point example

if I say if I if I keep it blank and

after : I will say for now in this case

it will start with the first later and

it will end at 3 which is before 4 and

that's why you got Y or u T so this fun

right

ok what else we can do here what if I

specify name and I will give the wrong

values I would say 3 : potatoes of

course we don't have 10 characters right

now in this case it will end at the last

one which is available so it will not

give you any other in this at this case

you can see we got tup e to start with T

and then its end at e because we don't

have any other character so if you don't

if you want to avoid errors this is what

is one way okay now can I change the

later example I don't want it to be

YouTube I want to I want it to be my

tube so I will say name from 0 to 3 I

want to change this with my can I do

that so I'm changing y-o-u to my the

moment I say enter you got an error

it says string object does not support

item assignment so the thing is once you

assign the value you cannot change it

not you want for one character if I say

0 I want to change the first character

to brought you even this will not work

you can see we got the error so you

not change the value that means strings

in Python is immutable you cannot change

the value of it that's awesome

right but then that doesn't means you

cannot you cannot change while printing

example if I if I want to plant my job

you can do that so first I will print my

and then I will give up I will give a

space and then plus I want to print cube

right so in that case I will say 3 :

6 4 maybe the ending part doesn't matter

so you can see we got my tube if you

don't want space you can do that you can

say my and then without space I can type

it too so this works yeah so this is how

you work with the string you work with

variables right you can have a number of

variables you can have a float variables

and you can have a string variable as

well in fact which string we have one

more thing let's say if you define a

string which is let's say my name is a

variable and my name here is Naveen

ready and if I want to know the length

of my name we can do that so we have a

Len function and we can pass my name as

I as I mentioned there are some inbuilt

functions available in Python and one of

them is left Len so you can see we got

11 characters so yeah that's it that's

it from this video we have talked about

different variables assignments and

stuff so I hope you are enjoying my

session let me know in the comments

section and they don't even like button

if you're enjoying it thanks for

watching everyone

you