vec = [2, 4, 6]
print([3*x for x in vec])
print(vec)
vec2 = [[x, x+1] for x in vec]
print(vec2)
vec3 = [3*x for x in vec if x>3]
print(vec3)
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
matrix2 = [[row[i] for row in matrix] for i in range(4)]
print(matrix2)
t =123, 345, 'hello'
print(t[0])
u = t, 1.2, 3.5, 'world'
print(u)
knights = {'gallahad':'the pure', 'robin':'the brave'}
for k, vin knights.items():
print(k, v)
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, ain zip(questions, answers):
print('What is your {0}? It is {1}.'.format(q, a))
输出:
[6, 12, 18]
[2, 4, 6]
[[2, 3], [4, 5], [6, 7]]
[12, 18]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
123
((123, 345, 'hello'), 1.2, 3.5, 'world')
gallahad the pure
robin the brave
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
网友评论