• Python

Command Palette

Search for a command to run...

Native Functions
  • Python Abs Function
  • Python Any All Functions
  • Python Zip Function
  • Python Sum Function
  • Python Filter Function
  • Python Max Min Functions
  • Python Map Function
  • Python Round Function
  • Python Sorted Function
Data Types
  • Python Integers
  • Python Floats
  • Python Complex Numbers
  • Python List Type
  • Python Tuple Type
  • Python String Type
  • Python Range Type
Collections.abc Module
  • Python Containers
  • Python Hashable
  • Python Iterable
  • Python Iterators
  • Python Sequence
  • Python Mutable Sequence

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

What Are Complex Numbers?Creating Complex NumbersAccessing Real and Imaginary PartsBasic Operations with Complex NumbersAbsolute Value (Magnitude) of a Complex NumberConjugate of a Complex NumberUsing Complex Numbers in Math FunctionsWhen to Use Complex NumbersSummary
      • Blog

      Python Complex Numbers

      What Are Complex Numbers?

      A complex number is a number that has both a real part and an imaginary part. In mathematics, it is written as a + bj, where a is the real part and b is the imaginary part. In Python, complex numbers are built-in and easy to use.

      Creating Complex Numbers

      You can create a complex number in Python using the complex() function or by writing it directly with a j (or J) to indicate the imaginary part.

      z1 = 3 + 4j
      z2 = complex(2, -5)
      print(z1)  # Output: (3+4j)
      print(z2)  # Output: (2-5j)
      
      • The real part is before the + or -, and the imaginary part is after, followed by j.
      • The complex(real, imag) function creates a complex number from two values.

      Accessing Real and Imaginary Parts

      Each complex number in Python has two attributes:

      • .real: The real part
      • .imag: The imaginary part
      z = 3 - 4j
      print(z.real)  # Output: 3.0
      print(z.imag)  # Output: -4.0
      

      Basic Operations with Complex Numbers

      You can add, subtract, multiply, and divide complex numbers just like regular numbers:

      a = 1 + 2j
      b = 3 - 4j
      print(a + b)  # Output: (4-2j)
      print(a - b)  # Output: (-2+6j)
      print(a * b)  # Output: (11+2j)
      print(a / b)  # Output: (-0.2+0.4j)
      

      Absolute Value (Magnitude) of a Complex Number

      The absolute value (or magnitude) of a complex number is its distance from zero in the complex plane. You can calculate it using the abs() function:

      z = 3 - 4j
      print(abs(z))  # Output: 5.0 (since sqrt(3**2 + 4**2) = 5)
      
      • This is useful in mathematics, engineering, and physics to measure the size of a complex number.

      Conjugate of a Complex Number

      The conjugate of a complex number reverses the sign of the imaginary part. In Python, use the .conjugate() method:

      z = 3 + 4j
      print(z.conjugate())  # Output: (3-4j)
      

      Using Complex Numbers in Math Functions

      The cmath module provides mathematical functions for complex numbers, such as square roots, exponentials, and trigonometric functions:

      import cmath
      z = 1 + 1j
      print(cmath.sqrt(z))  # Output: (1.0986841134678098+0.45508986056222733j)
      
      • Use cmath instead of math for complex numbers.

      When to Use Complex Numbers

      Complex numbers are used in many fields:

      • Electrical engineering (AC circuits)
      • Signal processing
      • Physics (wave equations)
      • Mathematics (roots of negative numbers)

      Summary

      • Complex numbers have a real and an imaginary part.
      • Create them with a + bj or complex(a, b).
      • Use .real, .imag, and .conjugate() for properties.
      • Use abs() for magnitude and cmath for advanced math.
      • Useful in science, engineering, and advanced math problems.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python Functions Scope

      For You

      Where Variables Live: Understanding Scope In Python, not all variables are accessible from every par

      Python Sets

      For You

      Working with Unique Items: Understanding Sets in Python Imagine you have a list of items, and some i

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.