基于變量的數(shù)據(jù)類型,解釋器會分配對應的內存,并決定什么可以存儲在保留內存中。因此,通過為變量分配不同的數(shù)據(jù)類型,可以存儲這些變量在整數(shù),小數(shù)或字符的形式。
Python變量不需要顯式聲明保留內存空間。當賦值給一個變量這些聲明自動發(fā)生。等號(=)是用來賦值給變量。
#!/usr/bin/python3 counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string print (counter) print (miles) print (name)
100 1000.0 John
a = b = c = 1
這里,一個整數(shù)對象使用數(shù)值1來創(chuàng)建,并且所有三個變量都分配到相同的存儲器位置。也可以將多個對象同時分配多個變量。例如 -
a, b, c = 1, 2, "john"
這里,兩個整數(shù)對象使用值1和2分別指定給變量a和b,以及“john”字符串對象值賦給變量c。
存儲在內存中的數(shù)據(jù)可以是多種類型的。例如,一個人的年齡可存儲為數(shù)值,而他或她的地址則可存儲為字母、數(shù)字和字符。Python有用于定義存儲方法操作的各種標準數(shù)據(jù)類型。
字符串
列表
元組
字典
數(shù)字數(shù)據(jù)類型存儲數(shù)值。當分配一個值給創(chuàng)建數(shù)值對象。 例如?
var1 = 1 var2 = 10
您也可以通過使用 del 語句刪除引用的那一個數(shù)字對象。del 語句的語法是 -
del var1[,var2[,var3[....,varN]]]]
del var del var_a, var_b
int (有符號整數(shù))
float (浮點實數(shù)值)
complex (復數(shù))
| int | float | complex |
|---|---|---|
| 10 | 0.0 | 3.14j |
| 100 | 15.20 | 45.j |
| -786 | -21.9 | 9.322e-36j |
| 080 | 32.3+e18 | .876j |
| -0490 | -90. | -.6545+0J |
| -0x260 | -32.54e100 | 3e+26J |
| 0x69 | 70.2-E12 | 4.53e-7j |
復數(shù)由一對有序組成,通過 x + yj 來表示實浮點數(shù), 其中 x 和 y 是實數(shù)并且 j 是虛數(shù)單位。
字符串在 Python 為一組連續(xù)的引號表示的字符。Python允許對單引號或雙引號對。 字符串子集可以用切片操作符 ([ ] and [:] ) :字符串的索引從0開始,并以-1結束。加號(+)號是字符串連接運算符和星號(*)是重復操作符。例如 -
#!/usr/bin/python3 str = 'Hello World!' print (str) # Prints complete string print (str[0]) # Prints first character of the string print (str[2:5]) # Prints characters starting from 3rd to 5th print (str[2:]) # Prints string starting from 3rd character print (str * 2) # Prints string two times print (str + "TEST") # Prints concatenated string
Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST
列表是最通用的Python復合數(shù)據(jù)類型。列表中包含用逗號分隔并使用方括號[]來包含項目。從某種程度上講,列表類似于C語言中的數(shù)組。一個較大的區(qū)別是,所有在一個列表中的項目可以是不同的數(shù)據(jù)類型。
存儲在一個列表中的值可以使用切片操作符([]和[:])進行訪問:列表的0索引位置為起點位置,并在以-1 結束。 加號(+)號是列表中連接運算,星號(*)是重復操作符。例如 -
#!/usr/bin/python3 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print (list) # Prints complete list print (list[0]) # Prints first element of the list print (list[1:3]) # Prints elements starting from 2nd till 3rd print (list[2:]) # Prints elements starting from 3rd element print (tinylist * 2) # Prints list two times print (list + tinylist) # Prints concatenated lists
['abcd', 786, 2.23, 'john', 70.200000000000003] abcd [786, 2.23] [2.23, 'john', 70.200000000000003] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']
元組是另一個序列數(shù)據(jù)類型,它類似于列表。元組中使用單個逗號來分隔每個值。不像列表,元組的值是放列在圓括號中。
列表和元組之間的主要區(qū)別是:列表是包含在方括號[]中,并且它們的元素和大小是可以改變的,而元組元素是括在括號()中,不能進行更新。元組可以被認為是只讀的列表。例如 -
#!/usr/bin/python3 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print (tuple) # Prints complete tuple print (tuple[0]) # Prints first element of the tuple print (tuple[1:3]) # Prints elements starting from 2nd till 3rd print (tuple[2:]) # Prints elements starting from 3rd element print (tinytuple * 2) # Prints tuple two times print (tuple + tinytuple) # Prints concatenated tuple
('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')
#!/usr/bin/python3 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 # Valid syntax with list
Python字典是一種哈希表類型。它們工作的方式就類似在Perl中關聯(lián)數(shù)組或哈希、鍵-值對。字典的鍵可以是幾乎任何Python類型,但通常是數(shù)字或字符串。另一方面,它的值可以是任意Python對象。
#!/usr/bin/python3
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print (dict['one']) # Prints value for 'one' key
print (dict[2]) # Prints value for 2 key
print (tinydict) # Prints complete dictionary
print (tinydict.keys()) # Prints all the keys
print (tinydict.values()) # Prints all the values
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
字典中沒有元素順序(排序)的概念。這是不正確的說,元素是“亂序”; 它們僅僅是無序的。
有時,可能需要執(zhí)行內置類型之間的轉換。要在類型之間轉換,只需使用類型名稱作為函數(shù)。
有幾個內置函數(shù)從一種數(shù)據(jù)類型轉換到另一個。這些函數(shù)返回代表轉換后的值的新對象。
| 函數(shù) | 描述 |
|---|---|
|
int(x [,base]) |
轉換x為整數(shù)。x是字符串則 base 為指定的基數(shù)
|
|
float(x) |
轉換x為一個浮點數(shù)
|
|
complex(real [,imag]) |
創(chuàng)建一個復數(shù)
|
|
str(x) |
轉換對象x為字符串表示
|
|
repr(x) |
轉換對象x為表達式字符串
|
|
eval(str) |
計算一個字符串,并返回一個對象
|
|
tuple(s) |
轉換s為一個元組
|
|
list(s) |
轉換s為一個列表 |
|
set(s) |
轉換s為一個集合 |
|
dict(d) |
創(chuàng)建一個字典。 d必須是(鍵,值)元組序列
|
|
frozenset(s) |
轉換s為冷凍集
|
|
chr(x) |
將一個字符轉換為整數(shù)
|
|
unichr(x) |
Unicode字符轉換為整數(shù)
|
|
ord(x) |
單個字符其轉換為整數(shù)值
|
|
hex(x) |
十六進制字符串轉換為整數(shù)
|
|
oct(x) |
轉換整數(shù)成為八進制字符串 |