
- Python基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境設定
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python控制語句
- Python - 控制流
- Python - 決策
- Python - if語句
- Python - if else
- Python - 巢狀if
- Python - Match-Case語句
- Python - 迴圈
- Python - for迴圈
- Python - for-else迴圈
- Python - while迴圈
- Python - break語句
- Python - continue語句
- Python - pass語句
- Python - 巢狀迴圈
- Python函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS檔案/目錄方法
- Python - OS路徑方法
- 面向物件程式設計
- Python - OOPs概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except塊
- Python - try-finally塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀try塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 加入執行緒
- Python - 為執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python網路程式設計
- Python - 網路程式設計
- Python - 套接字程式設計
- Python - URL處理
- Python - 泛型
- Python庫
- NumPy教程
- Pandas教程
- SciPy教程
- Matplotlib教程
- Django教程
- OpenCV教程
- Python雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI程式設計
- Python - XML處理
- Python - GUI程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件功能
- Python - 工具/實用程式
- Python - GUIs
- Python高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部結構
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - Monkey Patching
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python編譯器
- NumPy編譯器
- Matplotlib編譯器
- SciPy編譯器
Python - 字面量
什麼是Python字面量?
Python字面量或常量是在原始碼中表示固定值的表示法。與變數相反,字面量(123, 4.3, "Hello")是靜態值,或者可以說是在程式或應用程式執行過程中不會改變的常量。例如,在下面的賦值語句中。
x = 10
這裡10是一個數字字面量,表示10,直接儲存在記憶體中。然而,
y = x*2
這裡,即使表示式計算結果為20,它也沒有直接包含在原始碼中。你也可以使用內建的int()函式宣告一個int物件。但是,這也是一種間接的例項化方式,而不是使用字面量。
x = int(10)
不同型別的Python字面量
Python提供了以下字面量,本教程將對它們進行解釋
Python整數字面量
任何只包含數字符號(0到9)的表示法都會建立一個int型別的物件。可以使用賦值運算子透過變數引用宣告的物件。
整數字面量包含三種不同型別的字面值:十進位制、八進位制和十六進位制字面量。
1. 十進位制字面量
十進位制字面量表示帶符號或無符號的數字。使用0到9的數字來建立十進位制字面值。
檢視下面將十進位制字面量賦值給變數的語句:
x = 10 y = -25 z = 0
2. 八進位制字面量
Python允許將整數表示為八進位制數或十六進位制數。在Python中,只有八個數字符號(0到7)但以0o或0O為字首的數字表示是八進位制數。
檢視下面將八進位制字面量賦值給變數的語句:
x = 0O34
3. 十六進位制字面量
類似地,一系列十六進位制符號(0到9和a到f),以0x或0X為字首,表示Python中的十六進位制整數。
檢視下面將十六進位制字面量賦值給變數的語句:
x = 0X1C
但是,需要注意的是,即使你使用八進位制或十六進位制字面量表示法,Python內部仍然將它們視為int型別。
示例
# Using Octal notation x = 0O34 print ("0O34 in octal is", x, type(x)) # Using Hexadecimal notation x = 0X1c print ("0X1c in Hexadecimal is", x, type(x))
執行此程式碼時,將產生以下輸出:
0O34 in octal is 28 <class 'int'> 0X1c in Hexadecimal is 28 <class 'int'>
Python浮點數字面量
浮點數由整數部分和小數部分組成。按照慣例,小數點符號(.)在浮點數的字面量表示中分隔這兩個部分。例如,
浮點數字面量的示例
x = 25.55 y = 0.05 z = -12.2345
對於過大或過小的浮點數,如果小數點前或後的位數過多,則使用科學計數法進行緊湊的字面量表示。E或e符號後跟正整數或負整數,緊跟在整數部分之後。
浮點數科學計數法示例
例如,數字 1.23E05 等同於 123000.00。類似地,1.23e-2 等同於 0.0123
# Using normal floating point notation x = 1.23 print ("1.23 in normal float literal is", x, type(x)) # Using Scientific notation x = 1.23E5 print ("1.23E5 in scientific notation is", x, type(x)) x = 1.23E-2 print ("1.23E-2 in scientific notation is", x, type(x))
在這裡,您將得到以下輸出:
1.23 in normal float literal is 1.23 <class 'float'> 1.23E5 in scientific notation is 123000.0 <class 'float''> 1.23E-2 in scientific notation is 0.0123 <class 'float''>
Python 複數字面量
複數由實部和虛部組成。虛部是任何數字(整數或浮點數)乘以“-1”的平方根。
(√ −1)。在字面表示中,($\sqrt{−1}$) 由 "j" 或 "J" 表示。因此,複數的字面表示形式為 x+yj。
複數型別字面量示例
#Using literal notation of complex number x = 2+3j print ("2+3j complex literal is", x, type(x)) y = 2.5+4.6j print ("2.5+4.6j complex literal is", x, type(x))
此程式碼將產生以下輸出:
2+3j complex literal is (2+3j) <class 'complex'> 2.5+4.6j complex literal is (2+3j) <class 'complex'>
Python 字串字面量
一個字串物件是Python中序列資料型別之一。它是一個不可變的Unicode程式碼點序列。程式碼點是根據Unicode標準對應於字元的數字。字串是Python內建類'str'的物件。
字串字面量透過將一系列字元用單引號 ('hello')、雙引號 ("hello") 或三引號 ('''hello''' 或 """hello""") 括起來來編寫。
字串字面量示例
var1='hello' print ("'hello' in single quotes is:", var1, type(var1)) var2="hello" print ('"hello" in double quotes is:', var1, type(var1)) var3='''hello''' print ("''''hello'''' in triple quotes is:", var1, type(var1)) var4="""hello""" print ('"""hello""" in triple quotes is:', var1, type(var1))
在這裡,您將得到以下輸出:
'hello' in single quotes is: hello <class 'str'> "hello" in double quotes is: hello <class 'str'> ''''hello'''' in triple quotes is: hello <class 'str'> """hello""" in triple quotes is: hello <class 'str'>
包含雙引號的字串字面量示例
如果需要將雙引號作為字串的一部分嵌入,則字串本身應放在單引號中。另一方面,如果要嵌入單引號文字,則字串應放在雙引號中。
var1='Welcome to "Python Tutorial" from TutorialsPoint' print (var1) var2="Welcome to 'Python Tutorial' from TutorialsPoint" print (var2)
它將產生以下輸出:
Welcome to "Python Tutorial" from TutorialsPoint Welcome to 'Python Tutorial' from TutorialsPoint
Python 列表字面量
Python中的列表物件是其他資料型別物件的集合。列表是一個有序的專案集合,這些專案不一定屬於同一型別。集合中的各個物件透過從零開始的索引訪問。
列表物件的字面表示是用一個或多個專案完成的,這些專案用逗號分隔並用方括號 [] 括起來。
列表型別字面量示例
L1=[1,"Ravi",75.50, True] print (L1, type(L1))
它將產生以下輸出:
[1, 'Ravi', 75.5, True] <class 'list'>
Python 元組字面量
Python中的元組物件是其他資料型別物件的集合。元組是一個有序的專案集合,這些專案不一定屬於同一型別。集合中的各個物件透過從零開始的索引訪問。
元組物件的字面表示是用一個或多個專案完成的,這些專案用逗號分隔並用圓括號 () 括起來。
元組型別字面量示例
T1=(1,"Ravi",75.50, True) print (T1, type(T1))
它將產生以下輸出:
[1, 'Ravi', 75.5, True] <class tuple>
無括號的元組型別字面量示例
Python序列的預設分隔符是圓括號,這意味著沒有圓括號的逗號分隔序列也相當於元組的宣告。
T1=1,"Ravi",75.50, True print (T1, type(T1))
在這裡,您也將得到相同的輸出:
[1, 'Ravi', 75.5, True] <class tuple>
Python 字典字面量
與列表或元組一樣,字典也是一種集合資料型別。但是,它不是序列。它是一個無序的專案集合,每個專案都是一個鍵值對。值透過 ":" 符號繫結到鍵。一個或多個用逗號分隔的鍵值對放在花括號內以形成字典物件。
字典型別字面量示例
capitals={"USA":"New York", "France":"Paris", "Japan":"Tokyo", "India":"New Delhi"} numbers={1:"one", 2:"Two", 3:"three",4:"four"} points={"p1":(10,10), "p2":(20,20)} print (capitals, type(capitals)) print (numbers, type(numbers)) print (points, type(points))
鍵應該是不可變物件。數字、字串或元組可以用作鍵。鍵在一個集合中不能出現多次。如果鍵出現多次,則只保留最後一個。值可以是任何資料型別。一個值可以分配給多個鍵。例如,
staff={"Krishna":"Officer", "Rajesh":"Manager", "Ragini":"officer", "Anil":"Clerk", "Kavita":"Manager"}