Python Variable,First of all the question arises is that what is Python Variable and why do we need that??
Variable is like a container which stores the value of data(i.e. it store the numeric,string or any value inside it which you want)
For example,when we have added two numbers in the previous example, suppose we have to store that numbers that we have calculated then we required something which can store that values and that could be a variable
Variable stores the data
So,now let us try variables
Open your IDLE and type x=2 and press enter,
now this time you will not get any output,first time in this series this have happen because we have store the value in variable x and don’t have done anything else
now you can also print this value by just typing the variable as shown in the adjoining figure,
Now if we just simply type any variable and press enter it will show an error as shown in figure
You may be wondering that we are calling x as a variable,now as we have said earlier that python variable is a container which is storing the values,so variable can have any name like a,b,x,y,name,letuslearn, or any such things however there are some restrictions also that are:
- variable name should start with alphabets
- it should not contain any space in between
- if space is requires you can use _ underscore character,other special characters are not allowed
- you may use numbers such as 1,2,3..and so on but name of variable should always start from alphabet’s only
Here we have store a numeric value now let us store string’s.Example,
we have also shown all of this in the screenshot
Now why we have named them variable???
Because in simple variable means to change,here it’s about changing the values.
so let us try to assign the value and then change that value of variable.
you can now see the example in adjoining figure,First we have declared x the value of 4 and then we have changed it’s value to 2
and you can see how the values are changing,
and we have also added two variables and got the answer,you can have your fun and try to add two numbers or sting whatever you want after your fun tell me in the comment section bellow😉😎
In python variable’s we have a facility that is we don’t need to specify it’s type it is automatically adjusting it’s value according to our given data,example x is an integer when we have assigned any number to it and it’s string if we have assigned any string to it…
Variables in Python
A short description of variables can be: Variable in python is a name which is used to refer memory location. Variable also known as identifier and used to hold value.
In Python, we don’t need to specify the type of variable because Python is a type infer language and smart enough to get variable type.
Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.
It is recommended to use lowercase letters for variable name. Abhi and abhi both are two different variables.
Now, if we want to use python as a calculator and there are conditions we have to use previous output for further calculations then python is there to rescue us and have a beautiful option for us to ease our calculation let’s show you in an another screenshot:
We can see in the above figure that underscore _ can be used to have the result from previous calculation and perform further calculations
So till now we have worked with the integers,let see float variable,float means floating variable…means an number with whole and decimal part both,if we perform 2/5 then it will give us a decimal value and a whole number lets perform it and then show to you
In this example you can see that one / has been used for the division and giving us a decimal value where as // is used and have given us only the quotient part whereas if we need only the reminder then we can simply use % (pronounced as modulus).
Have you ever tried for assigning the single value to multiple variables,
let’s try and see the result
we can see that it’s possible now try something more and then ask if you have any error
Let’s us have more fun with the string’s too.On the next page ->
As you can see in the above figure array has two things first is it’s index value and the other is it’s data which you have stored in it.
Data can be any numeric/string/float value whereas index value is fixed to start from 0 and can go to any numbers of times we want,you may wondering that where do we have use of arrays so let me tell you that array is an important aspect and its used frequently in many cases like storing marks of students or price list or making an applications and a lot’s of the places like that
How do we use Array in Python