# cmath Python Module

Python have five built-in data types one of which is Numbers, now as numbers can be integers, floats or complex. Doing mathematics on these require different functions because you cannot mathematically treat intergers/float in the same way as Complex Numbers because these have an imaginary part while integers/floats don’t.

Owing to this reason, Python’s core developers have put together two modules containing Mathematical Functions.
cmath module for Complex Numbers
math module for all other types of numbers

How to import cmath in Python? You can import python’s cmath module into code by using import cmath statement and to access functions inside it do cmath.function_name(parameter).

If your unaware of Python’s Math Module then you can see – Math Module in Python Programming Language article which I put up on this website some days back.
Below are some tables explaining what are functions there inside cMath Module? with some examples for each.

Stop 🖐🏻
What are Complex Numbers? 🤷🏻 Those numbers which are made up of two parts – Real and Complex. And have form like a + bi where both a, b are real numbers while i is iota = √ -1 (Square root of -1).
Note – Complex numbers are not real but real number can be complex. Like if in a + bi, b = 0 then we will have a + 0i which is both real, complex.
That’s why functions in cMath Module can be applied to Integers/Floats as well but functions in Math Module can’t be applied to Complex Numbers.

How to create Complex Number in Python? For having simple numbers in Python, you just need to type it like 3, 2, 29. But owing to specific syntax of complex numbers a + bj. You need to you complex(a, b) constructor for creating these in Python, as a + bj will be considered just a String by Python Interpreter.

Just a Fact
In mathematics imaginary number is denoted by i (iota) but in Python conventionally its denoted by j

## Examples of using cmath Module in Python Code

``````# Firstly import cmath module
import cmath

# Printing Mathematical constants Pie, Square root of -1 iota
print(cmath.pi)
print(cmath.sqrt(-1))

# Above code outputs
3.141592653589793
1j``````

## Constants in cMath Python Module

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.