Computer >> Computer tutorials >  >> Programming >> Python

Python Numeric Types


In Python there are some distinct numeric types. These are integers type numbers, floating point numbers, complex numbers. In the complex numbers, there are two parts the real and imag. The complex numbers are denoted like (a + bj).

There is another function called fraction. The fraction holds rational numbers and decimal holds floating point numbers.

Some functions like int(), float(), complex(), these are used to convert numbers to integers, floats or complex numbers.

Some of the operations and functions of these numeric types are as follows −

Sr.No. Operation/Functions & Description
1

x + y

Sum of x and y

2

x - y

Subtract y from x

3

x * y

Multiply x and y

4

x / y

Divide x by y

5

x // y

Quotient of x after dividing by y

6

x % y

Remainder of x after dividing by y

7

x ** y

X to the power y

8

-x

The negated value of x

9

+x

Unchanged x value

10

abs(x)

Absolute (Magnitude) value of x

11

int(x)

Convert x to integer

12

float(x)

Convert x to floating point data

13

complex(re, im)

Convert from real and imaginary data to complex number

14

x.conjugate()

Find conjugate of a complex x

15

divmod(x,y)

Find Quotient and Remainder as a tuple

16

pow(x,y)

Find x to the power y

Example Code

from fractions import Fraction
x = 100
y = 3.256

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x // y)
print(x % 7) 
print(12 ** 3)

myComplex1 = complex('7+5j')
myComplex2 = complex('26+8j')
res = myComplex1 + myComplex2
print(res)
print(res.conjugate())
print(divmod(x, 3))
print(Fraction(0.125))

Output

103.256
96.744
325.59999999999997
30.712530712530715
30.0
2
1728
(33+13j)
(33-13j)
(33, 1)
1/8