python保留字
- 保留字即關鍵字,我們不能把它們用作任何標識符名稱
- Python 的標準庫提供了一個 keyword 模組,可以輸出目前的版本的所有關鍵字:
>>> 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‘]
多行語句
- Python 通常是一行寫完一條語句,但如果語句很長,我們可以使用反斜線(\)來實現多行語句
total = item_one + item_two + item_three
- 在 [], {}, 或 () 中的多行語句,不需要使用反斜線()
total = [‘item_one‘, ‘item_two‘, ‘item_three‘, ‘item_four‘, ‘item_five‘]
基礎資料型別 (Elementary Data Type)
- Python 中的變數不需要聲明。每個變數在使用前都必須賦值,變數賦值以後該變數才會被建立。
- 在 Python 中,變數就是變數,它沒有類型,我們所說的"類型"是變數所指的記憶體中對象的類型。
- 等號(=)用來給變數賦值。
- 等號(=)運算子左邊是一個變數名,等號(=)運算子右邊是儲存在變數中的值。
counter = 100 # 整型變數miles = 1000.0 # 浮點型變數name = "runoob" # 字串
# 建立一個整型對象,值為 1,從後向前賦值,三個變數都指向同一個記憶體位址。a = b = c = 1# 兩個整型對象 1 和 2 的分配給變數 a 和 b,字串對象 "runoob" 分配給變數 c。a, b, c = 1, 2, "runoob"
- Python3 中有六個標準的資料類型:
- Number(數字)
- String(字串)
- List(列表)
- Tuple(元組)
- Set(集合)
- Dictionary(字典)
- Python3 的六個標準資料類型中:
- 不可變資料(3 個):Number(數字)、String(字串)、Tuple(元組);
- 可變資料(3 個):List(列表)、Dictionary(字典)、Set(集合)。