# Python變數型別

## 將值分配給變數

`=`運算子左側的運算元是變數的名稱，而`=`運算子右側的運算元是將在儲存在變數中的值。 例如 -

``````#!/usr/bin/python3

counter = 100          # 一個整型數
miles   = 999.99       # 一個浮點數
name    = "Maxsu"       # 一個字串
site_url  = "https://www.tw511.com" # 一個字串

print (counter)
print (miles)
print (name)
print (site_url)
``````

``````100
999.99
Maxsu
https://www.tw511.com
``````

## 多重賦值

Python允許同時為多個變數分配單個值。

``````a = b = c = 1
``````

``````a, b, c = 10, 20, "maxsu"
``````

## 標準資料型別

Python有五種標準資料型別 -

• 1.數位
• 2.字串
• 3.列表
• 4.元組
• 5.字典

### 1.Python數位

``````var1 = 10
var2 = 20
``````

``````del var1[,var2[,var3[....,varN]]]]
``````

``````del var
del var_a, var_b
``````

Python支援三種不同的數值型別 -

• int(有符號整數)
• float(浮點實值)
• complex(複數)

Python3中的所有整數都表示為長整數。 因此，長整數沒有單獨的數位型別。

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

### 2.Python字串

Python中的字串被標識為在引號中表示的連續字元集。Python允許雙引號或雙引號。 可以使用片段運算子(`[]``[:]`)來獲取字串的子集(子字串)，其索引從字串開始處的索引`0`開始，並且以`-1`表示字串中的最後一個字元。

``````#!/usr/bin/python3
#coding=utf-8
# save file: variable_types_str1.py

str = 'tw511.com'

print ('str = ', str)          # Prints complete string
print ('str[0] = ',str[0])       # Prints first character of the string
print ('str[2:5] = ',str[2:5])     # Prints characters starting from 3rd to 5th
print ('str[2:] = ',str[2:])      # Prints string starting from 3rd character
print ('str[-1] = ',str[-1])      # 最後一個字元，結果為：'!'
print ('str * 2 = ',str * 2)      # Prints string two times
print ('str + "TEST" = ',str + "TEST") # Prints concatenated string
``````

``````F:\worksp\python>python variable_types_str1.py
str =  tw511.com
str[0] =  y
str[2:5] =  iba
str[2:] =  ibai.com
str[-1] =  m
str * 2 =  tw511.comtw511.com
str + "TEST" =  tw511.comTEST

F:\worksp\python>
``````

### 2.Python列表

``````#!/usr/bin/python3
#coding=utf-8
# save file: variable_types_str1.py
list = [ 'yes', 'no', 786 , 2.23, 'minsu', 70.2 ]
tinylist = [100, 'maxsu']

print ('list = ', list)          # Prints complete list
print ('list[0] = ',list[0])       # Prints first element of the list
print ('list[1:3] = ',list[1:3])     # Prints elements starting from 2nd till 3rd
print ('list[2:] = ',list[2:])      # Prints elements starting from 3rd element
print ('list[-3:-1] = ',list[-3:-1])
print ('tinylist * 2 = ',tinylist * 2)  # Prints list two times
print ('list + tinylist = ', list + tinylist) # Prints concatenated lists
``````

``````F:\worksp\python>python variable_types_list.py
list =  ['yes', 'no', 786, 2.23, 'minsu', 70.2]
list[0] =  yes
list[1:3] =  ['no', 786]
list[2:] =  [786, 2.23, 'minsu', 70.2]
list[-3:-1] =  [2.23, 'minsu']
tinylist * 2 =  [100, 'maxsu', 100, 'maxsu']
list + tinylist =  ['yes', 'no', 786, 2.23, 'minsu', 70.2, 100, 'maxsu']

F:\worksp\python>
``````

### 3.Python元組

``````#!/usr/bin/python3
#coding=utf-8
# save file : variable_types_tuple.py
tuple = ( 'maxsu', 786 , 2.23, 'yiibai', 70.2  )
tinytuple = (999.0, 'maxsu')

