python 기초/[Conditional Statement]

Loop Statement

쵸비 2023. 6. 27. 13:59
728x90

[반복문]

  • 특정 조건이 맞을 때, 계속 반복되는 구조
  • 들여쓰기를 통해, 반복 조건에 대한 수행문의 종속을 표현
  • while문의 기본 구조
i = 0

while i < 5 :
	print("Hi everone")
	i = i + 1

print("반복문 끝")
#Hi everone
#Hi everone
#Hi everone
#Hi everone
#Hi everone
#반복문 끝
  • break문을 이용해 While문을 중단
i = 1
while i > 0:
	print("Hello")
	i = i + 1

	if i > 3 :
		print("탈출")
		break

print("반복문 끝")
#Hello
#Hello
#Hello
#탈출
#반복문 끝
  • For 반복문
i = 1
for i in range(1,10):
	print(i)
#1
#2
#3
#4
#5
#6
#7
#8
#9
  • 여러 가지 자료형을 이용한 For 문
list1 = ['a','b','c','d']
for i in list1:
	print(i)
#a
#b
#c
#d
a = [(1,2),(3,4),(5,6)]
for (n1,n2) in a :
    print(n1+n2)
#3
#7
#11
a = [(1,2),(3,4),(5,6)]
for (n1,n2) in a :
    print(n1+n2)
#3
#7
#11
  • 계속 같은 수행문이 반복되기 때문에, 때에 따라서는 처리속도가 매우 느려질 수 있다.
  • 최대한 For문을 피하여, 간단하게 코딩을 하는 것이 좋다.
728x90

'python 기초 > [Conditional Statement]' 카테고리의 다른 글

조건문  (0) 2023.06.27