首页 > 解决方案 > 如何创建创建和打印正方形列表的代码?

问题描述

我必须编写一个可以创建和打印正方形列表的代码,但不知道该怎么做。到目前为止,我所拥有的是:

import string
import cmath


complex = [(i*3j) for i in range(1,21)]

我需要它看起来像这样在此处输入图像描述

标签: python

解决方案


可以使用直接赋值语句或使用 complex() 函数来创建 Python 复数。我已经使用了下面的复杂功能。尝试 :

complx = []
real_ = []
imag_ = []

for i in range(1,21):
    num = complex((-9*(i**2)),0)
    complx.append(num)
    real_.append(num.real)
    imag_.append(num.imag)
    
print('complx:',complx)
print('real:',real_)
print('Imaginary:',imag_)

输出:

 complx: 
[(-9+0j), (-36+0j), (-81+0j), (-144+0j), (-225+0j), (-324+0j), (-441+0j), (-576+0j), (-729+0j), (-900+0j), (-1089+0j), (-1296+0j), (-1521+0j), (-1764+0j), (-2025+0j), (-2304+0j), (-2601+0j), (-2916+0j), (-3249+0j), (-3600+0j)]

real: 
[-9.0, -36.0, -81.0, -144.0, -225.0, -324.0, -441.0, -576.0, -729.0, -900.0, -1089.0, -1296.0, -1521.0, -1764.0, -2025.0, -2304.0, -2601.0, -2916.0, -3249.0, -3600.0]

Imaginary: 
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

推荐阅读