Finding Magnitude of a Complex Number in Python
Hello! In this article, We use Python to get the Magnitude of a Complex Number using a simple snippet.
First, let us learn the basics of complex numbers in Python.
Complex Numbers in Python
Let us first see how to Initialize or declare a complex number in Python.
We can create a complex number by initializing a variable to p + qj. Here p and q belong to the set of real numbers.
cn = 1 + 3j
In the above example, the real part(p) is 1 and imaginary part(q) is 3.
We can also create a complex number using the complex([real][,imaginary]) method. It takes the real part and imaginary part as its parameters respectively.
If a parameter is omitted, then the respective part of the complex number is set to the default value. The default value is 0.
cn = complex(1,3)
In this program, we are going to make use of abs() function to get the magnitude of a complex number. The method abs() takes only one argument.
The argument can be :
1. Floating point Number
2. Complex Number
The abs(number) returns :
1. Modulus of the number if it is an Integer or Floating point.
2. Magnitude if the number is Complex.
Let us first declare complex number cn using any of the methods that we have discussed earlier.
cn = complex(3, 4)
Let us now find and also print the magnitude of the above complex number using abs() method.
Yahoo! We have learned to find the magnitude of a complex number using a single line of code.
Thank you for reading the article. I hope this article helped you in some way. Also do check out our other related articles below :