Python 3.9.7 | packaged by conda-forge | (default, Sep 23 2021, 07:28:37)
Type "copyright", "credits" or "license" for more information.
IPython 7.27.0 -- An enhanced Interactive Python.
In [1]: enumerate([45, 23, 12])
Out[1]: <enumerate at 0x7fae10590f80>
In [2]: A = _
In [3]: next(A)
Out[3]: (0, 45)
In [4]: next(A)
Out[4]: (1, 23)
In [5]: next(A)
Out[5]: (2, 12)
In [6]: next(A)
Traceback (most recent call last):
File "/tmp/ipykernel_19134/1844343699.py", line 1, in <module>
next(A)
StopIteration
In [7]: A
Out[7]: <enumerate at 0x7fae10590f80>
In [8]: lista = [ 3, 7, 34, 21, 54]
In [9]: for i in range(len(lista)):
...: print(i, lista[i])
...:
Output from spyder call 'get_cwd':
0 3
1 7
2 34
3 21
4 54
In [10]: for i, valore in enumerate(lista):
...: print(i, valore)
...:
0 3
1 7
2 34
3 21
4 54
In [11]: quadrati = []
In [12]: for valore in lista:
...: quadrati.append(valore**2)
...:
In [13]: quadrati
Out[13]: [9, 49, 1156, 441, 2916]
In [14]: quadrati = [ valore**2 for valore in lista ]
In [15]: quadrati
Out[15]: [9, 49, 1156, 441, 2916]
In [16]: quadrati_di_dispari = [ valore**2 for valore in lista if valore%2==1 ]
In [17]: quadrati_di_dispari
Out[17]: [9, 49, 441]
In [18]: def quadrato_più_cubo(X):
...: return X**2 + X**3
...:
In [19]: tabelline = []
In [20]: tabelline = [
...: [ X*Y for Y in range(1,11) ] for X in range(1, 11)
...: ]
In [21]: tabelline
Out[21]:
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20],
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30],
[4, 8, 12, 16, 20, 24, 28, 32, 36, 40],
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
[6, 12, 18, 24, 30, 36, 42, 48, 54, 60],
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70],
[8, 16, 24, 32, 40, 48, 56, 64, 72, 80],
[9, 18, 27, 36, 45, 54, 63, 72, 81, 90],
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]
In [22]: a, b = 1, 2, 3
Traceback (most recent call last):
File "/tmp/ipykernel_19134/1202805113.py", line 1, in <module>
a, b = 1, 2, 3
ValueError: too many values to unpack (expected 2)
In [23]: a, *b = 1, 2, 3
In [24]: a, b
Out[24]: (1, [2, 3])
In [25]: *a, b = 1, 2, 3
In [26]: a, b
Out[26]: ([1, 2], 3)
In [27]: a, *b, c = 1, 2, 3, 4, 5, 6
In [28]: a, b, c
Out[28]: (1, [2, 3, 4, 5], 6)
In [29]: *a, b, *c = 1, 2, 3, 4, 5, 6
File "/tmp/ipykernel_19134/907787962.py", line 1
*a, b, *c = 1, 2, 3, 4, 5, 6
^
SyntaxError: multiple starred expressions in assignment
In [30]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
Output from spyder call 'get_namespace_view':
S
In [31]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
In [32]: limiti_e_messaggi = {40 : 'o3',
...: 35 : 'o2',
...: 30 : 'o1',
...: 25 : 'S',
...: 18.5: 'N',
...: 16: 's',
...: 0: 'Starved' }
In [33]: limiti_e_messaggi.items()
Out[33]: dict_items([(40, 'o3'), (35, 'o2'), (30, 'o1'), (25, 'S'), (18.5, 'N'), (16, 's'), (0, 'Starved')])
In [34]: I = limiti_e_messaggi.items()
In [35]: next(I)
Traceback (most recent call last):
File "/tmp/ipykernel_19134/1643842136.py", line 1, in <module>
next(I)
TypeError: 'dict_items' object is not an iterator
In [36]: next(iter(I))
Out[36]: (40, 'o3')
In [37]: next(iter(I))
Out[37]: (40, 'o3')
In [38]: J = iter(I)
In [39]: next(J)
Out[39]: (40, 'o3')
In [40]: next(J)
Out[40]: (35, 'o2')
In [41]: next(J)
Out[41]: (30, 'o1')
In [42]: next(J)
Out[42]: (25, 'S')
In [43]: next(J)
Out[43]: (18.5, 'N')
In [44]: next(J)
Out[44]: (16, 's')
In [45]: next(J)
Out[45]: (0, 'Starved')
In [46]: next(J)
Traceback (most recent call last):
File "/tmp/ipykernel_19134/460280840.py", line 1, in <module>
next(J)
StopIteration
In [47]: list(I)
Out[47]:
[(40, 'o3'),
(35, 'o2'),
(30, 'o1'),
(25, 'S'),
(18.5, 'N'),
(16, 's'),
(0, 'Starved')]
In [48]: limiti_e_messaggi.keys()
Out[48]: dict_keys([40, 35, 30, 25, 18.5, 16, 0])
In [49]: list(limiti_e_messaggi.keys())
Out[49]: [40, 35, 30, 25, 18.5, 16, 0]
In [50]: list(limiti_e_messaggi.values())
Out[50]: ['o3', 'o2', 'o1', 'S', 'N', 's', 'Starved']
In [51]: len(limiti_e_messaggi)
Out[51]: 7
In [52]: limiti_e_messaggi[0]
Out[52]: 'Starved'
In [53]: limiti_e_messaggi[10]
Traceback (most recent call last):
File "/tmp/ipykernel_19134/4181109053.py", line 1, in <module>
limiti_e_messaggi[10]
KeyError: 10
In [54]: limiti_e_messaggi[0]
Out[54]: 'Starved'
In [54]:
In [54]:
In [54]:
In [55]: limiti_e_messaggi[0] = "denutrito"
In [56]: limiti_e_messaggi
Out[56]: {40: 'o3', 35: 'o2', 30: 'o1', 25: 'S', 18.5: 'N', 16: 's', 0: 'denutrito'}
In [57]: limiti_e_messaggi[0]
Out[57]: 'denutrito'
In [58]: dizio1 = { 'uno' : 1, 'tre': 3, 'quarantadue' : 42 }
In [59]: dizio2 = { 1 : 1, 2: 4, 3: 9 }
In [60]: dizio1.update(dizio2)
In [61]: dizio1
Out[61]: {'uno': 1, 'tre': 3, 'quarantadue': 42, 1: 1, 2: 4, 3: 9}
In [62]: dizio1.update({ 'tre' : 33 })
In [63]: dizio1
Out[63]: {'uno': 1, 'tre': 33, 'quarantadue': 42, 1: 1, 2: 4, 3: 9}
In [64]: dizio1[2:3]
Traceback (most recent call last):
File "/tmp/ipykernel_19134/1818719844.py", line 1, in <module>
dizio1[2:3]
TypeError: unhashable type: 'slice'
In [65]: insieme1 = { 1, 3, 5, 7, 1, 4, 2, 7, 5, }
In [66]: insieme1
Out[66]: {1, 2, 3, 4, 5, 7}
In [67]: set()
Out[67]: set()
In [68]: {}
Out[68]: {}
In [69]: type({})
Out[69]: dict
In [70]: A = set()
In [71]: type(A)
Out[71]: set
In [72]: A.add(1)
In [73]: A
Out[73]: {1}
In [74]: A.add(45)
In [75]: A.add(1)
In [76]: A
Out[76]: {1, 45}
In [77]: B = { 45, 17, 21 }
In [78]: A - B
Out[78]: {1}
In [79]: A + B
Traceback (most recent call last):
File "/tmp/ipykernel_19134/2673172716.py", line 1, in <module>
A + B
TypeError: unsupported operand type(s) for +: 'set' and 'set'
In [80]: A | B
Out[80]: {1, 17, 21, 45}
In [81]: A & B
Out[81]: {45}
In [82]: A.difference(B)
Out[82]: {1}
In [83]: A.intersection(B)
Out[83]: {45}
In [84]: A.union(B)
Out[84]: {1, 17, 21, 45}
In [85]: for elemento in A:
...: print(elemento)
...:
Output from spyder call 'get_cwd':
Output from spyder call 'get_cwd':
1
45
In [86]: sorted( A | B)
Out[86]: [1, 17, 21, 45]
In [87]: sorted( A | B, reverse=True)
Out[87]: [45, 21, 17, 1]
In [88]: testi = """
...: òljdsòlfkj
...: òlkdjflakj
...: p3urjfldkjjhworij
...: liuroiwfejlk
...: """
In [89]: testi = testi.split()
In [90]: testi
Out[90]: ['òljdsòlfkj', 'òlkdjflakj', 'p3urjfldkjjhworij', 'liuroiwfejlk']
In [91]: sorted(testi)
Out[91]: ['liuroiwfejlk', 'p3urjfldkjjhworij', 'òljdsòlfkj', 'òlkdjflakj']
In [92]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
In [93]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
In [94]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
In [95]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
Output from spyder call 'get_cwd':
N
devi dimagrire di 8 kilogrammi
In [96]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
devi ingrassare di 21 kilogrammi
In [97]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
evvai!
In [98]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
evvai!
devi dimagrire/ingrassare di 7.509999999999991 kili
In [99]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
evvai!
devi dimagrire/ingrassare di -20.002600000000015 kili
In [100]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
evvai!
devi dimagrire/ingrassare di 20.002600000000015 kili
In [101]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21/lezione05.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2021-22/Lezioni/lezione05-11-10-21')
N
evvai!
devi dimagrire/ingrassare di -7.509999999999991 kili
In [102]: lista = [ 1, 4, 7, 3, 67, 34 ]
In [103]: lista[3:6]
Out[103]: [3, 67, 34]
In [104]: lista[3:6] = []
In [105]: lista
Out[105]: [1, 4, 7]
In [106]: lista = [ 1, 4, 7, 3, 67, 34 ]
In [107]: del lista[3]
In [108]: lista
Out[108]: [1, 4, 7, 67, 34]
In [109]: lista.pop(3)
Out[109]: 67
In [110]: lista
Out[110]: [1, 4, 7, 34]
In [111]: lista.pop()
Out[111]: 34
In [112]: dizionario1 = { 'uno':1, 'due':2 , 'tre': 33}
In [113]: dizionario1['due']
Out[113]: 2
In [114]: del dizionario1['due']
In [115]: dizionario1
Out[115]: {'uno': 1, 'tre': 33}
In [116]: { valore**2 for valore in range(25) if valore%2==1 }
Out[116]: {1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529}
In [117]: { X : X**2 for X in range(25) if X%2==1 }
Out[117]:
{1: 1,
3: 9,
5: 25,
7: 49,
9: 81,
11: 121,
13: 169,
15: 225,
17: 289,
19: 361,
21: 441,
23: 529}
In [118]: import random
In [119]: { random.randint(1,6) for i in range(2) }
Out[119]: {2, 4}
In [120]: { random.randint(1,6) for i in range(2) }
Out[120]: {1, 3}
In [121]: { random.randint(1,6) for i in range(2) }
Out[121]: {1, 3}
In [122]: { random.randint(1,6) for i in range(2) }
Out[122]: {5, 6}
In [123]: { random.randint(1,6) for i in range(20) }
Out[123]: {1, 2, 3, 4, 5, 6}
In [124]: { random.randint(1,6) for i in range(20) }
Out[124]: {2, 3, 4, 5, 6}
In [125]: testi
Out[125]: ['òljdsòlfkj', 'òlkdjflakj', 'p3urjfldkjjhworij', 'liuroiwfejlk']
In [126]: sorted(testi, key=len)
Out[126]: ['òljdsòlfkj', 'òlkdjflakj', 'liuroiwfejlk', 'p3urjfldkjjhworij']
In [127]: len
Out[127]: <function len(obj, /)>
In [128]: dizionario1
Out[128]: {'uno': 1, 'tre': 33}
In [129]: sorted(dizionario1.items())
Out[129]: [('tre', 33), ('uno', 1)]
In [130]: dict(sorted(dizionario1.items()))
Out[130]: {'tre': 33, 'uno': 1}
In [131]: