Python3的一些基本输入输出

Python输入输出与split()用法
本文通过多个示例详细介绍了Python中基本输入输出操作的实现方式,并重点讲解了如何利用split()函数处理输入数据,包括求和等常见操作。

# python3

# 基本输入输出

#学会使用split()的使用是关键,input()的返回值一定是字符类型

 

.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

1 # Example Input
2 # 1 5
3 # Example Output
4 # 6
5  
6 a = []
7 for x in input().split():
8     a.append(int(x))
9 print(sum(a))

直接一行输出

1 print(sum(int(x) for x in input().split()))

 

.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

 1  # Example Input
 2  # 1 5
 3  # 10 20
 4  # Example Output
 5  # 6
 6  # 30
 7  
 8   while True:
 9       line = input()
10      if line:
11          print(sum(int(x) for x in line.split()))
12      else:
13          break

 

 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

 1  # Example Input
 2  # 2
 3  # 1 5
 4  # 10 20
 5  # Example Output
 6  # 6
 7  # 30
 8  
 9  N = int(input())
10  while(N):
11      print(sum(int(x) for x in input().split()))
12      N -= 1

 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

 1 # Example Input
 2 # 1 5
 3 # 10 20
 4 # 0 0
 5 # Example Output
 6 # 6
 7 # 3
 8 
 9 while True:
10     a = []
11     for x in input().split():
12         a.append(int(x))
13     if a[0] == a[1] == 0: # 以0 0结束
14         break
15     print(sum(a))

 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

 1 # Example Input
 2 # 4 1 2 3 4
 3 # 5 1 2 3 4 5
 4 # 0
 5 # Example Output
 6 # 10
 7 # 15
 8 
 9 while True:
10     a = []
11     line = input()
12     for x in line.split():
13         a.append(int(x))
14     if a[0] == 0:
15         break
16     print(sum(a)-a[0])

 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

 1 # Example Input
 2 # 4
 3 # 1 2 3 4
 4 # 5
 5 # 1 2 3 4 5
 6 # 0
 7 # Example Output
 8 # 10
 9 # 15
10 
11 while(int(input())):
12     print(sum(int(x) for x in input().split()))

 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

 1 # Example Input
 2 # 2
 3 # 4 1 2 3 4
 4 # 5 1 2 3 4 5
 5 # Example Output
 6 # 10
 7 # 15
 8 
 9 N = int(input())
10 while N:
11     a = []
12     line = input()
13     for x in line.split():
14         a.append(int(x))
15     print(sum(a)-a[0])
16     N -= 1


这些基础部分输入输出仅供参考、欢迎交流

 

转载于:https://www.cnblogs.com/cradle-q0518/p/7204809.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值