1 2 3 4 5 6 7 8 | # 輸入在前一小節做的練習 print ( 'Hello world!' ) print ( 1 + 1 ) print ( 9 - 2 ) print ( 2 * 3 ) print ( 6 / 3 ) |
Microsoft Windows [版本 10.0.17134.285] (c) 2018 Microsoft Corporation. 著作權所有,並保留一切權利。 C:\Users\user>d: ← 切換到 D 磁碟 D:\> cd \practice ← 切換到 practice 目錄/資料夾 D:\practice>python hello.py ← 呼叫 python 執行 hello.py 程式, 就會看到執行結果 Hello world! 2 7 6 2.0 |
D:\practice>python ← 進入 python 互動模式 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "help" , "copyright" , "credits" or "license" for more information. >>> help(print) ← 查詢 print 的用法 Help on built -in function print in module builtins: print(...) print(value, ..., sep= ' ' , end= '\n' , file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file -like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. |
1 2 3 4 5 6 7 8 9 10 11 12 | print ( 'I' , 'love' , 'python' ) # 指定 sep -- 要用來分隔各字串的字 print ( 'I' , 'love' , 'python' ,sep = ';' ) # 指定 end -- print 結束方式, 預設是換到下一行(\n), 這裡我們多加了 \n, 所以會多一行空白 print ( 'I' , 'love' , 'python' , sep = ';' , end = '\n\n' ) # 回到預設的 print print ( 'I' , 'love' , 'python' ) |
D:\practice>python first.py I love python ← 預設是一個空白分隔各字串 I;love;python ← 改成以分號分隔各字串 I;love;python ← 不僅以分號分隔各字串, 並且多了一行空白 I love python ← 可以看到這行跟上行間多了一行空白 D:\practice> |
1 2 3 4 5 6 | # 這是註解 # Python 一看到就會略過 # 實際執行的只有一行 print ( 'I' , 'love' , 'python' ) # 註解不只可以在每行一開始, 也可以在程式碼之後, 但要至少間隔一個空白 |
1 2 3 4 5 6 7 8 9 | """ 這也是註解, 稱為多行註解 上面的稱為單行註解, 每行註解都要以 # 開頭 多行註解則是以 3 個 雙引號 開始, 並以 3 個雙引號 結束 只要是在 上、下 各 3 個 雙引號的範圍內的文字, 都會被 Python 視為是註解 """ print ( 'I' , 'love' , 'python' ) |