Variable Assignment

Share:
In this topic we will talk about variable assignment that is important to know before we talk about types of variables in python because for taking examples for that we need to assign them to a variable name.

Variable assignment:
Variable assignment is a way to store our values into our computer storage with a proper name, it’s very easy and I am sure you will learn it very fast, for variable assignment in Python we need two main parts first the variable name then equal sign then the value of our variable for example:

                Number = 4

Of course it’s the place to tell you guys that Python is case sensitive that means the variable name Number is not equal to number if you try to call the number, Python will not understand what you mean and it will immediately complain that this name is not defined, so keep that in mind.
And the other thing is this that we can’t use space among a variable name if we do that we will face to error, for that we can use underscore instead of spaces in variable names like:

                short_number = 1

it is recommended to choose a good name to our variables, the reason is this that we can easily know which variable belongs to which part of our code it not only make simple to debug the errors but it also make it easy to other programmers to read your coding so for that try to avoid taking variable names like: A, B, C or A1, A2, A3 etc.…
The most interesting thing in python that I like that is this that we can just leave the value place empty by typing None after equal sign, programmers are using this kind of variable to reassign them later among their code.
Python is really flexible with variables that knows the type of variable by its given value so sometimes if we want to know what type is a variable is we can know that by doing the following:

                Type(variable_name)

Reassignment of variables:
We can reassign the variable after we take that a value very simple and for Python flexibility we can
even reassign a variable to a different variable type like:

                Number = 6

If we want to check what is the value of Number, we can use ‘print’ command to do that like if we print Number we will simply get back 6 and if we want to check its type that is possible with ‘type ()’ command the result will be int because it is a normal number but if we reassign that to another value it we store the new value on it like

                Number = 6
                Number = 0

So now we reassigned the Number to zero so the new value of Number is zero and the type still Int
Now let’s try to change the value to a different value like float:

                Number = 6
                Number = 6.4

OK now the new value of our variable is 6.4 and now if try to see the type of Number using ‘type ()’ command it will show the type float because now it has decimal points.
It was our topic for today I will see you in next topic

No comments