--- id: python-for-loops title: "Python For Loops" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["for loop", "range()", "nested loops", "파이썬 for 반복문"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["python", "programming", "w3schools", "for", "loop", "range"] raw_sources: ["https://www.w3schools.com/python/python_for_loops.asp"] applied_in: [] github_commit: "" --- # [[Python For Loops]] ## 🎯 한 줄 통찰 (One-line insight) Python's for loop is an iterator over any sequence (list, tuple, dict, set, string) rather than a counter-based loop like in C-style languages, and `range(6)` yields 0 through 5, not 0 through 6 — the end value is always exclusive. [S1] ## 🧠 핵심 개념 (Core concepts) - **`for` loop** — iterates over a sequence (list, tuple, dictionary, set, string); no indexing variable needed. [S1] - **Iterates any sequence, including strings** — `for x in "banana":` loops over characters. [S1] - **`break`** — exits the loop early. [S1] - **`continue`** — skips to the next iteration. [S1] - **`range()`** — generates a number sequence; default starts at 0, increments by 1, end value EXCLUSIVE (`range(6)` = 0..5). [S1] - **`range(start, stop)`** — custom start. [S1] - **`range(start, stop, step)`** — custom increment. [S1] - **`else`** — runs when the loop finishes normally; skipped if stopped by `break` (same rule as while loops). [S1] - **Nested loops** — inner loop runs fully for each iteration of the outer loop. [S1] - **`pass`** — required placeholder for an otherwise-empty for loop body. [S1] ## 🧩 추출된 패턴 (Extracted patterns) - **break-before-vs-after-print changes output** — placing the `if x == "banana": break` check BEFORE the print vs. AFTER it changes whether "banana" itself gets printed before the loop exits — order within the loop body matters. [S1] ## 📖 세부 내용 (Details) - Basic loop: `fruits = ["apple","banana","cherry"]; for x in fruits: print(x)`. [S1] - range() exclusive end: `for x in range(6): print(x)` → prints 0,1,2,3,4,5. [S1] - range() with start: `for x in range(2, 6):` → 2,3,4,5. [S1] - range() with step: `for x in range(2, 30, 3):`. [S1] - Nested loop: `for x in adj: for y in fruits: print(x, y)`. [S1] - else on natural completion: `for x in range(6): print(x) else: print("Finally finished!")`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) 소스에서 모순되는 정보는 발견되지 않음. ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — 이후 List Comprehension 챕터에서 이 for 순회가 한 줄 표현식으로 압축된다. [S1] ## 💻 코드 패턴 (Code patterns) range() with custom start/stop/step (Python): ```python for x in range(2, 30, 3): print(x) ``` Nested loop over two lists (Python): ```python adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y) ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.90 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[Python Tutorial]] - **관련 개념:** [[Python While Loops]], [[Python Lists Comprehension]], [[Python Range]] - **참조 맥락:** Python 2대 반복문 중 하나 — List Comprehension으로 압축 가능한 순회 패턴. ## 📚 출처 (Sources) - [S1] W3Schools — Python For Loops — https://www.w3schools.com/python/python_for_loops.asp ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "Python For Loops" page (Astra wiki-curation, P-Reinforce v3.1 format).