Nearby lessons

17 of 159

Python - complex Data Type

📌 What You Will Learn
  • Define the complex data type and the form a + bj
  • Identify the real and imaginary parts of a complex number
  • Understand the number systems allowed for the real and imaginary parts
  • Perform addition, subtraction, and multiplication on complex numbers
  • Access the real and imaginary parts using the real and imag attributes

Introduction

The complex data type is used to represent complex numbers.

A complex number is written in the form a + bj.

  • a = Real Part
  • b = Imaginary Part
  • j = Imaginary Unit (√-1)

Structure of a Complex Number

Part Description
a Real Part
b Imaginary Part
j Imaginary Unit (√-1)

Examples of Complex Numbers

Loading editor…

Real Part of a Complex Number

The real part of a complex number can be represented using different number systems.

  • Decimal
  • Binary
  • Octal
  • Hexadecimal

Example

Loading editor…

Imaginary Part of a Complex Number

The imaginary part must always be written in decimal form.

Binary, octal, and hexadecimal values are not allowed in the imaginary part.

Invalid Example

Loading editor…

Arithmetic Operations on Complex Numbers

Python supports arithmetic operations on complex numbers.

Addition

Loading editor…

Subtraction

Loading editor…

Multiplication

Loading editor…

Accessing Real and Imaginary Parts

Python provides two built-in attributes for complex numbers.

Attribute Purpose
real Returns the real part.
imag Returns the imaginary part.

Example

Loading editor…

Checking the Data Type

The type() function returns the data type of a value.

Loading editor…

Important Mathematical Note

For complex numbers:

j² = -1

Applications of Complex Numbers

Complex numbers are commonly used in:

  • Scientific Applications
  • Electrical Engineering
  • Electronics
  • Signal Processing
  • Electrical Circuits
  • Mathematical Calculations

Difference Between int, float, and complex

Data Type Example Description
int 10 Whole numbers
float 10.5 Decimal numbers
complex 10+5j Complex numbers

Important Points

  • Complex numbers are immutable.
  • The real part supports decimal, binary, octal, and hexadecimal values.
  • The imaginary part supports only decimal values.
  • Python supports arithmetic operations on complex numbers.
  • Use real and imag to access different parts of a complex number.

Key Points

  • The complex data type represents complex numbers.
  • A complex number is written in the form a + bj.
  • The real part can use different number systems.
  • The imaginary part must always be in decimal form.
  • Use type() to check the data type.
  • Use real and imag to access the real and imaginary parts.

Quick Summary

Feature Description
Data Typecomplex
PurposeRepresents complex numbers
Formata + bj
Real PartSupports decimal, binary, octal, and hexadecimal
Imaginary PartDecimal only
Attributesreal, imag
MutableNo (Immutable)
📝 Key Takeaways
  • A complex number is written in the form a + bj
  • The real part supports decimal, binary, octal, and hexadecimal values
  • The imaginary part must always be in decimal form
  • Python supports arithmetic operations on complex numbers
  • Use the real and imag attributes to access parts of a complex number

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.