>
, >=
, <
, <=
, ==
, !=
and
, or
, not
산술 > 관계 > 논리 순서
If문 예)
score1 = 90
score2 = 'A'
if score1 >= 90 and score2 == 'A':
print('pass')
else:
print('fail')
In, Not In 예)
q = [10, 20, 30]
w = {70, 80, 90}
e = {"name": "lee", "city": "Seoul"}
r = (10, 12, 14)
print(15 in q)
print('name' not in e)
print('Seoul' in e.values())
for in <collection>
1) 기본
for v1 in range(10):
print(v1)
2) 범위지정
for v2 in range(3, 11):
print(v2)
3) 범위 내에 띄어서
for v3 in range(1, 11, 2):
print(v3)
예) 1~1000까지의 합
sum1 = 0
for v in range(1, 1001):
sum1 += v #sum1 = sum1 + v
print(sum1)
예) 구구단
for i in range(2, 10):
for j in range(1, 10):
print('{:4d}'.format(i*j), end='')
print()
1) 34찾기
num = [14, 33, 15, 34, 36, 18]
for n in num:
if n == 34:
print('found')
break
else:
print('not found')
특정 데이터 스킵할 때 = continue
lt = ["1", 2, 5, True, 4, complex(4)]
for v in lt:
if type(v) is bool: # 자료형 대조할 땐 is
continue
print(type(v))
끝까지 찾았지만 없었으면 else 실행
num = [14, 33, 15, 34, 36, 18]
for n in num:
if n == 15:
print('found')
break
else:
print('not found')
조건을 만족하는 동안만 반복
예)
n = 5
while n > 0:
n = n - 1
print(n)
1) break
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('end')
print()
2) contin
m = 5
while m > 0:
m -= 1
if m == 2:
continue
print(m)
print('end')