Python Numbers – int, float, and complex

  • Numbers are used to store numerical values in the program.
  • Python support three types of numbers – int, float, and complex.
  • Python 2 also supports “long” but it’s deprecated in Python 3.
  • In Python, numbers are also an object. Their data types are – int, float, and complex.
  • There are built-in functions to create numbers – int(), float(), and complex().
  • We can also create a number by directly assigning the value to a variable.
  • The complex numbers are mostly used in geometry, calculus and scientific calculations.
  • We can define the numeric representation of an object by implementing __int__(), __float__(), and __complex__() methods.

How to Create a Number Variable in Python?

x = 10
y = 10.55
z = 1 + 2j

The complex number has two parts – real and imaginary. The imaginary part is denoted with “j” suffix.


How to find the type of a Number?

We can find the type of number using the type() function.

print(type(x))
print(type(y))
print(type(z))

Output:

Python Numbers
Python Numbers

1. Integer

Integers are whole numbers. They can be positive or negative. They must be without decimal values.

We can use int() function to get the integer representation of an object. The object must implement __int__() method that returns an integer.

Let’s look into some examples of creating integers in Python.

x = 10
print(type(x))

x = int("10")
print(type(x))


class Data:
    id = 0

    def __init__(self, i):
        self.id = i

    def __int__(self):
        return self.id


d = Data(10)

x = int(d)
print(x)
print(type(x))

Output:

<class 'int'>
<class 'int'>
10
<class 'int'>

String class provides __int__() method, that’s why we can easily convert a string to int using the int() method.

If the object doesn’t implement __int__() method, the int() function throws TypeError.

Python Int Typeerror
Python Int TypeError

Generally, integers are defined on base 10. But, we can also define them in binary, octal, and hexadecimal format.

i = 0b1010
print(i)  # 10

i = 0xFF
print(i)  # 255

i = 0o153
print(i)  # 107

2. Float

A floating point number contain decimal points. It can be positive or negative.

We can use float() function to get the float representation of an object. The object must implement __float__() method that returns a floating point number.

x = 10.50
print(x)
print(type(x))

x = float("10.50")
print(x)
print(type(x))


class Data:
    id = 0.0

    def __init__(self, i):
        self.id = i

    def __float__(self):
        return float(self.id)


d = Data(10.50)

x = float(d)
print(x)
print(type(x))

d = Data(10)
x = float(d)
print(x)
print(type(x))

Output:

10.5
<class 'float'>
10.5
<class 'float'>
10.5
<class 'float'>
10.0
<class 'float'>

String provides __float__() method implementation. That’s why we can easily convert a string to float.

If the object doesn’t implement__float__() method, we get the error message as:

TypeError: float() argument must be a string or a number, not 'Data'

If the object __float__() method doesn’t return a floating point number, we get the error message as:

TypeError: Data.__float__ returned non-float (type int)

We can also define a float in scientific notation using “e” or “E”. Here the number after “E” specifies the power to 10.

x = 10.5e2
print(x)

x = 10.5E2
print(x)

Output:

1050.0
1050.0

Explanation: 10.5E2 = 10.5 * pow(10, 2) = 10.5 * 100 = 1050.0


3. Complex

A complex number contains two part – real and imaginary. The imaginary part is written with the “j” suffix.

We can also use the complex() function to create a complex number. We can pass two ints or float arguments to the complex() function. The first argument is the real part and the second argument is the complex part.

x = 1 + 2j
print(x)
print(type(x))

x = -1 - 4j
print(x)
print(type(x))

x = complex(1, 2)
print(x)
print(type(x))

x = complex(1)
print(x)
print(type(x))

x = complex(-1, -2.5)
print(x)
print(type(x))

Output:

(1+2j)
<class 'complex'>
(-1-4j)
<class 'complex'>
(1+2j)
<class 'complex'>
(1+0j)
<class 'complex'>
(-1-2.5j)
<class 'complex'>

We can also get an object complex number representation by defining __complex__() method. This method must return a complex number.

class Data:

    def __init__(self, r, i):
        self.real = r
        self.imaginary = i

    def __complex__(self):
        return complex(self.real, self.imaginary)


d = Data(10, 20)
c = complex(d)
print(c)
print(type(c))

Output:

Python Complex Number
Python Complex Number

We can also convert a string to a complex number. There should not be any white space between the real and the imaginary part.

c = complex("1+2j")  # works fine

c = complex("1 + 2j") # ValueError: complex() arg is a malformed string

We can get the real part of the complex number using the “real” property. We can get the imaginary part of the complex number using the “imag” property.

c = 10 + 20j
print(c.real)  # real part
print(c.imag)  # imaginary part

Some other complex number methods are:

  • conjugate(): returns the complex conjugate number. The sign of the imaginary part is reversed.
  • abs(): returns the magnitude of the complex number.
c = 1 + 2j

print(c.conjugate())  # (1-2j)
print(abs(c))  # 2.23606797749979

Python Numbers Type Conversion

We can convert an int to float using the float() function. Similarly, we can use int() function to convert a float to int.

We can use complex() function to convert an int or float to the complex number, the imaginary part will be 0j.

We can’t convert a complex number to int or float.

i = 10
f = 10.55

# int to float conversion
f1 = float(i)
print(f1)
print(type(f1))

# float to int conversion
i1 = int(f)
print(i1)
print(type(i1))

# int and float to complex number conversion
c = complex(i)
print(c)
print(type(c))

c = complex(f)
print(c)
print(type(c))

Output:

10.0
<class 'float'>
10
<class 'int'>
(10+0j)
<class 'complex'>
(10.55+0j)
<class 'complex'>

Conclusion

Numbers are an integral part of any programming language. Python support three types of numbers – int, float, and complex. Numbers in python are also objects of type – int, float, and complex. We can convert an object to number using the int(), float(), and complex() functions. The complex number is mostly used in the geometry and scientific calculations.

References: