Python 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05)
Type "copyright", "credits" or "license" for more information.
IPython 7.18.1 -- An enhanced Interactive Python.
In [1]: map( sum, [1, 3, 5, 7], [2, 4, 6, 8] )
Out[1]: <map at 0x7fd0b862f490>
In [2]: list(_)
Traceback (most recent call last):
File "<ipython-input-2-1a5b60e28d67>", line 1, in <module>
list(_)
TypeError: 'int' object is not iterable
In [3]: list(map( sum, [1, 3, 5, 7], [2, 4, 6, 8] ))
Traceback (most recent call last):
File "<ipython-input-3-042e889f3add>", line 1, in <module>
list(map( sum, [1, 3, 5, 7], [2, 4, 6, 8] ))
TypeError: 'int' object is not iterable
In [4]: list(map( lambda x, y: x+y, [1, 3, 5, 7], [2, 4, 6, 8] ))
Out[4]: [3, 7, 11, 15]
In [5]: def somma(x,y): return x+y
In [6]: list(map( somma, [1, 3, 5, 7], [2, 4, 6, 8] ))
Out[6]: [3, 7, 11, 15]
In [7]: lambda x,y: x+y
Out[7]: <function __main__.<lambda>(x, y)>
In [8]: somma2 = _
In [9]: somma2
Out[9]: <function __main__.<lambda>(x, y)>
In [10]: somma2(2, 56)
Out[10]: 58
In [11]: A = [1, 3, 5, 7]
In [12]: B = [2, 4, 6, 8]
In [13]: zip(A,B)
Out[13]: <zip at 0x7fd0b85c37c0>
In [14]: list(zip(A,B))
Out[14]: [(1, 2), (3, 4), (5, 6), (7, 8)]
In [15]: list(zip(A,B,A,B))
Out[15]: [(1, 2, 1, 2), (3, 4, 3, 4), (5, 6, 5, 6), (7, 8, 7, 8)]
In [16]: [ a+b for a,b in zip(A,B) ]
Out[16]: [3, 7, 11, 15]
In [17]: [ somma2(a,b) for a,b in zip(A,B) ]
Out[17]: [3, 7, 11, 15]
In [18]: [ lambda x,y: x+y for a,b in zip(A,B) ]
Out[18]:
[<function __main__.<listcomp>.<lambda>(x, y)>,
<function __main__.<listcomp>.<lambda>(x, y)>,
<function __main__.<listcomp>.<lambda>(x, y)>,
<function __main__.<listcomp>.<lambda>(x, y)>]
In [19]: list(map(somma2, A, B))
Out[19]: [3, 7, 11, 15]
In [20]: sum(A)
Out[20]: 16
In [21]: def ispari(x) : return x%2 == 0
In [22]: import random
In [23]: C = [ random.randint(1,100) for _ range(20) ]
File "<ipython-input-23-66fdd29887f1>", line 1
C = [ random.randint(1,100) for _ range(20) ]
^
SyntaxError: invalid syntax
In [24]: C = [ random.randint(1,100) for _ in range(20) ]
In [25]: C
Out[25]: [27, 44, 43, 86, 56, 77, 80, 60, 88, 90, 44, 13, 3, 29, 14, 66, 97, 35, 67, 52]
In [26]: list(map(ispari, C))
Out[26]:
[False,
True,
False,
True,
True,
False,
True,
True,
True,
True,
True,
False,
False,
False,
True,
True,
False,
False,
False,
True]
In [27]: %pprint
Pretty printing has been turned OFF
In [28]: list(map(ispari, C))
Out[28]: [False, True, False, True, True, False, True, True, True, True, True, False, False, False, True, True, False, False, False, True]
In [29]: C
Out[29]: [27, 44, 43, 86, 56, 77, 80, 60, 88, 90, 44, 13, 3, 29, 14, 66, 97, 35, 67, 52]
In [30]: any(map(ispari, C))
Out[30]: True
In [31]: all(map(ispari, C))
Out[31]: False
In [32]: all(ispari(x) for x in C)
Out[32]: False
In [33]: [ispari(x) for x in C]
Out[33]: [False, True, False, True, True, False, True, True, True, True, True, False, False, False, True, True, False, False, False, True]
In [34]: (ispari(x) for x in C)
Out[34]: <generator object <genexpr> at 0x7fd0b0209190>
In [35]: list(_)
Out[35]: [False, True, False, True, True, False, True, True, True, True, True, False, False, False, True, True, False, False, False, True]
In [36]: all(ispari(x) for x in C)
Out[36]: False
In [37]: all( [ispari(x) for x in C] )
Out[37]: False
In [38]: [ispari(x) for x in C]
Out[38]: [False, True, False, True, True, False, True, True, True, True, True, False, False, False, True, True, False, False, False, True]
In [39]: filter(ispari, C)
Out[39]: <filter object at 0x7fd0b855fa90>
In [40]: list(filter(ispari, C))
Out[40]: [44, 86, 56, 80, 60, 88, 90, 44, 14, 66, 52]
In [41]: list(filter( lambda x: not ispari(x), C))
Out[41]: [27, 43, 77, 13, 3, 29, 97, 35, 67]
In [42]: list(filter( lambda x: x%2 == 1, C))
Out[42]: [27, 43, 77, 13, 3, 29, 97, 35, 67]
In [42]:
In [42]:
In [43]: tran = str.maketrans("ABC", "abc")
In [44]: tran
Out[44]: {65: 97, 66: 98, 67: 99}
In [45]: tran = str.maketrans("ABC", "abc", "RST")
In [46]: tran
Out[46]: {65: 97, 66: 98, 67: 99, 82: None, 83: None, 84: None}
In [47]: "SAGSETYFSNAETHA".translate(tran)
Out[47]: 'aGEYFNaEHa'
In [48]: ( x for x in filter(not ispari(y) for y in C))
Traceback (most recent call last):
File "<ipython-input-48-56499f63c933>", line 1, in <module>
( x for x in filter(not ispari(y) for y in C))
TypeError: filter expected 2 arguments, got 1
In [49]: ( x for x in filter(lambda y: not ispari(y), C))
Out[49]: <generator object <genexpr> at 0x7fd0b020b3c0>
In [50]: list(_)
Out[50]: [27, 43, 77, 13, 3, 29, 97, 35, 67]
In [51]: ( x for x in filter(lambda y: not ispari(y), C))
Out[51]: <generator object <genexpr> at 0x7fd0b0310190>
In [52]: GG = _
In [53]: GG
Out[53]: <generator object <genexpr> at 0x7fd0b0310190>
In [54]: for x in GG: print(X)
Traceback (most recent call last):
File "<ipython-input-54-47ee2e6e7d69>", line 1, in <module>
for x in GG: print(X)
NameError: name 'X' is not defined
In [55]: for x in GG: print(x)
43
77
13
3
29
97
35
67
In [56]: for x in GG: print(x)
In [57]: GG = ( x for x in filter(lambda y: not ispari(y), C))
In [58]: GG2 = GG.copy()
Traceback (most recent call last):
File "<ipython-input-58-5812664d8b73>", line 1, in <module>
GG2 = GG.copy()
AttributeError: 'generator' object has no attribute 'copy'
In [59]: [ x in filter(ispari, C) ]
Out[59]: [False]
In [60]: C
Out[60]: [27, 44, 43, 86, 56, 77, 80, 60, 88, 90, 44, 13, 3, 29, 14, 66, 97, 35, 67, 52]
In [61]: list(filter(ispari, C))
Out[61]: [44, 86, 56, 80, 60, 88, 90, 44, 14, 66, 52]
In [62]: [ x for x in filter(ispari, C) ]
Out[62]: [44, 86, 56, 80, 60, 88, 90, 44, 14, 66, 52]
In [63]: [ x for x in filter(lambda y: y%3==0, C) ]
Out[63]: [27, 60, 90, 3, 66]
In [64]: F = open('paperino.txt', mode='w', encoding='utf-8')
In [65]: F
Out[65]: <_io.TextIOWrapper name='paperino.txt' mode='w' encoding='utf-8'>
In [66]: F.write("Paperino andò al mare.")
Out[66]: 22
In [67]: F.write('\n')
Out[67]: 1
In [68]: print("Minnie andò a sciare.", end="\n\n", file=F)
In [69]: print("Minnie andò a sciare.", end="\n\n")
Minnie andò a sciare.
In [70]: F.close()
In [71]: with open('paperino.txt', mode='a', encoding='utf8') as F:
...: for i in range(10):
...: print(i)
...:
0
1
2
3
4
5
6
7
8
9
In [72]: with open('paperino.txt', mode='a', encoding='utf8') as F:
...: for i in range(10):
...: print(i, file=F)
...:
In [73]: A = 34
In [74]: B = 56
In [75]: f"calcoliamo {A}*{B} = {A*B}"
Out[75]: 'calcoliamo 34*56 = 1904'
In [76]: f"""calcoliamo {A}*{B} = {A*B}"""
Out[76]: 'calcoliamo 34*56 = 1904'
In [77]: f"""calcoliamo {A}*{B} = {A*B}"""
Out[77]: 'calcoliamo 34*56 = 1904'
In [78]: f"""calcoliamo
...: {A}*{B} = {A*B}"""
Out[78]: 'calcoliamo\n34*56 = 1904'
In [79]: print(f"""calcoliamo
...: {A}*{B} = {A*B}""")
calcoliamo
34*56 = 1904
In [80]: with open('minni.txt') as F:
...: print(F.read())
...:
Traceback (most recent call last):
File "<ipython-input-80-632bd5ce5ee6>", line 1, in <module>
with open('minni.txt') as F:
FileNotFoundError: [Errno 2] No such file or directory: 'minni.txt'
In [81]: try:
...: with open('minni.txt') as F:
...: print(F.read())
...: except:
...: print("è avvenuto un errore")
...:
è avvenuto un errore
In [82]: try:
...: with open('minni.txt') as F:
...: print(F.read())
...: except as e:
...: print("è avvenuto un errore", e)
File "<ipython-input-82-df0d6508e02b>", line 4
except as e:
^
SyntaxError: invalid syntax
In [83]: try:
...: with open('minni.txt') as F:
...: print(F.read())
...: except Exception e:
...: print("è avvenuto un errore", e)
File "<ipython-input-83-4eed66c72e4c>", line 4
except Exception e:
^
SyntaxError: invalid syntax
In [84]: try:
...: with open('minni.txt') as F:
...: print(F.read())
...: except Exception as e:
...: print("è avvenuto un errore", e)
...:
è avvenuto un errore [Errno 2] No such file or directory: 'minni.txt'
In [85]: try:
...: with open('minni.txt') as F:
...: print(F.read())
...: except FileNotFoundError as e:
...: print("è avvenuto un errore", e)
...:
è avvenuto un errore [Errno 2] No such file or directory: 'minni.txt'
In [86]: try:
...: with open('minni.txt') as F:
...: print(F.read())
...: except FileNotFoundError as e:
...: print("è avvenuto un errore", e)
...:
è avvenuto un errore [Errno 2] No such file or directory: 'minni.txt'
In [87]: try:
...: x = 45/0
...: with open('minni.txt') as F:
...: print(F.read())
...: except FileNotFoundError as e:
...: print("è avvenuto un errore", e)
...:
Traceback (most recent call last):
File "<ipython-input-87-75285362bcfd>", line 2, in <module>
x = 45/0
ZeroDivisionError: division by zero
In [88]: try:
...: x = 45/0
...: with open('minni.txt') as F:
...: print(F.read())
...: except Exception as e:
...: print("è avvenuto un errore", e)
...:
è avvenuto un errore division by zero
In [89]: try:
...: x = 45/0
...: with open('minni.txt') as F:
...: print(F.read())
...: except ZeroDivisionError:
...: print("divisione per zero")
...: except Exception as e:
...: print("è avvenuto un errore", e)
...:
divisione per zero
In [90]: try:
...: with open('minni.txt') as F:
...: print(F.read())
...: except ZeroDivisionError:
...: print("divisione per zero")
...: except Exception as e:
...: print("è avvenuto un errore", e)
...:
è avvenuto un errore [Errno 2] No such file or directory: 'minni.txt'
In [91]: