After some basic of learning python and practice, today we will learn python data type, variable and input.
Python Data Types:
Let us continue mathematical operations from last tutorial
We can perform all kind of mathematical operations in python.
Let us try something different, like 1 + x
Just like you suspected, we can not do that. maybe we forgot to put the quotation marks.
Let us try again
Now it is saying the operation isn’t possible because you can not literally add a number to a letter.
It also introduces ‘TypeError’. Yes in python it is called data type.
Let us see what type of data we faced.
To check the data type we will use the ‘type’ function.
We can see we faced int(integer), float and str(string)
Let us try something different. Now guess what is the result of type(‘2.5’) or type (‘1’)
So we can see anything we store in between ‘–‘ or “–” will become string and we can perform mathematical operations if the objects are integer or float. There are more types of objects in python like, boolean, tuple, dictionary, list, etc. Also remember we can perform multiplication between a integer and a string. And we can use ‘+’ to add strings.
If permeable you can change the data type using ‘str()’, ‘int()’, ‘float()’, etc.
Like in the mathematics, we can store any object in a variable and later we can call the variable to use. We can use those variables in all kinds of operations just like the original value.
The value of a variable will not change unless you reassign it to a new value.
Input in Python:
Now we will use the input function to let the user insert a value without changing the script.
Remember, input function takes string. Even if we put integer the input function will convert it to string. That is why if we want integer or float value we need to convert it to desired data type.
We also need to let the user know what they are imputing the value for. We will pass a string argument to do that.