Remember
complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.
complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = complex(3, 2)
print(number)
print(number.real)Top-level scope
(3+2j)complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = complex(3, 2)
print(number)
print(number.real)
|
v
DISPLAYED RESULT
(3+2j)
3.0
FINAL BINDINGS
number --> object 1 (complex)
object 1: (3+2j)Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = complex(3, 2)
print(number)
print(number.real)Top-level scope
(3+2j)The example has finished. Compare the final output with the values and references above.
(3+2j) 3.0
What do you expect this program to print?
number = complex(3, 2)
print(number)
print(number.real)complex() FunctionThe complex() function in Python is used to create complex numbers. A complex number has a real part and an imaginary part, like 3 + 4j.
This function is useful in math, simulations, electrical engineering, and many scientific applications.
complex(real=0, imag=0)
real (optional) – The real part (default is 0)imag (optional) – The imaginary part (default is 0)real + imag*jz = complex(3, 4)
print(z)
(3+4j)
z = complex(5)
print(z)
(5+0j)
z = complex("2+3j")
print(z)
(2+3j)
Note: If you use a string, it must be a valid complex number format.
z = complex(3, 4)
print(z.real) # Output: 3.0
print(z.imag) # Output: 4.0
complex("2 + 3j") will raise a ValueError.complex("3+4j", 2) is invalid.Complex numbers are rarely asked directly in interviews, but you may encounter them in signal processing or competitive math problems. Know how to extract .real and .imag values.
complex() creates a complex number..real and .imag.Write a program that reads two numbers from the user and creates a complex number:
real = float(input("Enter real part: "))
imag = float(input("Enter imaginary part: "))
z = complex(real, imag)
print("The complex number is:", z)
Connect what you noticed in the example with the idea below.
complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.
The second argument to complex() is another real component.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.