Python Variables( int , float , bool)

Share:
In this topic we will cover three types of variable inPython that two of them are related to each other because those two variables are both numbers so you will ask what is the difference between them, don’t warry we will learn it with a lot of details and examples in this topic. First of all, we will talk about int and second we will move to float and at the end we will talk about bool.

Int:
What is ‘int’ it is a shortcut for Integer that presents normal numbers for us in python programming language that means it is a number without any decimal point, we can easily define them by taking a normal number (without any decimal point) to a variable for that like:

Number = 13

In above example ‘Number’ is the name of our variable and ’13’ is that value of ‘Number’ variable that is assigned by ‘=’ symbol to ‘Number’.
Of courses it is the place that I mention that any mathematic operation on integers (I used integers because all number in this situation have to be integer) returns a complete number for us like 2+2 we all know that returns 4 which is a complete number. Now that we know enough about integer we can move to float.

Float:
float is shortcut of floating point that presents numbers with decimal points and we all know in math decimal points can be very important sometimes like ‘2.9’ if we want to round it off the result is 3 but if we want to change ‘2.9’ as an integer the result will be 2 because integer don’t know what is ‘.9’ really is. we can define a float by taking the value of a variable as a number with decimal point like:

Number = 2.9

And as you thought any mathematic operation on floats returns a number with decimal points Like 1,5+1,6=3,1.
Now the question will come into your mind what if we perform mathematic operations between float and int like 2,5+5 what will be the result, the result will return a float back to us try it now and see what is the result.

Bool:
bool is taken from Boolean that have tow values True or False it cannot take both values in same time it only holds one value of course we can assign it to True or False later in our code bool is widely used in condition because condition themselves are Booleans like if we make a condition like (2<1) it is a bool that is False it cannot be True and it cannot be a little True or a little False it is absolutely False. We can define a bool by this way but before that remember which True and False are starting with capital letters if you write it with small letters you will face to an error.

milk_is_white = True

if we want to check what is the variable type of ‘milk_is_white’ we can easily write : type(milk_is_white) we can do this to all of our variables to check their variable type.

It is the end of this topic all in all int presents numbers without decimal points (complete numbers) , float presents numbers with decimal point and bool presents True or False for us in python I wish you found this topic useful thanks for reading.

No comments