การวนซ้ำด้วย for และ range
ไล่ผ่านตัวเลขและลำดับข้อมูล
การวนซ้ำด้วย for และ range เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Walk Over Items
When you want to visit each value in turn, reach for a for loop. It pulls one item at a time and runs the body for each. 🚶
The for Shape
Write for, a loop variable, the word in, something to walk over, then a colon and an indented block.
for item in things:
print(item)Count with range
To loop a fixed number of times, pair for with range. range(n) yields 0 up to but not including n.
for i in range(3):
print(i)range Stops Before the End
This catches beginners: range(5) gives 0, 1, 2, 3, 4. The end value is excluded, so you get exactly five numbers.
Choose a Start
Give range two values to set a start and end. range(2, 5) walks 2, 3, 4, beginning where you tell it.
for i in range(2, 5):
print(i)Add a Step
A third value sets the step between numbers. range(0, 10, 2) yields the even numbers 0, 2, 4, 6, 8.
for i in range(0, 10, 2):
print(i)Count Downward
A negative step lets range go backward. range(3, 0, -1) produces 3, 2, 1, perfect for a simple countdown.
for i in range(3, 0, -1):
print(i)Loop Over a List
for works on collections too. Iterating a List hands you each element directly, so you rarely need an index.
for name in names:
print(name)Index When You Need It
If you do need positions, loop over range(len(items)) and use the index to reach into the collection.
for i in range(len(items)):
print(items[i])Accumulate Across a for
Just like while, a for loop can build a total. Initialize before the loop, then add inside the body each pass.
var total = 0
for i in range(1, 5):
total += iPrefer for for Known Counts
Use for with range when the number of passes is known up front. It is clearer and harder to break than a manual while.
Quick Check
You write for i in range(5). Which exact sequence of numbers does i take across the loop?
Recap
A for loop visits each item, and range generates numbers with start, end, and step. Remember the end value is excluded. 🎯
คำถามที่พบบ่อย
บทเรียน “การวนซ้ำด้วย for และ range” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การวนซ้ำด้วย for และ range” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การวนซ้ำด้วย for และ range”
ไล่ผ่านตัวเลขและลำดับข้อมูล คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การวนซ้ำด้วย for และ range” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตัดสินใจด้วย if/else
- การวนซ้ำด้วย while
- การวนซ้ำด้วย for และ range
- break, continue และการออกก่อนกำหนด