Python ประเภทข้อมูล
- หน้าก่อนหน้า Python ตัวแปร
- หน้าต่อไป ตัวเลข Python
ประเภทข้อมูลที่มีมาตรฐาน
ในการต่อรอง ประเภทข้อมูลเป็นความคิดสำคัญ:
ตัวแปรสามารถเก็บข้อมูลของประเภทที่ต่างกัน และประเภทต่างกันสามารถทำการต่างกันได้:
ในหมวดหมู่เหล่านี้ Python มีประเภทข้อมูลที่เป็นต้นฉบับดังนี้:
ประเภทข้อมูลแบบข้อความ: | str |
ประเภทข้อมูลแบบเลข: | int , float , complex |
ประเภทข้อมูลแบบลำดับ: | list , tuple , range |
ประเภทข้อมูลแบบแผนฐาน: | dict |
ประเภทข้อมูลแบบชุด: | set , frozenset |
ประเภทข้อมูลแบบบูล: | bool |
ประเภทข้อมูลแบบเบอร์เกณฑ์: | bytes , bytearray , memoryview |
หาประเภทข้อมูล
คุณสามารถใช้ฟังก์ชัน type() ในการหาประเภทข้อมูลของสิ่งใดๆ ได้:
ตัวอย่าง
แสดงประเภทข้อมูลของตัวแปร x:
x = 10 print(type(x))
กำหนดประเภทข้อมูล
ใน Python ในขณะที่คุณกำหนดค่าแก่ตัวแปร จะกำหนดประเภทข้อมูล:
ตัวอย่าง | ประเภทข้อมูล | ลองด้วยตัวเอง |
---|---|---|
x = "Hello World" | str | ลองด้วยตัวเอง |
x = 29 | int | ลองด้วยตัวเอง |
x = 29.5 | float | ลองด้วยตัวเอง |
x = 1j | complex | ลองด้วยตัวเอง |
x = ["apple", "banana", "cherry"] | list | ลองด้วยตัวเอง |
x = ("apple", "banana", "cherry") | tuple | ลองด้วยตัวเอง |
x = range(6) | range | ลองด้วยตัวเอง |
x = {"name" : "Bill", "age" : 63} | dict | ลองด้วยตัวเอง |
x = {"apple", "banana", "cherry"} | set | ลองด้วยตัวเอง |
x = frozenset({"apple", "banana", "cherry"}) | frozenset | ลองด้วยตัวเอง |
x = True | bool | ลองด้วยตัวเอง |
x = b"Hello" | bytes | ลองด้วยตัวเอง |
x = bytearray(5) | bytearray | ลองด้วยตัวเอง |
x = memoryview(bytes(5)) | memoryview | ลองด้วยตัวเอง |
กำหนดประเภทข้อมูลเฉพาะ
ถ้าต้องการกำหนดประเภทข้อมูล คุณสามารถใช้ฟังก์ชันตั้งค่าต่อไปนี้
ตัวอย่าง | ประเภทข้อมูล | ลองด้วยตัวเอง |
---|---|---|
x = str("Hello World") | str | ลองด้วยตัวเอง |
x = int(29) | int | ลองด้วยตัวเอง |
x = float(29.5) | float | ลองด้วยตัวเอง |
x = complex(1j) | complex | ลองด้วยตัวเอง |
x = list(("apple", "banana", "cherry")) | list | ลองด้วยตัวเอง |
x = tuple(("apple", "banana", "cherry")) | tuple | ลองด้วยตัวเอง |
x = range(6) | range | ลองด้วยตัวเอง |
x = dict(name="Bill", age=36) | dict | ลองด้วยตัวเอง |
x = set(("apple", "banana", "cherry")) | set | ลองด้วยตัวเอง |
x = frozenset(("apple", "banana", "cherry")) | frozenset | ลองด้วยตัวเอง |
x = bool(5) | bool | ลองด้วยตัวเอง |
x = bytes(5) | bytes | ลองด้วยตัวเอง |
x = bytearray(5) | bytearray | ลองด้วยตัวเอง |
x = memoryview(bytes(5)) | memoryview | ลองด้วยตัวเอง |
- หน้าก่อนหน้า Python ตัวแปร
- หน้าต่อไป ตัวเลข Python