CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python – Numeric Types – float and complex Classes

In our previous article, we discussed int Class which deals with Integer numbers. In this article, I will explain float and complex classes. And these classes deal with floating point numbers and complex numbers respectively.

float Class – Deals with Floating point numbers

We can create floating point numbers using float() constructor. We can also use the floating point numbers directly and Python treat them as an object of float Class.

float() constructor takes a single optional argument to create an object with a floating point number as its value. The argument can be a number or a string. If the NO value is passed to the argument; it treats as the value “0.0”. Below are examples to create float objects.

>>> 2.3
2.2999999999999998
>>> type(2.3)
<type 'float'>
>>> float(5.7)
5.7000000000000002
>>> float("9.3")
9.3000000000000007
>>>

complex Class – Deals with Complex numbers

The objects of the complex class can be created using complex() Class constructor or directly by giving the complex value. Complex numbers have real and imaginary parts. The value (2+3j) is a complex number; where 2 is the real part and 3 is the imaginary part.

>>> 2+3j
(2+3j)
>>> (2 + 3j)
(2+3j)

The Syntax of the constructor is:

complex(x, y)

Where x is the real part and y is the imaginary part. Both these arguments are optional. If you do not pass any arguments Python creates an object with the complex value 0j.

>>> complex()
0j
>>> complex(2)
(2+0j)
>>> complex(2, 3)
(2+3j)

The first argument will be a number or a string. If you pass a string in the first argument; it doesn’t allow you to take the second argument.

>>> complex("2")
(2+0j)

If you try to pass a second argument, passing a string through the first argument; Python will throw the Error.

>>> complex("2", 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: complex() can't take second arg if first is a string

You can give a complete complex number in a string form through its first argument. When you pass a complex number as a string, spaces are not allowed between the number and the “+” sign. That means, you can pass it as “2+3j”; NOT “2 + 3j”. If you pass an invalid string, Python will throw the Error.

>>> complex("2+3j")
(2+3j)
>>> complex("2 + 3j")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: complex() arg is a malformed string

As complex numbers have real and imaginary parts, sometimes it is required to access these parts separately from the complex number. the complex class provides real and imag properties to get real and imaginary parts of the complex number respectively.

>>> c = complex(2,3)
>>> c
(2+3j)
>>> c.real
2.0
>>> c.imag
3.0
>>>

We will discuss more topics as we go.

/Shijit/

Python – Numeric Types – float and complex Classes

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top