02.py (527B)
1 #!/usr/bin/env python3 2 def Complex(r, i): 3 """ 4 Construct a complex number 5 Arguments: 6 r: real part 7 i: imaginary part 8 """ 9 return (r, i) 10 11 12 # interface with complex numbers 13 def real(c): 14 """Get the real part of a complex number c""" 15 return c[0] 16 17 18 def imag(c): 19 """Get the imaginary part of a complex number c""" 20 return c[1] 21 22 23 def string(c): 24 """Represent a complex number c as a string""" 25 return f"{c[0]:e} + i{c[1]:e}" 26 27 28 z = Complex(1, 2) 29 print(real(z), imag(z), string(z))