print((lambda x:[x+1 for x in range(x)])(9))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
# 等价下面的代码
def f(x):
for i in range(x):
yield (i+1)
print(list(f(9)))
>>> [x**2 for x in range(1, 11) if x //2==1]
[4, 9]
>>> [x**2 for x in range(1, 11) if x%2==0 ]
[4, 16, 36, 64, 100]
[x**2 if x<3 else x for x in range(1, 11) ]
[1, 4, 3, 4, 5, 6, 7, 8, 9, 10]
input("\n\n按下 enter 键后退出。")
>>> import sys; x = 'runoob'; sys.stdout.write(x + '\n')
runoob
7
>>>i = iter([1,2,3,4,5])
<list_iterator object at 0x0000025AF9C8B8B0>
next(i)
>>> lst = [1, 2, 3]
>>> f=lambda x: [i for i in x]
>>> f(lst)
[1, 2, 3]
>>> f=lambda x: [print(i) for i in x]
>>> f(lst)
1
2
3
[None, None, None]
>>> k=f(lst)
1
2
3
>>> import keyword
>>> keyword
<module 'keyword' from 'D:\\Program Files\\Python\\Python312\\Lib\\keyword.py'>
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
p=[print(i) for i in keyword.kwlist]
False
None
True
and
as
assert
..............
>>> f=lambda x: [i for i in range(x)] # lambda中使用for
>>> f(8)
[0, 1, 2, 3, 4, 5, 6, 7]