a = [y for y in range(1, 26)] # 列表表达式
b = list(range(1, 26)) # 将range转换为list对象
c = []
for i in range(1, 26):
c.append(i)
print(a)
print(b)
print(c)
运行结果:
PS A:\python_file> python -u "a:\python_file\project\practice\1.py"
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
注意,如果写成print(a,"\n",b,"\n",c)
就会出现下面这种情况列表前面多一个空格,原因是逗号在print中充当空格的作用:
PS A:\python_file> python -u "a:\python_file\project\practice\1.py"
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
a = [i for i in range(1, 26) if i % 2 == 0]
print(a)
运行结果:
PS A:\python_file> python -u "a:\python_file\project\practice\1.py"
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
a = list(filter(lambda x: True if x % 2 == 0 else False, range(1, 26)))
print(a)
运行结果:
PS A:\python_file> python -u "a:\python_file\project\practice\1.py"
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
a = "变量一" if 条件 else "变量二" # 意义:如果满足条件那么a=变量一,否则等于变量二