
Compound Statment (복합문)란?
Compound Statement is a statement that uses a colon :
- to sperate a header (head) from its body (=suite라고도 불림),
- which consists of either
- a single simple statement on the same line or
- an indented code block of one or more statements on subsequent lines.
정확히 애기하면 header와 suite를 합쳐서 clauses(절) 라고 부르며,
Compound Statement는 1개 이상의 clauses로 이루어짐.
1개 이상의 clauses로 이루어진 compound statement의 예는 다음 접은 글을 펼쳐서 확인해볼 것:
# 1개의 clause로 이루어진 compound statement
if x > 0: # header
print("양수") # suite
# 3개의 clause로 이루어진 compound statement
if x > 0: # clause 1: if clause
print("양수")
elif x < 0: # clause 2: elif clause
print("음수")
else: # clause 3: else clause
print("영")
Coumpound Statement 는 주로 code block을 body(=suite)로 가지는게 대부분이나,
다음과 같이 한 줄만 필요한 경우엔 single line으로 구성하기도 함.
if x > 0: print("양수") # 한 줄로 if문 작성
물론 이 경우도 indented block 을 사용할 수도 있음:
if x > 0:
print("양수") # code block을 사용한 if문 작성
권장하는 방법은 아니지만, 다음과 같이 여러 statement도 single line으로 표현할 수 있음 (command separator를 이용)
if x < y < z: print(x); print(y); print(z)
- binary operator에 익숙한 다른 프로그래밍 언어 개발자들이 보면 위의 condtional (or chained comparison)이 무지 어색할 수 있음.
- 평범한(?) binary operator 구성된 conditional도 당연히 사용가능함.
compound statement에 대한 official document는 다음과 같음:
https://docs.python.org/ko/3.8/reference/compound_stmts.html
8. 복합문(Compound statements) — Python 3.8.20 문서
8. 복합문(Compound statements) 복합문은 다른 문장들(의 그룹들)을 포함합니다; 어떤 방법으로 그 다른 문장들의 실행에 영향을 주거나 제어합니다. 간단하게 표현할 때, 전체 복합문을 한 줄로 쓸 수
docs.python.org
참고 : code block이란
Python code에서 여러 line을 하나의 block 으로 묶을 수 있음.
- code block은 indent (들여 쓰기) level이 증가할 때 시작됨.
- code block은 다른 code block을 포함할 수 있음 : nested block이 가능.
- code block은 indent가 없거나, 자신을 포함한 code block의 indent level로 감소할 때 끝남.
Compound Statement의 종류
다음과 같은 경우들이 Compound Statement임:
if/elif/else
2023.07.28 - [Python] - [Python] if, elif, else statements
[Python] if, elif, else statements
if, elif, else statements (조건분기문)프로그램의 flow control를 담당함. (loop문과 함께)더보기Flow control을 위한 Control Structures에 대한 내용은 다음 URL을 참고.2025.04.23 - [Python] - [Programming] Control Flow 와 Cont
ds31x.tistory.com
for/else
else는 break checker로 break로 나오지 않은 경우 수행됨:
https://dsaint31.tistory.com/573
[Python] for statement
for statement는 loop를 위한 control structure의 대표격이다.더보기https://ds31x.blogspot.com/2023/07/basic-control-structures-and-control.html Basic : Control Structures and Control FlowControl Structure 프로그램을 구성하는 statement
dsaint31.tistory.com
while/else
for문의 경우처럼 else 는 break checker임.
2023.07.28 - [Python] - [Python] while statement, break and continue
[Python] while statement, break and continue
Python의 경우, loop structure로 while statement와 for statement를 제공한다.Contol Flow와 Control Structure에 대한 개념은 다음 URL을 참고 :http://ds31x.blogspot.com/2023/07/basic-control-structures-and-control.html참고로 do-while st
ds31x.tistory.com
with (context manager)
2024.11.27 - [Python] - [Py] Context Manager: with statement!
[Py] Context Manager: with statement!
1. Python의 Context Manager 개념Python의 Context Manager는 resource(자원, 리소스)를 안전하게 관리하기 위한 도구(특정 메서드를 구현한 객체임). 일반적으로 file(파일), socket(네트워크 소켓), connection(데이
ds31x.tistory.com
def (함수정의)
https://dsaint31.tistory.com/506
[Python] Function Definition, Call and Arguments
이 문서는 function에 대한 간단한 소개를 하고 있다.일부 자세한 내용들은 관련 URL을 추가하는 형태로 확장될 수 있음.Function이란?재사용성과 가독성 을 위해 논리적으로 코드를 나누는(or 그룹짓
dsaint31.tistory.com
class (클래스 정의)
2024.07.24 - [Python] - [Python] Class 간단 정리
[Python] Class 간단 정리
OOP 개념https://dsaint31.me/mkdocs_site/python/oop/oop_0_00_OOP/ BMEObject Oriented Programming (OOP) OOP란? OOP는 Object 에 기반 하여, Object 를 이용 하고 Object 를 만들고(정의 및 구현), Object 를 조합 하여 프로그래밍 하
ds31x.tistory.com
match/case (3.10+)
구조적 패턴 매칭 기능을 지원. C/C++ 의 switch/case 문과 비슷하나 regex등을 지원.
2025.08.11 - [Python] - match statement-Structural Pattern Matching-match/case
match statement-Structural Pattern Matching-match/case
Python match/case Tutorial — 구조적 패턴 매칭1. match/casematch case는 Python 3.10에 도입된 구조적 패턴 매칭(Structural Pattern Matching) 기능임.단순 값 비교뿐 아니라, 자료구조의 모양(구조)을 분해하여 조건
ds31x.tistory.com
Coroutine (3.8+)
서브루틴을 보다 일반화한 것. 비동기 처리와 관련이 깊음.
같이보면 좋은 자료들
https://dsaint31.tistory.com/514
[Python] Expression vs. Statement
Expression (표현식)프로그래밍 또는 컴퓨터 과학 분야에서 Expression은 흔히, function call, identifier, number, operator, literal 등으로 이루어진다.표현식(or 수식) 으로 번역.하나의 value로 reduce 될 수 있는 code
dsaint31.tistory.com
match-case 의 간단한 예: https://dsaint31.tistory.com/510
[Python] Keyword란? (Soft Keyword 포함)
Keywords (or Reserved Words)Keyword란 Python에서 특별한 단어 (special word)들을 가르킨다.Keyword들은 Python에서 특정한 목적으로 사용되도록 이미 정해진 word들로Python에서 정해놓은 방법 외로는 사용을 할 수
dsaint31.tistory.com
'Python' 카테고리의 다른 글
| percent(%) formatting (2) | 2025.07.31 |
|---|---|
| list의 sort() 메서드 와 sorted() 내장 함수 (3) | 2025.07.30 |
| Magic commands-Jupyter NoteBook&IPython (1) | 2025.07.24 |
| Jupyter NoteBook-vscode 확장 중심 (4) | 2025.07.21 |
| ipynb 파일 (IPython NoteBook) (5) | 2025.07.17 |