打開Power Shell終端,敲命令:python,輸出結果:
Windows PowerShell
版權所有 (C) Microsoft Corporation。保留所有權利。
PS C:\Users\Administrator> 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.
1.1 編碼聲明
默認情況下,Python 3 源碼文件以 UTF-8 編碼,所有字符串都是 unicode 字符串。 當然你也可以為源碼文件指定不同的編碼:
# -*- coding: cp-1252 -*-
指定UTF-8編碼
# -*- coding: utf-8 -*-
1.2 標識符
- 首字符必須是字母表中字母或下劃線'_'
- 標識符的其他的部分有字母、數(shù)字和下劃線組成
- 標識符對大小寫敏感
1.3 保留字
保留字即關鍵字,我們不能把它們用作任何標識符名稱。Python的標準庫提供了一個keyword module,可以輸出當前版本的所有關鍵字:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try',
'while', 'with', 'yield']
1.4 行與縮進
python最具特色的就是使用縮進來表示代碼塊,不需要使用大括號({})。
縮進的空格數(shù)是可變的,但是同一個代碼塊的語句必須包含相同的縮進空格數(shù)。
1.5 多行語句
Python 通常是一行寫完一條語句,但如果語句很長,我們可以使用反斜杠()來實現(xiàn)多行語句。
# 多行語句
item_one, item_two, item_three = 1, 2, 3;
total = item_one +\
item_two + \
item_three;
在 [], {}, 或 () 中的多行語句,不需要使用反斜杠(),例如:
total = ['item_one', 'item_two', 'item_three',
'item_four', 'item_five']
同一行顯示多條語句,使用分號分割
# 同一行顯示多條語句
import sys; x = 'runoob'; sys.stdout.write(x + '\n')
1.5.1 單行注釋
# 單行注釋
1.5.2 多行注釋
'''
單引號多行注釋
單引號多行注釋
單引號多行注釋
'''
"""
雙引號多行注釋
雙引號多行注釋
雙引號多行注釋
"""