6 Python Data Types

6.1 Built-in Data Types

  • python內建的data type,我喜歡把他分成三大類:
    • 基本款
    • collections
    • binary

6.1.1 基本款

  • 基本款:
    • Text: str
    • Numeric: int, float, complex
    • Boolean: bool

6.1.2 collections

  • collections(array):
    • Sequence: list, tuple, range
    • mapping: dict
    • set: set, frozenset

6.1.3 binary

  • binary types: bytes, bytearray

6.2 Getting the data type

  • type(),可以獲取此variable的類型

6.3 Setting the specific data type

  • 我們只要在value的前面,加上想宣告的資料類型,就可明確的定義出這種type的value
# 基本款  
x = str("Hello World")
x = int(20)  
x = float(20.5)  
x = complex(1j)
x = bool(True)

# collections  
x = 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"))

# binary
x = bytes(5)
x = bytearray(5)
x = memoryview(bytes(5))