# tuple[1] = 'new item value' 不能這樣賦值

print ('tuple = ', tuple)           # Prints complete tuple
print ('tuple[0] = ', tuple[0])        # Prints first element of the tuple
print ('tuple[1:3] = ', tuple[1:3])      # Prints elements starting from 2nd till 3rd
print ('tuple[-3:-1] = ', tuple[-3:-1])       # 輸出結果是什麼？
print ('tuple[2:] = ', tuple[2:])       # Prints elements starting from 3rd element
print ('tinytuple * 2 = ',tinytuple * 2)   # Prints tuple two times
print ('tuple + tinytuple = ', tuple + tinytuple) # Prints concatenated tuple
``````

``````F:\worksp\python>python variable_types_tuple.py
tuple =  ('maxsu', 786, 2.23, 'yiibai', 70.2)
tuple[0] =  maxsu
tuple[1:3] =  (786, 2.23)
tuple[-3:-1] =  (2.23, 'yiibai')
tuple[2:] =  (2.23, 'yiibai', 70.2)
tinytuple * 2 =  (999.0, 'maxsu', 999.0, 'maxsu')
tuple + tinytuple =  ('maxsu', 786, 2.23, 'yiibai', 70.2, 999.0, 'maxsu')

F:\worksp\python>
``````

``````#!/usr/bin/python3

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
list = [ 'abcd', 786 , 2.23, 'john', 70.2  ]
tuple[2] = 1000    # 無法更新值，程式出錯
list[2] = 1000     # 有效的更新，合法
``````

## Python字典

Python的字典是一種雜湊表型別。它們像Perl中發現的關聯陣列或雜湊一樣工作，由鍵值對組成。字典鍵幾乎可以是任何Python資料型別，但通常為了方便使用數位或字串。另一方面，值可以是任意任意的Python物件。

``````#!/usr/bin/python3
#coding=utf-8
# save file : variable_types_dict.py

dict = {}
dict['one'] = "This is one"
dict[2]     = "This is my"

tinydict = {'name': 'maxsu', 'code' : 1024, 'dept':'IT Dev'}

print ("dict['one'] = ", dict['one'])       # Prints value for 'one' key
print ('dict[2] = ', dict[2])           # Prints value for 2 key
print ('tinydict = ', tinydict)          # Prints complete dictionary
print ('tinydict.keys() = ', tinydict.keys())   # Prints all the keys
print ('tinydict.values() = ', tinydict.values()) # Prints all the values
``````

``````F:\worksp\python>python variable_types_dict.py
dict['one'] =  This is one
dict[2] =  This is my
tinydict =  {'name': 'maxsu', 'code': 1024, 'dept': 'IT Dev'}
tinydict.keys() =  dict_keys(['name', 'code', 'dept'])
tinydict.values() =  dict_values(['maxsu', 1024, 'IT Dev'])
``````

## 資料型別轉換

1 `int(x [,base])` `x`轉換為整數。如果`x`是字串，則要`base`指定基數。
2 `float(x)` `x`轉換為浮點數。
3 `complex(real [,imag])` 建立一個複數。
4 `str(x)` 將物件`x`轉換為字串表示形式。
5 `repr(x)` 將物件`x`轉換為表示式字串。
6 `eval(str)` 評估求值一個字串並返回一個物件。
7 `tuple(s)` `s`轉換為元組。
8 `list(s)` `s`轉換為列表。
9 `set(s)` `s`轉換為集合。
10 `dict(d)` 建立一個字典，`d`必須是`(key，value)`元組的序列
11 `frozenset(s)` `s`轉換為凍結集
12 `chr(x)` 將整數`x`轉換為字元
13 `unichr(x)` 將整數`x`轉換為Unicode字元。
14 `ord(x)` 將單個字元`x`轉換為其整數值。
15 `hex(x)` 將整數`x`轉換為十六進位制字串。
16 `oct(x)` 將整數`x`轉換為八進位制字串。