Lists
List เป็น collection ที่ใช้บ่อยที่สุดใน Python สามารถเก็บข้อมูลหลายประเภท แก้ไขได้ และเรียงตามลำดับ
สร้าง List
Section titled “สร้าง List”# สร้างด้วย square bracketsfruits = ['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 listempty = []empty2 = list()
# สร้างจาก stringchars = list('hello')print(f"Chars: {chars}")
# สร้างจาก rangenums = list(range(5))print(f"From range: {nums}")
# List comprehensionsquares = [x**2 for x in range(5)]print(f"Squares: {squares}")เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google