Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
More on Lists
venList = []
or i in numList:
evenList.append(2*i)
rint("numList:", numList)
rint("evenList:", evenList)
numList: [0, 1, 2, 3, 4, 5, 6, 7]
evenList: [0, 2, 4, 6, 8, 10, 12, 14]
clauses: ['The name is Bond', ' James Bond']
words: ['The', 'name', 'is', 'Bond,', 'James', 2
'Bond']
reply: Good evening Mr Bond
More on Lists
You can also create lists of lists to generate a
matrix.
matrix = [[5,3,5],
[3,4,9],
[2,6,7]]
# a nested list of lists
print("matrix:", matrix)
print("matrix[2]:", matrix[2])
# access 3rd entry of the list
print("matrix[1][0]:", matrix[1][0])
del matrix[1]
print("reduced matrix:", matrix)
matrix.insert(1, [9,9,9])
print("new matrix:", matrix)
Referencing
A = [1, 2, 3, 4, 5]
B = A
print("A:", A)
print("B:", B)
B[2] = 999
B = [9.9, 8.8, 7.7]
C = A
print("A:", A)
print("B:", B)
print("C:", C)
A
B
99
4
3
9
Connection is broken
9.
9
8.
8
A:
B:
A:
5]
B:
C:
5]
7.
7
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 999, 4,
[9.9, 8.8, 7.7]
[1, 2, 999, 4,
Referencing
You can copy the actual values using slice referencing. This is
called a deep copy.
Connection is broken
C
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 999, 4, 5]
[1, 2, 3, 4, 5]
[9.9, 8.8, 7.7]
[1, 2, 3, 4, 5]
9.
9
8.
8
99
3
9
7.
7
Functions
Functions
import math
def circleArea(dia):
Area = math.pi*dia*dia/4
return Area
Function calls
Variable Scope
Functions
def convert_mileh_2_ms (x):
x = x*0.44704
return x
# convert to SI units
crashEnergy = 0.5*mass*speed*speed/1000
# E = 1/2*m*v^2 [kJ]