L = [10, 20, 30]
L
[] # [] is the empty list
L[1], len(L), len([])
L[2] = 33
L
L = [11, 22, 33]
L[-1], L[-2], L[-3]
L = [0, 11, 22, 33, 44, 55]
L[2:4]
L[-4:4]
L[2:-2]
L[:4]
L[4:]
L = [0, 11, 22, 33, 44, 55, 66, 77]
L[2:6] = [12, 13, 14] # substitutes [22, 33, 44, 55]
L
L = [0, 11, 22, 33, 44, 55, 66, 77]
L[:1] = [] # delete the first term of a list
L
L = [0, 11, 22, 33, 44, 55, 66, 77]
L[-1:] = [] # delete the last term of a list
L
L = [0, 11, 22, 33, 44, 55, 66, 77]
L[:0] = [100] # insert 100 in front of a list
L
L = [0, 11, 22, 33, 44, 55, 66, 77]
L[len(L):] = [100] # insert 100 in tail of a list
L
L = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
L[3:len(L)-5] == L[3-len(L):-5]
[5 in L, 6 in L]
L = [1, 2, 3]
L + [10, 20, 30]
4 * [1, 2, 3]
L = 5*[10, 20, 30]
L[:3]+L[3:] == L
[1..3, 7, 10..13]
[1, 4..20, 100, -20..-10]
G = map(cos, [0, pi/6, pi/4, pi/3, pi/2]) # generator
L = list(G) # list
L
list(G)
G = map(lambda t: cos(t), [0, pi/6, pi/4, pi/3, pi/2])
list(G)
G = map(lambda t: N(cos(t)), [0, pi/6, pi/4, pi/3, pi/2])
print(list(G))
G = map(N, map(cos, [0, pi/6, pi/4, pi/3, pi/2]))
print(list(G))
G = map(compose(N, cos), [0, pi/6, pi/4, pi/3, pi/2])
print(list(G))
G = filter(is_prime, [1..55])
print(list(G))
p = 37
G = filter(lambda n: n^4 % p == 7, [0..p-1])
print(list(G))
G = map(lambda n: 2*n+1, [0..15])
print(list(G))
[2*n+1 for n in [0..15]] # list comprehension
G = filter(is_prime, [1..55])
print(list(G))
[p for p in [1..55] if is_prime(p)]
G = filter(is_prime, [4*n+1 for n in [0..20]])
print(list(G))
[n^2 for n in [1..20] if is_prime(n)]
reduce(lambda x, y: 10*x+y, [1, 2, 3, 4])
reduce(lambda x, y: x + [[y]], [1, 2, 3, 4], [])
L = [2*n+1 for n in [0..9]]
reduce(lambda x, y: x*y, L, 1)
prod([2*n+1 for n in [0..9]]) # a list with for
prod( 2*n+1 for n in [0..9] ) # without a list
prod( n for n in [0..19] if n%2 == 1 )
def fct(x):
return 4/x == 2
all(fct(x) for x in [2, 1, 0])
any(fct(x) for x in [2, 1, 0])
# [fct(x) for x in [2, 1, 0]] # error
[[x, y] for x in [1..2] for y in [6..8]]
[ [[x, y] for x in [1..2]] for y in [6..8]]
G = map(lambda x, y: [x, y], [1..3], [6..8])
print(list(G))
L = [[1, 2, [3]], [4, [5, 6]], [7, [8, [9]]]]
flatten(L, max_level = 1)
flatten(L, max_level = 2)
flatten(L) # equivalent to flatten (L, max_level = 3)
x = var('x')
factor(diff(x*exp(x), [x, x]))
G = map(lambda n: factor(diff(x*exp(x), n*[x])), [0..6])
print(list(G))
print([factor(diff(x*exp(x), n*[x])) for n in [0..6]])
L = [1, 8, 5, 2, 9]
L.reverse()
L
L.sort()
L
L.sort(reverse = True)
L
L = [1, 8, 5, 2, 9]
L.append(100)
L
L = [1, 8, 5, 2, 9]
L.extend([100, 200])
L
L = [1, 8, 5, 2, 9]
L.insert(3, 100)
L
L = [1, 8, 5, 2, 9, 2, 5, 5]
L.count(5)
L = [1, 8, 5, 2, 9, 2, 5, 9, 5]
L.index(9)
L = [1, 8, 5, 2, 9, 2, 5, 9, 5]
L.remove(9)
L
def fct1(L):
E = list(filter(lambda n: n % 2 == 0, L))
O = list(filter(lambda n: n % 2 == 1, L))
return [E, O]
fct1([1..10])
def fct2 (L):
res0 = []
res1 = []
for k in L:
if k%2 == 0:
res0.append(k) # or res0[len(res0):] = [k]
else:
res1.append(k) # or res1[len(res1):] = [k]
return [res0, res1]
fct2([1..10])
def fct3a(L, res0, res1):
if L == []:
return [res0, res1]
elif L[0]%2 == 0:
return fct3a(L[1:], res0+[L[0]], res1)
else:
return fct3a(L[1:], res0, res1+[L[0]])
def fct3(L):
return fct3a(L, [], [])
fct3([1..10])
def subSequences(L):
if L == []:
return []
res = []
start = 0
k = 1
while k < len(L): # 2 consecutive terms are defined
if L[k-1] > L[k]:
res.append(L[start:k])
start = k
k = k+1
res.append(L[start:k])
return res
subSequences([1, 4, 1, 5])
subSequences([4, 1, 5, 1])
S = 'This is a character string.'
S
S = 'This is \n a déjà-vu \t example.'
print(S)
S
S = 'one two three four five six seven'
L = S.split()
L
L1 = [11, 22, 33]
L2 = L1
L1[1] = 222
L2.sort()
L1, L2
L1[2:3] = []
L2[0:0] = [6, 7, 8]
L1, L2
L1 = [11, 22, 33]
L2 = L1
L3 = L1[:]
[L1 is L2, L2 is L1, L1 is L3, L1 == L3]
La = [1, 2, 3]
L1 = [1, La]
L2 = copy(L1)
L1[1][0] = 5 # [1, [5, 2, 3]] for L1 and L2
[L1 == L2, L1 is L2, L1[1] is L2[1]]
list(map(copy, L))
La = [1, 2, 3]
L1 = [1, La]
L2 = deepcopy(L1)
L1[1][0] = 5
[L1 == L2, L1 is L2, L1[1] is L2[1]]
S0 = ()
S1 = (1, )
S2 = (1, 2)
[1 in S1, 1 == (1)]
S1 = (1, 4, 9, 16, 25)
[k for k in S1]
L1 = [0..4]
L2 = [5..9]
list(zip(L1, L2))
list(map(lambda x, y:(x, y), L1, L2))
E = Set([1, 2, 4, 8, 2, 2, 2])
F = Set([7, 5, 3, 1])
E, F
E = Set([1, 2, 4, 8, 2, 2, 2])
F = Set([7, 5, 3, 1])
5 in E, 5 in F, E + F == F | E
E + F, E | F, E & F, E - F, E ^^ F
E = Set([1, 2, 4, 8, 2, 2, 2])
[E[k] for k in [0..len(E)-1]], [t for t in E]
def included(E, F):
return E+F == F
Set([Set([]), Set([1]), Set([2]), Set([1, 2])])
Set([ (), (1, ), (2, ), (1, 2) ])
def Parts(EE):
if EE == Set([]):
return Set([EE])
else:
return withOrWithout(EE[0], Parts(Set(EE[1:])))
def withOrWithout(a, E):
return Set(map (lambda F: Set([a])+F, E)) + E
Parts(Set([1, 2, 3]))
D={}
D['one'] = 1
D['two'] = 2
D['three'] = 3
D['ten'] = 10
D['two'] + D['three']
D = {'a0':'b0', 'a1':'b1', 'a2':'b2', 'a3':'b0', 'a4':'b3', 'a5':'b3'}
E = Set(D.keys()) ; Imf = Set(D.values())
Imf == Set(map (lambda t: D[t], E)) # is equivalent
def injective(D):
return len(D) == len (Set(D.values()))
injective(D)
D = {'a0':'b0', 'a1':'b1', 'a2':'b2', 'a3':'b0', 'a4':'b3', 'a5':'b3'}
F = ['a0', 'a3', 'a4']
G = ['b1', 'b3']
Set([D[t] for t in F]) # image f(F)
Set([t for t in D if D[t] in G]) # preimage f^{-1}(G)
DR = dict((D[t], t) for t in D) # inverse function of f
DR