Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Standard Data Types (ชนิดข้อมูลมาตรฐาน) ใน Python 3

ชนิดข้อมูลมาตรฐานของ Python แบ่งเป็น 3 หมวดหลัก:

1. Numeric Types (ชนิดตัวเลข)

  • int - จำนวนเต็ม (ไม่จำกัดขนาดใน Python 3) 10, -10, 0
  • float - จำนวนจริง (มีส่วนทศนิยม) 1.55, 0.005, -3.14
  • complex - จำนวน مختلط (real + imaginary) 2+3j, 5.6-1.5j

2. Sequence Types (ชนิดลำดับ)

  • str - สตริง (ลำดับอักขระ Unicode) 'hello', "world", '''multi-line'''
  • list - ลิสต์ (mutable, ordered) [1, 2, 3, 'a']
  • tuple - ทัพเพิล (immutable, ordered) (1, 2, 3), 1, 2, 3
  • range - ช่วงตัวเลข (lazy sequence) range(10)
  • bytes - ไบต์ immutable b'hello'
  • bytearray - ไบต์ mutable
  • memoryview - มุมมองหน่วยความจำ

3. Mapping Type (ชนิดการแมป)

  • dict - ดิกชันนารี (key-value pairs, unordered*) {'a': 1, 'b': 2}

* Python 3.7+ รักษาลำดับการเพิ่ม (insertion order)

4. Set Types (ชนิดเซต)

  • set - เซต mutable, unique elements {1, 2, 3}
  • frozenset - เซต immutable

5. Boolean Type

  • bool - True, False (subclass ของ int)

6. None Type

  • NoneType - None (ค่าว่าง/ไม่มีค่า)

ตัวอย่างสรุป

10, -10          → int
1.55, 0.005      → float
2+3j, 5.6-1.5j   → complex
'hello', "world" → str
[1, 2, 3, 4]     → list
(1, 2, 3, 4)     → tuple
{'a':1, 'b':2}   → dict
{1, 2, 3}        → set