1. 使用`complex()`函数创建复数:
z = complex(real, imag)
其中`real`是实部,`imag`是虚部。
2. 直接使用实部和虚部创建复数:
z = real + imag * 1j
3. 使用字符串表示法创建复数:
z = 'real + imagj'
import random
real = random.uniform(-1, 1)
imag = random.uniform(-1, 1)
z = complex(real, imag)
5. 使用`numpy`库生成随机复数:
import numpy as np
z = np.random.uniform(-1, 1, 2) + np.random.uniform(-1, 1, 2) * 1j
以上是创建复数的一些常见方法。