input() 表示接收一个输入。
map(int, 数组) 表示将该数组元素全部变成 int 型。
l = list(map(int, input().split()))
for i in range(len(l)):
print(l[i])
while True 和 try except:break 的组合使用堪称无敌,当接收循环输入的时候,只要有不符合规定的输入出现时,程序就立刻停止,正好符合一般的算法要求。
while True:
try:
a = list(map(int, input().strip().split()))
print(a[0] + a[1])
except:
break
sum(数组) 可以直接对数组元素求和。
while True:
try:
a = list(map(int, input().strip().split()))
print(sum(a))
except:
break
sorted() 方法可以对数值型数组进行排序,也可以对字符串数组排序。
“ ”.join(数组) 可以将字符串数据变成一个字符串,元素之间以空格连接。也可以用其他字符连接。
n = int(input())
a = sorted(list(input().strip().split()))
print(" ".join(a))
a = ["tense", "sad", "happy"]
print(" and ".join(a)) # tense and sad and happy