>>> list(range(1,11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> x=[x*x for x in range(1,11)]
>>> x
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> [x+y for x in 'abc' for y in '123']
['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
>>> improt os
File "<stdin>", line 1
improt os
^
SyntaxError: invalid syntax
>>> import os
>>> dir =[d for d in os.listdir()]
>>> dir
['.idea', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll', 'venv']
>>> def fib(max):
... n,a,b =0,0,1
... while n<max:
... print(b)
File "<stdin>", line 4
print(b)
^
IndentationError: expected an indented block
>>> def fib(max):
... n,a,b =0,0,1
... while n<max:
... print(b)
... a,b=b,a+b
... n=n+1
... return 'done'
...
>>> fib(10)
1
1
2
3
5
8
13
21
34
55
'done'
>>> from conllections import iterable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'conllections'
>>> from conllections import Iterable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'conllections'
>>> from collections import iterable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'iterable' from 'collections' (C:\python\lib\collections\__init__.py)
>>> from collections import Iterable
__main__:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
>>>
>>>
>>> isinstance([],Iterable)
True
>>> isinstance({},Iterable)
True
>>> isinstance('abc',Iterable)
True
>>> def f(x):
... return x*x
...
>>> r=map(f,[rang(1,11)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'rang' is not defined
>>> r=map(f,[range(1,11)])
>>> r
<map object at 0x000001FF9DB9EC50>
>>> list(r)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
TypeError: unsupported operand type(s) for *: 'range' and 'range'
>>> r=map(f,[1,2,3,4,5,6])
>>> r
<map object at 0x000001FF9DB9E860>
>>> list(r)
[1, 4, 9, 16, 25, 36]
>>> list(map(str,[23,4,5,%,77,]))
File "<stdin>", line 1
list(map(str,[23,4,5,%,77,]))
^
SyntaxError: invalid syntax
>>> list(map(str,[23,4,5,0,77,]))
['23', '4', '5', '0', '77']
>>> x=range(1,11)
>>> x
range(1, 11)
>>> x=list(range(1,11))
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> r=map(f,x)
>>> r
<map object at 0x000001FF9DBEFFD0>
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> list(x)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> sum(x)
55
>>> from functools import reduce
>>> def fn(x,y):
... return x*10+y
...
>>> reduce(fn,x)
1234567900
>>> def char2num(s):
... digits={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
... return digits[s]
...
>>> reduce(fn,map(char2num,'145171'))
145171
>>> digits={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
File "<stdin>", line 1
digits={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
^
IndentationError: unexpected indent
>>> digits={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
>>> def str2int(s):
... def fn(x,y)
File "<stdin>", line 2
def fn(x,y)
^
SyntaxError: invalid syntax
>>> def str2int(s):
... def fn(x,y):
... return x*10+y
... def char2num(s):
... return digits[s]
... return reduce(fn,map(char2num,s))
...
>>> str2int('15461346')
15461346