Python入門之print 函式的部分功能

2021-10-09 20:01:58 字數 1639 閱讀 5047

(1)在控制台輸出數字

print(數字)–其中數字可以是任意型別,包括int,float…

例:print(520)

(2)在控制台輸出字串

print(『字串』)

print(「字串」)–與輸出數字所不同的是,在控制台輸出字串,需要在print()函式裡面加入一對單引號』 ',或者一對雙引號" ",在引號中間加入想要輸出的字串內容

即:print(『string』)或print(「string」)

例:print(『hellopython』)

(3)在控制台輸出運算表示式–(會計算出結果後輸出)

print(運算表示式)–包含多種運算操作,例如;加減乘除,乘方取餘等.

例:print(50+2.1314)

(4)將資料輸出到檔案當中

print(「資訊」,flie=open(『位置』,『a+』))-- a+的作用是:如果不存在該檔案,就建立出來,如果存在該檔案,加在該檔案後面新增資訊

例:fileadress=open(『e:/text.txt』,『a+』)#設定開啟的檔案位址,沒有則建立出來

print(『hellopython』,file=fileadress)#寫入內容,並放在檔案中

fileadress.close()#關閉開啟的檔案

程式執行後控制台顯示資訊:

執行後自動建立的新檔案:

檔案內寫入的內容:

最後讓我們綜合上述功能:

在檔案中寫入表示式 2 ** 3+32/2+4 * 8+10%4 – 5.8686 並計算出結果:

程式體:

fileadress=open('e:/text.txt','a+')

result=2**3+32/2+4*8+10%4-5.8686

question="2**3+32/2+4*8+10%4-5.8686"

print(question,'=',result,file=fileadress)

fileadress.close()

執行結果:

Python之print語句Python的注釋

print語句可以向螢幕上輸出指定的文字。比如輸出 hello,world 用 實現如下 print hello,world print語句也可以跟上多個字串,用逗號 隔開,就可以連成一串輸出 print the quick brown fox jumps over the lazy dog the...

244 emacs lisp 中的print函式

完整的common lisp的學習集合整理如下 繼續學習emacs,繼續學習lisp,繼續學習emacs lisp。這一次看一下emacs lisp中的print函式。看起來,文件以及 得多看,否則的話一些資訊都不知道。在此之前,我的腦子裡就沒有print這個東西。從common lisp開始,倒是...

Python入門(一) print語句和注釋

print語句可以向螢幕上輸出指定的文字。比如輸出 hello,world 用 實現如下 print hello,world print語句也可以跟上多個字串,用逗號 隔開 輸出時逗號變為空格 就可以連成一串輸出 print the quick brown fox jumps over the la...