第一個 Python 程序
在寫代碼之前,請千萬不要用“複製”-“粘貼”把代碼從頁面粘貼到你自己的電腦上。寫程序也講究一個感覺,你需要一個字母一個字母地把代碼自己敲進去,在敲代碼的過程中,初學者經常會敲錯代碼:拼寫不對,大小寫不對,混用中英文標點,混用空格和 Tab 鍵,所以,你需要仔細地檢查、對照,才能以最快的速度掌握如何寫程序。
在 Python 交互式模式下,可以直接輸入代碼,然後執行,並立刻得到結果。
在命令行模式下,可以直接運行 .py 文件。
使用文本編輯器
推薦微軟出品的 Visual Studio Code。
**注意:**不要用 Word 和 Windows 自帶的記事本。Word保存的不是純文本文件,而記事本會自作聰明地在文件開始的地方加上幾個特殊字符(UTF-8 BOM),結果會導致程序運行出現莫名其妙的錯誤。
Python代碼運行助手-Pycharm
輸入 & 輸出
輸入
Microsoft Windows [版本 10.0.17763.1]
(c) 2018 Microsoft Corporation。保留所有權利。
C:\Users\Administrator>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello, world')
hello, world
>>> print('The quick brown fox', 'jumps over', 'the lazy dog')
The quick brown fox jumps over the lazy dog
>>> print(100 + 200)
300
>>> print('100 + 200 =', 100 + 200)
100 + 200 = 300
輸出
>>> name = input()
Michael
>>> name
'Michael'
>>> print('1024 * 768 =',1024 * 768)
1024 * 768 = 786432
>>>