Python Data Type and Variable

Python Data Type and variable

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

python math

We can perform all kind of mathematical operations in python.

Let us try something different, like 1 + x

python data type

Just like you suspected, we can not do that. maybe we forgot to put the quotation marks.

Let us try again

python data type

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)

Python data type

Let us try something different. Now guess what is the result of type(‘2.5’) or type (‘1’)

data type

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.

python
anaconda
we love python

If permeable you can change the data type using ‘str()’, ‘int()’, ‘float()’, etc.

type changing

Python Variables:

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.

variable
variables and types
variable operations

The value of a variable will not change unless you reassign it to a new value.

variable
We apply str() on a integer
Although it showed us the result we wanted but The value did not change.
Then we reassign the variable using ‘=’ again, and now the variable is changed

Input in Python:

Now we will use the input function to let the user insert a value without changing the script.

input
Taking the input
input
Printing the desired result

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.

input function

Leave a Comment

Your email address will not be published.