6 Python Data Types
6.1 Built-in Data Types
- python內建的data type,我喜歡把他分成三大類:
- 基本款
- collections
- binary
6.2 Getting the data type
- 用
type()
,可以獲取此variable的類型
6.3 Setting the specific data type
- 我們只要在value的前面,加上想宣告的資料類型,就可明確的定義出這種type的value
# 基本款
= str("Hello World")
x = int(20)
x = float(20.5)
x = complex(1j)
x = bool(True)
x
# collections
= list(("apple", "banana", "cherry"))
x = tuple(("apple", "banana", "cherry"))
x = range(6)
x = dict(name = "john", age = 36)
x = set(("apple", "banana", "cherry"))
x = frozenset(("apple", "banana", "cherry"))
x
# binary
= bytes(5)
x = bytearray(5)
x = memoryview(bytes(5)) x