## Numbers in Python

##### In this part of Learning Python we Cover how to use Python Numbers like Int, float, Complex etc.
Written by |10-Apr-2019 | 0 Comments | 459 Views

Integer in Python:

An integer is a number that is written without a fractional component. It contains whole number and their additive inverse. The set of integers can be represented as:

`{-n, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, n}`

Like string there is no special keyword to assign an integer as a variable in a python. Python determine the type of data at run time. If there is no point after an integer python consider it as integer. We can apply all the mathematical operation on integers.

Float in Python:

A float is a number that is written with fractional component. It contains a range of number with in two integers. A floating number can have any number of decimals after the point. The set of floating-point number can be represented as:

`{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0}`

Unlike C, C++, or Java there is no special keyword for assigning the double or float number. Python decides on a run time. And it is decided by the presence of point within two numbers.

Mathematical Operations:

Mathematical operation on integers and float can be performed as following:

 Addition of two Numbers Print Values `print(3+5)` `print(3.0+5.5)` `8` `8.5` Multiplication of Two Numbers `print(100*3)` `print(100.0*3.5)` `300` `350.0` Division of two numbers `print(28/9)` `print(28.0/9.0)` `3.111111111111111` `3.111111111111111` Subtraction of two numbers `print(28-9)` `print(28.0-9.0)` `19` `19.0`

Type Casting:

Type casting is a technique in which a data type is converted from one type to another. For example, if there is a string of digits and we want to apply mathematical operation on it we can't do so without type casting. If we want to convert integer into float than we must apply type casting. Python provides built-in functionality to type cast it. Following example will show how to convert data type of variables:

 `num_string1 = '1789'` `num_string2 = '6543'`   `result = num_string1+num_string2` `print(result)` `print(type(result))`

If we run the above-mentioned program in python interpreter, it will not generate any type of error. The above mentioned program will concatenate the two string and will print “17896543” as a output and when we print the second print statement that is type of result it will give  " class of 'str' "  as an output.

So now what we have to do is type casting if we change the code a little bit we can achieve our goal which is as following:

 `num_string1 = '1789'` `num_string2 = '6543'`   `result = int(num_string1)+int(num_string2)` `print(result)` `print(type(result))`

Now the output of this program will be 8332 and the type of result will be "class of 'int' " .

Similarly, we can change the type of other data type as following.

 `Num = 1` `float(Num) #convert 1 into floating number` `str(Num) #convert 1 into string.`

Can we apply mathematical operation with float and integer?

Most languages don't allow to add, subtract, multiply and divide the floating number with integer. But there is no such restriction in python. If one number is float and other is an integer than python convert the integer to float implicitly and make the operation on it.

Modulous Operator:

There is other operator other than basic mathematical operator which is known as modulous operator. This operator returns the reminder of two numbers. It is a short way of checking if two numbers are completely divisible with each other or not.

 `print(5%2)` `Output: 1`   `print(9%7)` `Output: 2`

The output will be zero if two numbers are completely divisible by each other.

Boolean Operators:

Boolean operators are those operators which returns either True or False. In C or C++ the program return 1 for true statement and 0 for false statement. But in case of python the program returns True or False. Following are the some boolean operators to check weather it is true or not.

Equal To “=”:

The sign equal to is use to assign a variable or assigning a value to already existing variable. As we have studied earlier.

`>> num =1234`
`>> string = 'Hassan'`

Double Equals to “==”:

Double equals to is use to check if bot the statement are equal to each other or not. It will returns True if they are equal otherwise False.

`>> 'Hassan' == 'Hassan'`
`>> Returns: True`
`>> 12 == 14`
`>> Returns: False`

Not Equal To “!=”:

Not equals to symbol check if the two variables or the values are not equal. If they are equal than it returns False else return True.

`>> 'Pokemon' != 'Digimon'`
`>> Returns: True`
`>> 12 != 12`
`>> Returns: False`

Less Than and Greater Than <>:

Less than and greater than sign check if the value is less than the other or greater than the other.

`>> 9 > 3`
`>> Returns: True`
`>> 9 < 3`
`>> Returns: False`

Greater than and equal to AND Less than and equal to “<= , >=”:

Its is same as mentioned above plus they have additional property of double equals to

`>> 9 >= 9`
`>> True`

Logical Operators:

Logical operators includes the logic gates you have studied in science subject. Python include all sort of logic gates but for now we only discuss two important logic gates so that we dont make it more complicated. The two logic operators are AND and OR.

Before diving into code we must know what are the possible output of these gates so we make truth table of these operators

 Value 1 Value 2 AND OR 0 1 False True 0 0 False False 1 1 True True 1 0 False False

By the truth table we can conclude thats AND gate is only true when both the values are true and OR gate is true when if even one value is true.

So in python we don these operations by giving the keyword “and” or “or”.

Following is the example of these operators.

`>> 12==12 and 13==13`
`>> Output: True`
`>> 12==12 and 13==31`
`>> Output: False`
`>> 12==12 or 13==13`
`>> Output: True`
`>> 12==12 or 13==31`
`>> Output: True`
`>> 'pikachu'=='blazikane' or 'balbasour'=='venasour'`
`>> Output: False`

This is the detail introduction of numbers and operations that can be performed in python.