-->

# Operations and Usage of Numbers - Python

Python provides four different data types to handle the numbers/numeric data.
1. int: positive or negative numbers without decimal point
Ex: -2, -1, 1, 0, 1, 2, etc.

2. long: same as int. The only difference is that it can handle unlimited size.
Ex: 98754632154568754555545545821

3. float: numbers with decimal points
Ex: 5546.2563, 78546.25634, etc.
4. complex numbers: complex numbers are of the form x + yj, where 'x' and 'y' are floats and 'j' represents imaginary number.
j = square root of -1.

### Addition of Numbers in python

`# let's try with integer data typenum1 = 100num2 = 152sum_of_num1_and_num2 = num1 + num2print("num1 + num2 = ", sum_of_num1_and_num2)# Output: num1 + num2 = 252# let's try with float data typenum1 = 100.55num2 = 152.23sum_of_num1_and_num2 = num1 + num2print("num1 + num2 = ", sum_of_num1_and_num2)# Output: num1 + num2 =  252.77999999999997# lets try with float and integernum1 = 152num2 = 189.1121print(num1 + num2)# Output: 341.1121 # python automatically converts the integer to float and then it performs addition on numberscomplex1 = 123 + 50jcomplex2 = 100 + 55jprint(complex1 + complex2)# Output: (223+105j)`

### Subtraction of Numbers in python

`# let's try with integer data typenum1 = 100num2 = 152difference = num1 - num2print("num1 - num2 = ", difference)# Output: num1 - num2 = -52# let's try with float data typenum1 = 100.55num2 = 152.23difference = num1 - num2print("num1 - num2 = ", difference)# Output: num1 - num2 =  # lets try with float and integernum1 = 152num2 = 189.1121print(num1 - num2)# Output: -37.1121complex1 = 123 - 50jcomplex2 = 100 - 55jprint(complex1 - complex2)# Output: (23+5j)`
python automatically converts the integer to float and then it performs addition on numbers

### Division of Numbers in python

`# integer data typenum1 = 10num2 = 3print(num1/num2)# Output: 3# float divisionnum1 = 152.33num2 = 12.55print(num1/num2)# Output: 12.1378486056print(25.5/3)# Output: 8.5print(25/3)# Output: 8print(25/3.0)# Output: 8.33333333333print(25.0/3)# Output: 8.33333333333# Now, let's do complex numbers divisionnum1 = 12+2jnum2 = 10+2jprint(num/num2)# Output: (1.1923076923076923-0.03846153846153849j)print(num1/2)# Output: (6+1j)print(num1 / 2j)# Output: (12+2j)`

### Multiplication of Numbers in python

`# let's start with integersnum1 = 12num2 = 6print(num1 * num2)# Output: 72num1 = 2.5num2 = 4print(num1 * num2)# Output: 10num1 = 2.5 + 45jnum2 = 40 - 2jprint(num1 * num2)# Output: (190+1795j)`

### How to get remainder with modulus "%" operator ?

`num1 = 55num2 = 2remainder = num1 % num2print(remainder)# Output: remainder`

### How to convert string to number ?

`s = "14"# convert string to inti = int(s)print(i)# Output: 14# convert to floatf = float(s)print(f)# Output: 14.0# convert to longln = long(s)print(ln)# Output: 14# convert to complexln = complex(s)print(ln)# Output: (14+0j)s = '3+2j'n = complex(s)print(ln)# Output: (3+2j)# Now, let's try Errorss = "12.5"n = int(s)Output: ValueError: invalid literal for int() with base 10: '12.5'n = int("12test")# Output: ValueError: invalid literal for int() with base 10: '12test'n = float("12+33j")# Output: ValueError: invalid literal for float(): 12+33jn = float("12+3")# Output: ValueError: invalid literal for float(): 12+3n = complex("12+33")# Output: ValueError: complex() arg is a malformed string`

### How to round float to two or three decimal points ?

`num = round(2.6463636263,2)print (num)# Output: 6.65num = round(2.6463636263, 4)print (num)# Output: 2.6464# we can do it with string formats = "%.2f" % 3.14159print(s)# Output: '3.14's = "%.5f" % 3.1print(s)# Output: '3.10000's = "{0:.2f}".format(13.949999999999999)print(s)# Output: '13.95'`

### How to find "Floor" of a number in python ?

`import mathn = math.floor(3.1)print(n)# Output: 3.0n = math.floor(3.9)print(n)# Output: 3.0`

### How to find "Ceil" of a number in python ?

`import mathn = math.ceil(3.1)print(n)# Output: 4.0n = math.ceil(3.9)print(n)# Output: 4.0`