if else 문

- 들여쓰기 중요

a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")

while문

i = 1
while i < 6:
  print(i)
  i += 1

break문

i = 1
while i < 6:
  print(i)
  if i == 3:
    break #break문으로 루프 종료 가능
  i += 1

continue문

- 재 반복을 중지하고 다음 반복을 계속할 수 있음

i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)

while문 역시 else문 사용가능

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")

for문

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

지정된 횟수만큼 반복하려면 range() 함수를 사용

for x in range(6):
  print(x)

출처

https://www.w3schools.com/python/python_conditions.asp

'Python' 카테고리의 다른 글

함수  (0) 2021.06.17
Dictionary  (0) 2021.06.17
Set  (0) 2021.06.17
Tuple  (0) 2021.06.17
List  (0) 2021.06.17

+ Recent posts