Python3變數型別

賦值給變數

Python變數不需要顯式宣告保留記憶體空間。當賦值給一個變數這些宣告自動發生。等號(=)是用來賦值給變數。

=運算子的左邊是變數名稱，而 =運算子右側是儲存在變數的值。例如 -
```#!/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```

多重賦值

Python允許同時分配一個值給幾個變數。例如 -
`a = b = c = 1 `

`a, b, c = 1, 2, "john" `

標準資料型別

Python 有五個標準資料型別 -
• 數位
• 字串

• 列表

• 元組

• 字典

Python數位

```var1 = 1
var2 = 10 ```

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

```del var
del var_a, var_b```
Python支援四種不同的數位型別 -
• int (有符號整數)

• float (浮點實數值)

• complex (複數)

範例

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
• 複數由一對有序組成，通過 x + yj 來表示實浮點數， 其中 x 和 y 是實數並且 j 是虛數單位。

Python字串

```#!/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列表

```#!/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']```

Python元組

```#!/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字典

Python字典是一種雜湊表型別。它們工作的方式就類似在Perl中關聯陣列或雜湊、鍵-值對。字典的鍵可以是幾乎任何Python型別，但通常是數位或字串。另一方面，它的值可以是任意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'] ```

資料型別轉換

int(x [,base])

float(x)

complex(real [,imag])

str(x)

repr(x)

eval(str)

tuple(s)

list(s)

set(s)

dict(d)

frozenset(s)

chr(x)

unichr(x)

Unicode字元轉換為整數

ord(x)

hex(x)

oct(x)