input: name = input(“請輸入你的名稱:”)
print: ⇒print(value, …, sep=’ ’, end=‘\n’, file=sys.stdout) print(1, 2, 3, sep=”,”, end=“\n\n”) #以,作為分隔,結束時多空2行
print(1, 2, 3, file = open(“data.txt”, “w”)) #將資料寫入data.txt
open回傳_io.TextIOWrapper物件
格式化字串:
text = “%d %.2f %s” % (10, 0.3, “test”)
int(“1”) ⇒形態轉換
open: open(file,mode=“r”,buffering=None,encoding=None, errors=None,newline=None,closefd=True)
如下: file = open(name, ‘r’, encoding=‘UTF-8’)
使用方法: content = file.read() ⇒一次讀入所有檔案 print(content) file.close()
while True: line = file.readline() if not line: break print(line, end=”)
name = input(‘請輸入檔名:’) file = open(name, ‘r’, encoding=‘UTF-8’) for line in file.readlines(): #一次全讀進來陣列之後再跑迴圈,效率比較不好 print(line, end=”) file.close()
name = input(‘請輸入檔名:’) for line in open(name, ‘r’, encoding=‘UTF-8’): #檔案會自動關閉 print(line, end=”)
寫檔方法: name = input(‘請輸入檔名:’) file = open(name, ‘w’, encoding = ‘UTF-8’) file.write(‘test’) file.close()
0b10 ⇒8進位 0x10 ⇒16進位
type(1) ⇒查看型態
oct(10)⇒8進位 hex(10)⇒16進位
int(‘10’, 8) ⇒指定成8進位
10 // 3 =3 ⇒去小數
2 ** 100 ⇒2^100 次方運算
1.0 - 0.8 0.19999999999999996 ⇒使用repr()函式來顯示⇒程式開發人員該了解的結果 print(1.0 - 0.8) 0.2 ⇒使用str()函式來顯示較友善、適於非開發人員觀看的結果
import decimal #用來顯示浮點數精確的結果 a = decimal.Decimal(‘1.0’) b = decimal.Decimal(‘0.8’) a - b Decimal(‘0.2’)
複數的寫法:
a = 3 + 2j b = 5 + 3j c = a + b c (8+5j) type(c) < class ‘complex’