• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Data Types / Complex numbers in Python

Complex numbers in Python

Author: Aditya Raj
Last Updated: June 22, 2021

While working on data science, machine learning or scientific calculations, we often need to perform calculations on numeric data types including  complex numbers. In this article, we will learn how to define and use complex numbers in python. 

What is a complex number ?

A complex number is a number that can be written in the form of (a+b j) where a and b are real numbers. Here j is an imaginary number and is defined to be the square root of -1. Complex numbers occur in pairs and are mostly used in calculations involving square roots of negative numbers.

How to define complex numbers in Python?

We can define complex numbers in python by simply declaring a variable and assigning an expression of the form (a+bj) .Here “a” and “b” should be a numeric literal and “j” can be any alphabet character. We can also check the data type of the defined variable using type() function as follows.

myNum= 3+2j
print("The Number is:")
print(myNum)
print("Data type of Number is:")
print(type(myNum))

Output:

The Number is:
(3+2j)
Data type of Number is:
<class 'complex'>

We can also define a complex number using complex() function. The complex function takes a compulsory input as first parameter which denotes the real part of the complex number and an optional input as second parameter which denotes the imaginary part of the complex number. We can define a complex number using the complex() function as follows.

myNum= complex(3,2)
print("The Number is:")
print(myNum)
print("Data type of Number is:")
print(type(myNum))

Output:

The Number is:
(3+2j)
Data type of Number is:
<class 'complex'>

Extracting real and imaginary part of a complex number

In a complex number (a+b j), “a” is called the real part and “b” is called the imaginary part. We can extract real part of the complex number using an attribute named “real” which contains value “a” and imaginary part using the attribute “imag” which contains the value “b” as follows.

myNum= complex(3,2)
print("The Complex Number is:")
print(myNum)
print("Real part of the complex Number is:")
print(myNum.real)
print("Imaginary part of the complex Number is:")
print(myNum.imag)

Output:


The Complex Number is:
(3+2j)
Real part of the complex Number is:
3.0
Imaginary part of the complex Number is:
2.0

Conjugate of a complex number in Python

For a complex number (a+ b j), its conjugate is defined as the complex number (a- b j).We can obtain the conjugate of any complex number in python using the conjugate() method. The conjugate() method when invoked on a complex number, returns the complex conjugate of the number. This can be done as follows.

myNum= complex(3,2)
print("The Complex Number is:")
print(myNum)
print("Conjugate of the complex Number is:")
print(myNum.conjugate())

Output:

The Complex Number is:
(3+2j)
Conjugate of the complex Number is:
(3-2j)

Magnitude of the complex number

The magnitude of a complex number (a+b j) is the distance of the point (a,b) from (0,0). It is the length of the vector which represents the complex number. The magnitude of a complex number can be calculated as follows in python.

import math
def magnitude(num):
    x=num.real
    y=num.imag
    mag=x*x+y*y
    return math.sqrt(mag)
myNum= complex(3,2)
print("The Complex Number is:")
print(myNum)
print("Magnitude of the complex Number is:")
print(magnitude(myNum))

Output:

The Complex Number is:
(3+2j)
Magnitude of the complex Number is:
3.605551275463989

Conclusion

In this article, we have studied the basics of complex numbers and its attributes in python. We have also derived the real part, imaginary part , conjugate and magnitude of a complex number. We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Data Types Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us