C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
[1,2,3,4,5,6,7,8,9,10]
Process finished with exit code 0
用一行语句代替循环生成列表
print([x * x for x inrange(1,11)])
C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
[1,4,9,16,25,36,49,64,81,100]
Process finished with exit code 0
for循环后面还可以加上if判断
print([x * x for x inrange(1,11)if x %2==0])
C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
[4,16,36,64,100]
Process finished with exit code 0
使用两层循环
print([m + n for m in'ABC'for n in'XYZ'])
C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
['AX','AY','AZ','BX','BY','BZ','CX','CY','CZ']
Process finished with exit code 0