Lists and data abstraction · ลิสต์และ abstraction ของข้อมูล (Lists and data abstraction)
Why a list?
- A program often needs many values at once, like a class of scores.
- One variable per value would be messy.
- A list holds many values under one name — a useful data abstraction.
ทำไมต้องใช้ลิสต์?
- โปรแกรมมักต้องการ หลายค่า พร้อมกัน เช่น คะแนนของนักเรียนทั้งห้อง
- ตัวแปรหนึ่งตัวต่อหนึ่งค่าจะยุ่งเหยิง
- ลิสต์ เก็บหลายค่าภายใต้ชื่อเดียว — เป็น การนามธรรมข้อมูล (data abstraction) ที่มีประโยชน์
What is a data abstraction?
- An abstraction hides detail so you can think about the whole, not the parts.
- A list lets you treat 1 value or 1000 values the same way.
- You write the same code no matter how long the list is.
การนามธรรมข้อมูลคืออะไร?
- การนามธรรม (abstraction) ซ่อนรายละเอียดเพื่อให้คุณคิดเกี่ยวกับภาพรวม ไม่ใช่ส่วนย่อย
- List ช่วยให้คุณจัดการกับ 1 value หรือ 1000 values ได้ในวิธีเดียวกัน
- คุณเขียนโค้ดเดิมไม่สนใจว่าลิสต์ยาวแค่ไหน
Make a list and read it
- Write items inside square brackets:
[10, 20, 30]. - Get one item by its index (its position). Python starts at
0. scores[0]is the first item;scores[-1]is the last item.
สร้างลิสต์และอ่านค่าจากลิสต์
- เขียน items ในวงเล็บเหลี่ยม:
[10, 20, 30]. - ดึงค่าหนึ่งออกโดยใช้ ดัชนี (index) (ตำแหน่ง) Python เริ่มนับที่
0 scores[0]คือรายการแรก;scores[-1]คือรายการสุดท้าย
scores = [10, 20, 30]
print(scores[0])
print(scores[-1])
print(len(scores))
Grow a list with append
scores.append(x)addsxto the end of the list.- The list can grow while the program runs.
len(scores)tells you how many items it now has.
ขยายลิสต์ด้วย append
scores.append(x)เพิ่มxเข้าไปที่ ปลาย ของลิสต์- ลิสต์สามารถขยายขนาดขณะโปรแกรมทำงาน
len(scores)บอกจำนวนรายการที่มีอยู่ในปัจจุบัน
scores = [10, 20]
scores.append(30)
print(scores)
print(len(scores))
Traverse (visit every item)
- To traverse a list means to go through every item, one by one.
for s in scores:gives you one item each pass — this is a for-each loop.- It is the clean way to process the whole list.
Traverse (访问ทุกรายการ)
- การ traverse ลิสต์หมายถึงการผ่านรายการแต่ละรายการทีละรายการ
for s in scores:ให้คุณได้ค่าหนึ่งต่อรอบ — นี่คือ for-each loop- นี่คือวิธีที่สะอาดที่สุดในการประมวลผลทั้งลิสต์
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In AP CSP pseudocode
- A list is the exam's list too — but CB lists are 1-indexed.
- So
aList[1]is the first element in CB, while Python's first isscores[0]. FOR EACH x IN listmatches Python'sfor x in list:.DISPLAY= print,←==.
In AP CSP pseudocode
- List เป็น exam's list ด้วย แต่ CB lists ใช้1-indexed
- ดังนั้น
aList[1]คือ รายการแรก ใน CB ในขณะที่รายการแรกของ Python คือscores[0] FOR EACH x IN listตรงกับ Python'sfor x in list:.DISPLAY= print,←==
aList ← [10, 20, 30]
DISPLAY aList[1] ## the FIRST item (1-indexed!)
total ← 0
FOR EACH x IN aList
{
total ← total + x
}
DISPLAY total
Common mistakes
- Index a list from
0; the last item isa[-1]. - A list groups related values under one name.
ข้อผิดพลาดที่พบบ่อย
- ดึงค่าลิสต์โดยเริ่มที่
0; รายการสุดท้ายคือa[-1] - ลิสต์กลุ่มค่าที่เกี่ยวข้องกันภายใต้ชื่อเดียว
Now you try
- Build, grow, or traverse a list in each task.
- Press Check answer to test your code.
ลองดูเลย
- สร้าง ขยาย หรือ traverse ลิสต์ในแต่ละโจทย์
- กด Check answer เพื่อทดสอบโค้ดของคุณ
A list of items, indexed · ลิสต์ของรายการ, มีการจัดอันดับด้วย index
A list stores many values in order, each at an index from 0. · ลิสต์เก็บหลายค่าเรียงลำดับ แต่ละค่าอยู่ที่ index ตั้งแต่ 0
Start from the list colors = ["red", "green"]. Use .append(...) to add "blue" to the end, so the list becomes ["red", "green", "blue"]. · เริ่มจากรายการ colors = ["red", "green"] ใช้ .append(...) เพื่อเพิ่ม "blue" เข้าไปต่อท้าย รายการจะกลายเป็น ["red", "green", "blue"]
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
nums is [4, 8, 15, 16, 23]. Traverse it with a for loop to add up every value, then print the total. (It should print 66.) · nums คือ [4, 8, 15, 16, 23] Traverse ด้วยลูป for เพื่อรวมทุกค่า แล้ว print ผลรวม (ควรพิมพ์ 66)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Store the first day in first and the last day in last using indexing. · days = ["Mon", "Tue", "Wed", "Thu", "Fri"] เก็บวันที่ แรก ใน first และวันที่ สุดท้าย ใน last โดยใช้ index
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่