算法筆試過程中的幾個輸入輸出python語句



title: python在線筆試學習筆記
localimage: image1
urlname: writenexam
categories:
summary
tags: [writen, exam]
date: 2019-9-17 10:00:00

摘要

本文主要介紹一些算法筆試過程中的幾個輸入輸出python語句的代碼格式

  • [x] Edit By Porter, 積水成淵,蛟龍生焉。

字符串型

單行輸入

import sys
line = sys.stdin.readline().strip()
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
輸出:1-2-3

數值型

輸入數字

n=int(input())
print(n)#輸出為數字

單行輸入輸出為數組

l=list(map(int,input().split(" ")))
print(l)

輸出形式為矩陣

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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM