- Jython 教程
- Jython - 首頁
- Jython - 概述
- Jython - 安裝
- Jython - 匯入 Java 庫
- Jython - 變數和資料型別
- Jython - 使用 Java 集合型別
- Jython - 決策控制
- Jython - 迴圈
- Jython - 函式
- Jython - 模組
- Jython - 包
- Jython - Java 應用程式
- Jython - Eclipse 外掛
- Jython - Eclipse 中的專案
- Jython - NetBeans 外掛和專案
- Jython - Servlet
- Jython - JDBC
- Jython - 使用 Swing GUI 庫
- Jython - 佈局管理
- Jython - 事件處理
- Jython - 選單
- Jython - 對話方塊
- Jython 有用資源
- Jython - 快速指南
- Jython - 有用資源
- Jython - 討論
Jython - 變數和資料型別
變數是計算機記憶體中命名的儲存位置。每個變數可以儲存一個數據。與 Java 不同,Python 是一種動態型別語言。因此,在使用 Jython 時,也不需要事先宣告變數的資料型別。變數的型別不是決定可以儲存哪些資料的因素,而是資料決定了變數的型別。
在下面的示例中,一個變數被賦值一個整數。使用 type() 內建函式,我們可以驗證變數的型別是整數。但是,如果同一個變數被賦值一個字串,type() 函式將字串作為同一變數的型別。
> x = 10 >>> type(x) <class 'int'> >>> x = "hello" >>> type(x) <class 'str'>
這就解釋了為什麼 Python 被稱為動態型別語言。
以下 Python 內建資料型別也可以在 Jython 中使用:
- 數字
- 字串
- 列表
- 元組
- 字典
Python 將數字資料識別為數字,它可以是整數、帶浮點數的小數或複數。字串、列表和元組資料型別稱為序列。
Jython 數字
在 Python 中,任何帶符號的整數都被認為是 'int' 型別。要表示長整數,在其後附加字母 'L'。用小數點分隔整數部分和小數部分的數字稱為 'float'。小數部分可以包含使用 'E' 或 'e' 以科學記數法表示的指數。
複數在 Python 中也被定義為數字資料型別。複數包含一個實部(浮點數)和一個虛部,虛部在其後附加 'j'。
為了用八進位制或十六進位制表示一個數字,在其前面加上 0O 或 0X。下面的程式碼塊給出了 Python 中不同數字表示的示例。
int -> 10, 100, -786, 80 long -> 51924361L, -0112L, 47329487234L float -> 15.2, -21.9, 32.3+e18, -3.25E+101 complex -> 3.14j, 45.j, 3e+26J, 9.322e-36j
Jython 字串
字串是任何用單引號(例如 'hello')、雙引號(例如 "hello")或三引號(例如 '“hello’” 或 “““hello”””)括起來的字元序列。如果字串的內容跨越多行,則三引號特別有用。
轉義序列字元可以在三引號字串中逐字包含。下面的示例顯示了在 Python 中宣告字串的不同方法。
str = ’hello how are you?’ str = ”Hello how are you?” str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up. """
第三個字串列印時,將輸出以下內容。
this is a long string that is made up of several lines and non-printable characters such as TAB ( ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up.
Jython 列表
列表是一種序列資料型別。它是由逗號分隔的項的集合,這些項不一定是相同型別,儲存在方括號中。可以使用基於零的索引訪問列表中的各個專案。
下面的程式碼塊總結了 Python 中列表的使用。
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]
下表描述了一些與 Jython 列表相關的最常見的 Jython 表示式。
| Jython 表示式 | 描述 |
|---|---|
| len(List) | 長度 |
| List[2]=10 | 更新 |
| Del List[1] | 刪除 |
| List.append(20) | 追加 |
| List.insert(1,15) | 插入 |
| List.sort() | 排序 |
Jython 元組
元組是儲存在括號中的逗號分隔的資料項的不可變集合。無法刪除或修改元組中的元素,也無法向元組集合中新增元素。下面的程式碼塊顯示了元組操作。
tup1 = ('physics','chemistry‘,1997,2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]
Jython 字典
Jython 字典類似於 Java 集合框架中的 Map 類。它是鍵值對的集合。用逗號分隔的對括在花括號中。字典物件不遵循基於零的索引來檢索其中的元素,因為它們是透過雜湊技術儲存的。
同一個鍵不能在一個字典物件中出現多次。但是,多個鍵可以具有相同的關聯值。字典物件中可用的不同函式如下所述:
dict = {'011':'New Delhi','022':'Mumbai','033':'Kolkata'}
print "dict[‘011’]: ",dict['011']
print "dict['Age']: ", dict['Age']
下表描述了一些與字典相關的最常見的 Jython 表示式。
| Jython 表示式 | 描述 |
|---|---|
| dict.get(‘011’) | 搜尋 |
| len(dict) | 長度 |
| dict[‘044’] = ‘Chennai’ | 追加 |
| del dict[‘022’] | 刪除 |
| dict.keys() | 鍵列表 |
| dict.values() | 值列表 |
| dict.clear() | 移除所有元素 |