示例:
1 5
10 20
6
30
while 1:
try:
a,b = map(int, input().split())
print(a+b)
except:
break
map(function,iterable,...)
function – 函数
iterable – 一个或多个序列
str.split(str="", num=string.count(str))
str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表位(\t)等。
num – 分割次数。默认为-1,即分割所有。
import sys
for line in sys.stdin:
if line == '\n':break
a,b = (int(i) for i in line.split())
result = a + b
sys.stdout.write("{}\n".format(result))
import sys
引入python标准库中的sys.py模块sys.stdin
sys.stdin
是一个标准化输入的方法。
使用sys.stdin.readline()
可以实现标准输入,其中默认输入的格式是字符串,如果是int,float类型则需要强制转换。
与input()区别
sys.stdin.readline()
会将标准输入全部获取,包括末尾的‘\n‘,因此用len
计算长度时是把换行符’\n‘计算进去的,而input()获取输入时返回的结果是不包含末尾的换行符’\n‘的。
此外,input()括号内可以直接填写说明文字,例如
n = int(input('Please input a number:\n'))
for <variable> in <sequence>
<statements>
else:
<statements>
由此猜测
sys.stdin
可以理解为一个由字符串组成的列表,每一行的输入是其中的一个元素,即\n
分隔且构成元素??
4. a,b = (int(i) for i in line.split())
赋值语句括号内,声明变量i
是否可以不加外括号?变量i的适用范围?是否仅限于外括号?
猜测:外括号就是用于规定声明局部变量i适用范围
5. sys.stdout.write()
使用print(obj)
在console上打印对象的时候,实质上调用的是sys.stdout.write(obj+'\n')
,print在打印时会自动加个换行符.
sys.stdout.write('hello'+'\n')
print 'hello'
以上两行等价。
6. str.format
>>>print('{0} 和 {1}'.format('python','c++'))
python 和 c++
>>>print('{1} 和 {0}'.format('python','c++'))
c++ 和 python
>>>print('{first}, {second}'.format(first='python',second='c++'))
python, c++
import sys
for a in sys.stdin:
b = a.split()
print(int(b[0])+int(b[1]))
if __name__=="__main__":
while True:
try:
a,b = map(int,input(),split())
print(a + b)
except:
break
if __name__="__main__"
:if __name__="__main__"
之下的代码块将被运行;if __name__="__main__"
之下的代码块不被运行。import sys
s = sys.stdin.readline().strip('\n')
res = []
while s!="":
res.append(s)
s = sys.stdin.readine().strip('\n')
for ch in res:
tmp = ch.split()
ans = int(tmp[0])+int(tmp[1])
print(ans)
strip()
方法strip()
方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。str.strip([chars])
chars – 移除字符串头尾指定的字符序列。
append()
方法append()
方法用于在列表末尾添加新的对象。list.append(obj)
obj – 添加到列表末尾的对象。
import sys
for i in sys.stdin:
print(sum(map(int,i.split())))
示例:
2
1 5
10 20
6
30
for _ in range(int(input())):
print(sum(map(int,input().split())))
import sys
a = int(input())
for i in range(a)
b,c = map(int,sys.stdin.readline().strip().split())
print(b+c)
import sys
for line in sys.stdin:
data = list(map(int,line.strip().split()))
if len(data) == 1:
continue
else:
print(data[0]+data[1])
n = int(input())
for i in range(n):
x = input().split(" ")
print(int(x[0])+int(x[1]))
0 0
则结束输入示例:
1 5
10 20
0 0
6
30
import sys
for line in sys.stdin:
a,b = map(int,line.strip().split())
if (a == 0)&(b == 0):
break
else:
print(a + b)
&
和and
用在数值变量之间时有区别,用在逻辑变量间时基本一致&
,|
表示位运算(位与,位或),and
,or
则依据是否非0来决定输出while 1:
a,b = map(int,input().split())
if a==0 and b==0:
break
print(a+b)
while True
a,b = map(int, input().split())
if a==b==0:
break
else:
print(a+b)
a==b==0
python可以使用这种语法while True:
s = list(map(int,input().split()))
if s[0]==0 and s[1]==0:
break
print(s[0]+s[1])
示例:
4 1 2 3 4
5 1 2 3 4 5
0
10
15
import sys
for line in sys.stdin:
s=list(map(int,line.strip().split()))
if s[0] == 0:
break
print(sum(s)-s[0])
map()
返回的不是列表??而split()
方法调用后是列表??map()
返回值不是列表,split()
方法的返回值是分割后的字符串列表while True:
s=list(map(int,input().split()))
if s[0] == 0:
break
print(sum(s)-s[0])
while True:
try:
line = input().split(" ")
n = int(line[0])
if n == 0:
break
else:
print(sum(map(int,line[1:])))
except:
break
line[1:]
map()
返回的是迭代器(iterators)可以使用sum()
函数求和,但是不能当作列表使用。s = input()
while s!='0':
print(sum(list(map(int,s.split()))[1:]))
s = input()
s!='0'
比较两个对象是否不相等print(sum(list(map(int,s.split()))[1:]))
修改为print(sum(map(int,s.split()[1:])))
使代码更加简短,并略微增加可读性while True:
s=input().split()
if s[0]=='0':break
print(sum(map(int,s[1:])))
while True:
m=input()
if m=='0':
break
l=list(map(int,m.split(' ')))
print(sum(l[1:]))
示例:
2
4 1 2 3 4
5 1 2 3 4 5
10
15
for _ in range(int(input())):
s=list(map(int,input().split()))
print(sum(list[1:]))
for
语句和range()
,次数要用range()
,而不能直接用数字示例:
4 1 2 3 4
5 1 2 3 4 5
10
15
略
示例:
1 2 3
4 5
0 0 0 0 0
6
9
0
略
示例:
5
c d a bb e
a bb c d e
while True:
try:
n=int(input())
s=input().split()
print(' '.join(sorted(s)))
except:
break
删除字符串中重复的字母数字,并按原来的顺序进行输出。
while True:
try:
s=input()
ss=list(set(s))
ss.sort(key=s.index)
print("".join(ss))
except:
break
sort()
中的key对每一个元素调用函数,按照函数返回值进行排序。join()