Python Part I · Module 2 · Variables and Data Types
Complex Numbers
Python has a third built-in number type alongside int and float, and this is a short chapter because that is proportional to how often you will use it. Complex numbers are built into the language rather than requiring a library, which is unusual and occasionally very convenient for signal-processing or control work. For most automation — counting errors, timing runs, reading reports — you will never write one. The goal here is recognition rather than fluency: enough that a complex number in someone else's code does not stop you, and enough to know the one piece of notation that catches engineers out, which is that Python writes the imaginary unit as j rather than i.
Foundation5 min readPythonPart IFundamentalsNumberscomplex
Part I · Module 2 · Chapter 2.7 · Complex Numbers
1. The Third Number Type
z = 3 + 4j
print(z)(3+4j)3 + 4j is a complex number with a real part of 3 and an imaginary part of 4. Python calls this type complex.
Note the output: Python printed it in brackets, (3+4j). That is simply how it displays a complex value.
2. Python Writes It as j, Not i
This is the one thing worth carrying away.
Mathematics, and most engineering education, writes the imaginary unit as i. Python — following the convention used in electrical engineering, where i already means current — writes it as j.
z = 3 + 4iSyntaxError: invalid decimal literalPython read 4i and could not make sense of it. Use 4j.
The number must also be attached directly to the j, with nothing between them. 4j is a value; 4 j is not.
3. Reading the Two Parts
A complex value can tell you its real and imaginary parts:
z = 3 + 4j
print(z.real)
print(z.imag)3.0
4.0Both come back as floats, even though you wrote whole numbers — which is consistent with Chapter 2.6, where a float is the type for a quantity that could have a fractional part.
There is also a built-in for the magnitude:
z = 3 + 4j
print(abs(z))5.0abs is a name Python provides, used the same way print is — write the name, then brackets, then the value. For a complex number it gives the distance from zero, which for 3 and 4 is 5.
4. When You Will Actually Need This
Rarely, and the honest answer is worth stating.
Complex numbers are genuinely useful in signal processing, filter design, impedance calculations and control theory. If your work touches those, having them built into the language — rather than needing a library — is a real convenience.
For the automation this course builds toward in Part II — launching tools, reading logs, counting results, producing reports — you will not use them. That is why this chapter is short.
5. Predict, Run, Observe
z = 1 + 2j
print(z)
print(z.real)
print(z.imag)
print(abs(3 + 4j))(1+2j)
1.0
2.0
5.0The thing to check: did you predict 1.0 and 2.0 rather than 1 and 2? If not, re-read §3 — the parts come back as floats.
6. Exercise
Exercise 1 — write one. Assign a complex number with a real part of 5 and an imaginary part of −2, and print it. Check how Python displays a negative imaginary part.
Exercise 2 — predict the error. Predict what happens, then run it:
z = 2 + 3iWhich error appears, and what should the line have been?
Exercise 3 — the type of the parts. Print z.real for z = 3 + 4j. Explain in one sentence why the output has a decimal point when you never typed one.
7. Summary
complexis Python's third built-in number type, alongsideintandfloat.- The imaginary unit is written
j, noti, and attaches directly to its number:4j. z.realandz.imaggive the two parts, both as floats.abs(z)gives the magnitude.- Most engineering automation never uses this. Recognise it; do not memorise it.
That completes Python's numbers. The next chapter covers the two value kinds that record a decision rather than a quantity — and the one that records the absence of an answer.
Where this fits
Part of the Python curriculum.
