Skip to content
เข้าสู่ระบบ

Lists

List เป็น collection ที่ใช้บ่อยที่สุดใน Python สามารถเก็บข้อมูลหลายประเภท แก้ไขได้ และเรียงตามลำดับ

# สร้างด้วย square brackets
fruits = ['apple', 'banana', 'cherry']
numbers = [1, 2, 3, 4, 5]
mixed = [1, 'hello', 3.14, True]
print(f"Fruits: {fruits}")
print(f"Numbers: {numbers}")
print(f"Mixed: {mixed}")
# Empty list
empty = []
empty2 = list()
# สร้างจาก string
chars = list('hello')
print(f"Chars: {chars}")
# สร้างจาก range
nums = list(range(5))
print(f"From range: {nums}")
# List comprehension
squares = [x**2 for x in range(5)]
print(f"Squares: {squares}")

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด