for loop: iterate over a sequence For Loop: วนตามลำดับสมาชิก
Use for to walk each item of a list / string / range / enumerate, with break and continue
ใช้ for วนทีละสมาชิกของ list / สตริง / range / enumerate พร้อม break และ continue
for variable in sequence: takes each item in turn, puts it in the variable, and runs the indented
block. It fits when you know the sequence to walk (a list, a string, or a number range from range).
for ตัวแปร in ลำดับ: จะดึงสมาชิกทีละตัวมาใส่ตัวแปร แล้วรันบล็อกที่ย่อหน้า
เหมาะเมื่อรู้ลำดับที่จะวน (list, สตริง, หรือช่วงตัวเลขจาก range)
1 for — repeat over items 1 for — ทำซ้ำหลายครั้ง
Walk each item in a list — the variable after for holds one item per round.
วนทีละสมาชิกใน list — ตัวแปรหลัง for รับค่าสมาชิกในแต่ละรอบ
apple banana cherry
2 for with a string 2 for กับสตริง
A string is also a sequence — for walks it one character at a time.
สตริงก็เป็นลำดับได้ — for จะวนทีละตัวอักษร
h i
3 range() — counting the rounds for you 3 range() — นับรอบให้
range() generates the numbers for you, so you do not write a list of numbers yourself.
range() สร้างตัวเลขให้เอง ไม่ต้องเขียน list ของตัวเลขเอง
| Form | Meaningความหมาย |
|---|---|
range(n) | 0 to n-10 ถึง n-1 |
range(start, stop) | start to stop-1start ถึง stop-1 |
range(start, stop, step) | stepping by stepกระโดดทีละ step |
3.1 for with range(n)
range(n) gives numbers from 0 to n-1 (not including n).
range(n) ได้ตัวเลขตั้งแต่ 0 ถึง n-1 (ไม่รวม n)
0 1 2 3 4
3.2 for with range(start, stop, step)
range(start, stop) starts at start and stops before stop. range(start, stop, step)
jumps by step each round, e.g. step=2 gives even numbers.
range(start, stop) เริ่มที่ start ไปจนก่อน stop
range(start, stop, step) กระโดดทีละ step ในแต่ละรอบ เช่น step=2 ได้เลขคู่
0 2 4 6 8
4 enumerate() — get both the position and the value 4 enumerate() — ได้ทั้งตำแหน่งและค่า
Use enumerate() when you want both the position of an item in a list and its
value. The position starts at 0.
ใช้ enumerate() เมื่ออยากรู้ทั้งตำแหน่งของสมาชิกใน list และค่าของมัน
ค่าเริ่มต้นของตำแหน่งคือ 0
0 red 1 green 2 blue
4.1 enumerate starting from 1 4.1 enumerate เริ่มนับจาก 1
Pass a second argument to enumerate(sequence, start) to begin counting from the number you want,
e.g. 1.
ส่งอาร์กิวเมนต์ที่สองให้ enumerate(ลำดับ, start) เพื่อเริ่มนับจากตัวเลขที่ต้องการ เช่น 1
1 red 2 green 3 blue
5 break out of the loop 5 break ออกจากลูป
break stops the loop at once — the remaining rounds are not run.
break หยุดลูปทันที — ไม่ทำรอบที่เหลือ
0 1
6 continue to skip a round 6 continue ข้ามรอบ
continue skips the rest of the current round and jumps straight to the next round.
continue ข้ามส่วนที่เหลือของรอบปัจจุบัน แล้วไปรอบถัดไปทันที
0 2 3
7 Daily sales 7 ยอดขายรายวัน
A for loop is ideal for totals. enumerate(sales, 1) (the second argument is the first number to count from) gives the day number and the amount together; the loop adds each amount to total and remembers the day with the highest sales.
ลูป for เหมาะกับการรวมค่า enumerate(sales, 1) (อาร์กิวเมนต์ตัวที่สองคือเลขที่เริ่มนับ) ให้ทั้งลำดับวันและยอดขายพร้อมกัน ลูปบวกยอดแต่ละวันเข้า total และจำวันที่ขายได้สูงสุด
Total: 440 Best day: 3
8 Common mistakes 8 ข้อผิดพลาดที่พบบ่อย
-
Off-by-one with
range().range(1, 5)stops before 5, so it gives 1, 2, 3, 4. To include 5 writerange(1, 6). -
ผิดไปหนึ่งค่ากับ
range()range(1, 5)หยุดก่อน 5 จึงได้ 1, 2, 3, 4 ถ้าต้องการรวม 5 ให้เขียนrange(1, 6) - Changing a list while looping over it. Adding or removing items of the list you are iterating over skips or repeats items. Loop over a copy or build a new list instead.
- แก้ list ระหว่างที่วนอยู่ การเพิ่มหรือลบสมาชิกของ list ที่กำลังวนจะทำให้ข้ามหรือซ้ำรายการ ให้วนบนสำเนา หรือสร้าง list ใหม่แทน
-
breakleaves only the innermost loop. In the nested loops below it stops the innerjloop, while the outeriloop keeps going. -
breakออกจากลูปในสุดเท่านั้น ในลูปซ้อนด้านล่างbreakหยุดลูปjด้านใน ส่วนลูปiด้านนอกยังวนต่อ
0 0 1 0 2 0
9 Think further 9 แบบฝึกคิดต่อ
Try these yourself in the Run boxes above (or in Google Colab):
ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):
-
Print the 3 times table (
3 x 1 = 3…3 x 10 = 30) withrange(1, 11). -
พิมพ์สูตรคูณแม่ 3 (
3 x 1 = 3…3 x 10 = 30) ด้วยrange(1, 11) -
Count the vowels in
"education"by looping over the string. Hint: test each letter within "aeiou". -
นับสระใน
"education"โดยวนทีละตัวอักษร Hint: ตรวจแต่ละตัวด้วยin "aeiou" -
Add up the even numbers from 1 to 10 in two ways: with
range(2, 11, 2), and withrange(1, 11)plus anif. -
รวมเลขคู่ตั้งแต่ 1 ถึง 10 สองวิธี: ใช้
range(2, 11, 2)และใช้range(1, 11)ร่วมกับif
Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab
References References / แหล่งอ้างอิง
Sources used for this lesson:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
- The for statement — Python Language Reference — for
- range — Built-in Types — range
- enumerate — Built-in Functions — enumerate