Variables in python with Examples
Description:
in this article, we will talk about what is variables in python? so basically variable is a container where you can put your values example we are storing 2 here in ‘a’ container and the name of that container is X of course you can give anything doesn’t matter you can say xyz, you can say a box you can give any name right so we can say
1 2 3 |
>>>X = 2 >>> |
and the moment I pressed the enter now this is the first time when you pressed the enter and you have not got any output ,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, that works so I do this now can I say
1 |
>>>x + 7 |
yes because in X we have a value which is 2, and when press the enter it will give the out which is
9
we got 9 that works can I store 3 as well let’s try
1 |
>>>Y = 5 |
now we have two variables x and y so we have two boxes and now I will say
1 2 3 |
>>>x+y 7 |
it works so we can say X has 2, y has 5 and we are adding them, so that’s why we can use them but why they’re called variables now normally when you say variable this term simply means you can change the value can be changed here can I say
1 |
>>>x=40 |
can I change the value from 2 to 40 let’s try and then when I pressed the enter?
1 |
>>> |
it works that there was no error but now if I try to do this if I say
1 |
>>>x+y |
will have a value which is two or nine that’s a question because we are changing it but there is no error enter
45
that means the value of x now is 40, so we can change the value and now we can justify that what is variable, it’s a box but it can have a value and we need to define the type of the variable you don’t need to so the moment I assign a value 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 40, what if I use a variable xyz now, of course, we have not defined this variable right and we are still trying to use it for example
1 |
>>>xyz |
press Enter key I got an error
the error is it says name error xyz  is not defined and that’s why we have not defined xyz yet so if you are  using a variable that is not defined so it will of course give you an error now I will do one more thing I will say
1 2 3 |
>>>x+10 50 |
so, of course, it will give you 50 now what if I want to add 50 with y I mean something this is something in this way I would say
1 |
>>>50 + y |
but then if you think about this 50 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 for example
1 |
>>>_ +y |
and if I pressed the enter
you can see a record 55 so it is 40 plus 5 which is 55. so this works right so this is amazing you know there are so many things you can work with this so we can use variables in python for numbers right so we can use it for float all integer.
String variables in python:
what about string variables in python, can I use names as a variable in python with string or the string variable let’s try for example
1 |
>>> name = 'Fawad khan' |
so I put a variable as a name and I use a name as Fawad khan and if I pressed enter it works and if I print name
1 2 3 4 |
>>> name 'Fawad khan' >>> |
We got Fawad khan so it is perfectly working so we can use string as well for example I can say
1 |
>>> name +' owner of Programming digest' |
You can see the Fawad khan owner of programming digest, so it is a name which is Fawad khan plus owner of programming digest. this work that’s perfectly fine.
let me just do an experiment can I use string variables in python without any assignment operator
1 |
>> name ' owner of Programming digest' |
not working so you have to make sure that you put plus( + ) there because if you have plus then put 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 Fawad khan it’s a string, and it’s a combination of characters that means even if I want to fetch one character we should be able to do that, and yes we can do that so if I use name, and if I want to fetch one character how will I do that so let’s say if I have a Fawad khan, and Fawad khan has 9 letters or 9 characters if you break it down it will create an array or you can say a collection of characters the numbering starts with zero because normally in computer’s numbering always starts with zero so if I want to fetch it we have to use a square [ ] and you have to mention zero inside the square bracket
so we are specifying hey I want to fetch the first letter of names, in this case, it is F how it works so now IÂ fetch the last one which is 9, Fawad khan is basically 9 but I am using the one space between Fawad and khan so that why IÂ specifying the 10th index which 9Â which is n
we got n that works. now I fetch let’s say 10 see we don’t have 10 characters why do we only have 10 characters so the first one is 0 the last one is 9 how about 10 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, so you cannot do that.
What will happen when I use negative numbers, let’s try I am using[- 1]
1 |
>>>name[-1] |
so I want your guess here what you think what it will do come on think about it, Â let’s see what happens so when I press enter
i got n, so the answer is when you put a negative number it will start from right to left, so it will start with the ending so which is ‘n’ in this case that means if I put [-2] it will give you the second last data which is ‘a’
and if I put [-10] which will be the first on which is ‘F’
it goes in reverse order. now what if I want to print two characters, so let’s say I want to print ‘F’ and ‘a’ so in this case I will say name, and It will start with 0, of course, we want to print ‘F’ the first character and the second character, that means we will give 2 now this 2 doesn’t mean number of characters it means the ending so I want to start with 0 and 1 so first two characters and you can see
the same thing if I do it for [1:4], so come on guess so 1 means second character right so it will start with ‘a’ and it will end at 3 because you cannot include 4 so it will it is exclusive of 4 so it is 1 which is a, w, and ‘a’
it works we got ‘awa’. now what if I specify only [ 1: ] if I don’t specify the ending what happens in this case it will start with 1 and it will go till the end if you don’t specify the ending part it will go in that.
what if I specify the ending part but not the starting point example if I keep it blank and after colon, I will say 4 Â [:4] now in this case it will start with the first l letter ‘F’ and it will end at 3 which is before 4 which ‘w’.
you got ‘F’, ‘a’, ‘w’, and ‘a’ so what else we can do here what if I specify name and I will give the wrong values I would say
1 |
>>> name[0:14] |
we don’t have 14 characters right now in this case it will end at the last one which is available so it will not give you any other at this case you can see we got Fawad khan, start with F, and then its end at ‘n’ because we don’t have any other character so if you don’t if you want to avoid errors this is one way.
now can I change the letter for example I don’t want it to be Fawad khan I want to be programmingdigest  so I will say name from [0 : 9] I want to change this with programmingdigest can I do that so I’m changing Fawad khan to programmingdigest the moment I say When I press enter, I 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 name[0] I want to change the first character to name[0]=’khan’, even this will not work you can see we got the error
so you did not change the value that means strings in Python is immutable you cannot change the value of it.
but it doesn’t mean you cannot change while printing example if I want to print ‘shahzada’ you can do that for example
1 2 |
>>> 'shahzada '+name[0:] 'shahzada Fawad khan' |
In the example first I print ‘shahzada’ and then i will give a space and then plus(+) and then I write the name variable, so in my case, I say ‘shahzada ‘ +name[0 : ] maybe the ending part doesn’t matter so you can see i got shahzada fawad khan. so this is how you work with the string variable in python, you work with variables, you can have a number of variables in python. you can have float variables in python and you have string variables in python as well.