Please enable JavaScript.
Coggle requires JavaScript to display documents.
W5 structured types - Coggle Diagram
W5 structured types
mutation, alias, clone
list - mutable
tuple - immutable
list and alias
object in memory
variable point to object
multiple variable can point to the same list (
alias
)
--> se cambio lista con variabile A,
si cambia lista anche con variabile B
a = [1, 2, 3]
b = a
alais
a = [1, 2, 3]
b = a[:]
clone
sort a list
sort()
: mutates a list
a.sort()
sorted()
: does not mutate
b = sorted(a)
list of list
alias
concatenations
print(a+b)
non cambia le liste
avoid
mutate
a list as you are
iterating
over it
list operation
append
L.append(4) --> [1, 2, 3, 4]
concatenation
L1 + L2 = L3
extension
L1.extend([5, 6]) --> [1, 2, 3, 4, 5, 6]
delete
del(L(1)) --> [1, 2, 3]
L.pop() --Z [1, 2]
remove last element
remove
L.remive(2) --> [1, 3]
string to list
list(s)
s.split()
" ".join(L) --> "1 2 3"
L.sort()
sorted(L)
L.reverse()
lists
ordered
mixed element types
mutable
iterable
L = [1, "two", 3]
len(L) = 3
L[0] = 1
L[0] + 1 = 2
index
può essere sia una funziona che una variabile
L[1] = 2
L 0 [1, 2, 3]
for i in range(len(L)):
for i in L:
functions as objects
first class objects
have types
element of data structures like list
can appears in expression
function argument of other functions
list of functions
HOP (High order procedure
map
map(int, [1, 2, 3])
b in map(int, [1, 2, 3]) (
iteration
)
Tuples
ordered
mix element types
immutable
iterable
t = (1, "two", 3)
t[0] = 1
t + (5, 6) = (1, "two", 3, 5, 6)
t[0:1] = (1, )
range
produce una tupletta
range(2,5) --> (2, 3, 4)