PhysComp1 Lect10
PhysComp1 Lect10
Computers
1
References
https://docs.python.org/3/library/stdtypes.html#lists
https://docs.python.org/3/library/stdtypes.html#tuples
2
listComprehension.py
squares = []
for x in range(1,11):
squares.append(x**2)
print('squares ',squares)
3
listComprehension.py
squares = []
for x in range(1,11):
squares.append(x**2)
print('squares ',squares)
4
listComprehension.py
squares = []
for x in range(1,11):
if x > 5:
squares.append(x**2)
print('squares ',squares)
5
listComprehension.py
combs = []
for x in [1,2,3]:
for y in [3,1,4]:
if x != y:
combs.append((x, y))
print('combs ',combs)
6
listComprehension.py
inputs = input('Enter numbers\n')
mylist = inputs.split()
print('string list: ', mylist)
7
listComprehension.py
mylist = input('Enter numbers\n').split()
print('string list: ', mylist)
8
listComprehension.py
# in one line
mylist = [int(x) for x in input('Enter numbers\n').split()]
print('number list: ', mylist)
9
quiz.py
# use for-loops for scalar products
# vector triple product use a×(b×c) = (a·c)b - (a·b)c
Enter 3d vector a: 1 2 3
Enter 3d vector b: 1 5 7
Enter 3d vector c: 2 3 1
the a·(b×c) = -11
the a×(b×c) = (-53,−41,45)
10