1)input() Python3.x 中 input() 函數接受一個標准輸入數據,返回為 string 類型.
2)print() 用於打印輸出,常用參數sep、end
3)print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
4)map() 根據提供的函數對指定序列做映射.Return a list or iterator.
list() 用於將元組或字符串轉換為列表.
map(function,iterable,…)會根據提供的函數對指定序列做映射,python2.x返回列表,python3.x返回迭代器.
5)split() 通過指定分隔符對字符串進行切片,如果第二個參數 num 有指定值,則分割為 num+1 個子字符串,返回分割後的字符串列表.
6)str.split(str=“”, num=string.count(str))
7)try:可能拋出異常的語句
8)except:捕獲異常,處理異常
單行:
import sys
line = sys.stdin.readline().strip()
print(line) # 輸出為字符串
# 用input()也一樣
line = input()
print(line) # 輸出為字符串
多行:
import sys
if __name__ == '__main__':
data = []
while True:
line = sys.stdin.readline().strip()
if not line:
break
data.append(line)
print("".join(data))
1
2
3
4
5
12345
num = int(input())
print(num) # 輸出為數字
l = list(map(int,input().split(" ")))
print(l)
輸入:
1 2 3 4
輸出:
[1, 2, 3, 4]
import sys
if __name__ == "__main__":
data = []
while True:
line = sys.stdin.readline().strip()
if not line:
break
tmp = list(map(int,line.split(" ")))
data.append(tmp)
print(data)
1 2 2
1 2 3
1 2 4
[[1, 2, 2], [1, 2, 3], [1, 2, 4]]
import sys
# input()方法:
str1 = input()
print('input 輸入:',str1,'len = ',len(str1))
print('Loop through the input to get each character of the inputASCII碼如下:')
for i in str1:
print(ord(i))
#sys.stdin.readline()方法:
str2 = sys.stdin.readline()
print('sys.stdin.readlien() 輸入:',str2,'len = ',len(str2))
print('Loop through the input to get each character of the inputASCII碼如下:')
for i in str2:
print(ord(i))
print('換行的ASCII碼是',ord('\n'))
abc
input 輸入: abc len = 3
Loop through the input to get each character of the inputASCII碼如下:
97
98
99
abc
sys.stdin.readlien() 輸入: abc
len = 4
Loop through the input to get each character of the inputASCII碼如下:
97
98
99
10
換行的ASCII碼是 10
結論:If the above code wants to fix the input number,只需將input()和sys.stdin.readline()加上int()的限制,It can be seen that the above two inputs,sys.stdin.readline()method to get the last newline character of each line of data.
print('input()輸入:')
list1 = [x for x in input().split()]
print(list1)
print('sys.stdin.readline()輸入:')
import sys
list2 = [x for x in sys.stdin.readline().split()]
print(list2)
input()輸入:
a b c
['a', 'b', 'c']
sys.stdin.readline()輸入:
a b c
['a', 'b', 'c']
print('請輸入數據的行數:')
N = int(input())
print('N=',N)
print('input()輸入:')
list1 = [[x for x in input().split()] for y in range(N)]
print(list1)
print('sys.stdin.readline()輸入:')
import sys
list2 = [[x for x in sys.stdin.readline().split()] for y in range(N)]
print(list2)
請輸入數據的行數:
3
N= 3
input()輸入:
1 2 3
4 5 6
7 8 9
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
sys.stdin.readline()輸入:
1 2 3
4 5 6
7 8 9
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
Do not specify the number of lines of input,But it must end with only spaces or nothing on the last line
import sys
try:
ssn = []
while True:
sn = sys.stdin.readline().strip()
# 若是多輸入,strip()默認是以空格分隔,返回一個包含多個字符串的list
if sn == '':
break
sn = list(sn.split())
ssn.append(sn)
print(ssn)
except:
pass
import sys
try:
ssn = []
while True:
sn = input().strip()
# 若是多輸入,strip()默認是以空格分隔,返回一個包含多個字符串的list
if sn == '':
break
sn = list(sn.split())
ssn.append(sn)
print(ssn)
except:
pass
1 2 3
w e r
ss ss sss
[['1', '2', '3'], ['w', 'e', 'r'], ['ss', 'ss', 'sss']]
1 2 3
w e r
ss ss sss
[['1', '2', '3'], ['w', 'e', 'r'], ['ss', 'ss', 'sss']]
import sys
try:
while True:
sn = input().strip()
#sn = sys.stdin.readline().strip()
# 若是多輸入,strip()默認是以空格分隔,返回一個包含多個字符串的list
if sn == '':
break
sn = list(sn.split())
print('This line is entered as:',sn)
except:
pass
1 2 3
This line is entered as: ['1', '2', '3']
2 2 2
This line is entered as: ['2', '2', '2']
3 4 5
This line is entered as: ['3', '4', '5']
……
m = [i for i in input().split()]
print(m)
輸入:1,2,3,4
輸出:['1,2,3,4']
輸入:1 2 3 4
輸出:['1', '2', '3', '4']
m,n = list(map(int,input().split()))
lines = []
for _ in range(m):
lines.append(list(map(int,input().split(' '))))
#lines.append([int(x) for x in input().split()])
print(lines)
輸入:
2 3
1 1 1
1 1 1
輸出:
[[1, 1, 1], [1, 1, 1]]
代碼1:
# Unknown number of rows
res = []
while True:
try:
s = input()
res.append(list(map(int,s.split(' '))))
#res.append(s.split(' '))
except:
break
代碼2:
import sys
for line in sys.stdin:
#a = line.split()
a = list(map(int,line.split()))
print(a[0]+a[1])
該處使用的url網絡請求的數據.
分享:
Silence gives thoughtful thought,awake consciousness,Peace of mind,Can cultivate a temperament of introspection.不事張揚,踏實做事,This silence is a rapidly growing practice.A person who thinks more and talks less and is more profound,Often the real wise man.