Language/Python

[Python] 2_제어문

Deveun 2021. 5. 2. 23:47

- if

# if
### 참, 거짓을 판단하는 조건 뒤에 ':'
### 조건에서 수행할 문장은 반드시 들여쓰기
### 조건 : <,>,==, and, or, not...
### 조건 :  a in b, a not in b
if cond == 1:
    print("one")
elif cond == 2:
    print("two")
else:
    print("else")
res = "one" if cond == 1 else "two" # 조건부 표현식 (한줄)

 

- for

# for
for a in arr:
	print(a)			#case1
for (first, last) in tuple_:
	print(first + " " + last)	#case2
for i in range(1,11):
	print(i)			#case3 (range)

 

- while

# while
while cond:
    print("work")
    if cond2:
    	break		# 조건문 끝내기
    if cond3:
        continue	# 조건문 처음으로

 


참고: wikidocs.net/book/1

 

위키독스

온라인 책을 제작 공유하는 플랫폼 서비스

wikidocs.net