참과 거짓(bool)의 결과로 실행문을 결정함
money = True
if money:
print("택시를 타고 가라")
else:
print("걸어가라")
# a < b가 참일때
a = 1
b = 2
if a < b: # True, 출력됨
print("택시를 타고 가라")
else:
print("걸어가라")
# a < b가 거짓일떄
a = 2
b = 1
if a < b: # False, 출력x
print("택시를 타고 가라")
else: # True, 출력됨
print("걸어가라")
# and , or , not 구문
# or( = | )은 둘중 하나만 참이여도 된다
money = True
card = 1
if money or card: # True, 출력됨
print("택시를 타고 가라")
else:
print("걸어가라")
# and( = & )은 둘다 참이여야 된다
money = True
card = 1
if money or card: # True, 출력됨
print("택시를 타고 가라")
else:
print("걸어가라")
# not은 bool값을 바꿈
money = True
card = 1
if not money: # false, 출력안됨
print("택시를 타고 가라")
else: # True, 출력됨
print("걸어가라")
# x in s, not int s
# x(요소) in s(리스트 or 튜플) 안에 있으면 True
money = True
card = 1
if 1 in [1,2,3]: # True, 출력됨
print("택시를 타고 가라")
else:
print("걸어가라")
# not x(요소) in s(리스트 or 튜플) 안에 없으면 True
money = True
card = 1
if not 1 in [1,2,3]: # false, 출력 ㄴ
print("택시를 타고 가라")
else: # True, 출력됨
print("걸어가라")
# 아무것도 하지 않게 설정하고 싶다면
money = True
card = 1
if 1 in [1,2,3]:
pass # True여도 통과됨
else:
print("걸어가라")
# elif(=else if랑 같은말) 다중 조건
pocket = ['paper', 'cellphone']
card = True
if money in pocket: # false
pass
elif card: # True
print("택시를 타고 가라")
else: # 즉, 모두가 false 일 경우에만 실행
print("걸어가라")
# 조건부 표현식
score = 70
# messsage를 success로 만약 score가 60 이상이라면, 아니면 failure
# 1. 성공일때 조건 먼저 써준다
# 2. 조건식을 써준다
message = "success" if score >= 60 else "failure"
"""
아래와 같은 식
if score >= 60:
message = "success"
else:
message = "failure"
"""
print(message)
조건이 참일 경우에 실행문을 반복한다, 거짓일때는 반복하지 않는다.
treeHit = 0
while treeHit < 10:
treeHit = treeHit +1
print("나무를 %d번 찍었습니다." % treeHit)
if treeHit == 10:
print("나무 넘어 갑니다")
# break -> 반복문 탈출
coffee = 10
money = 300
while money:
print("돈을 받았으니 커피를 줍니다.")
coffee = coffee -1
print("나dms 커피의 양은 %d개입니다" %coffee)
if not coffee: # coffe = 0 (=false)에 not = True
print("커피가 다 떨어졌습니다. 판매를 중지합니다")
break #반복문을 탈출함
# continue -> 반복문을 바로 다시 실행함
a = 0
while a< 10:
a = a+1
if a%2 == 0: # 짝수인지
continue # 실행되면 밑에 코드는 해석하지 않고 반복문 반복함
print(a)
test_list = ['one','two','three']
for i in test_list:
print(i) # i는 for문이 돌면서 리스트의 a[0]~끝날때까지 담긴다
# 리스트 + 튜플구조
a = [(1,2),(3,4),(5,6)]
fot (first,last) in a:
print(first)
print(last)
# 종합
marks = [90,25,67,45,80]
number = 0
for mark in marks:
number = number +1
if mark >= 60:
print("%d번 학생은 합격입니다." % number)
else:
print("%d번 학생은 불합격입니다." % number)
# 3항연사자로 한다면
# print("%d번 학생은 합격입니다." % number) if mark >= 60 else print("%d번 학생은 불합격입니다." % number)
# for + continue
marks = [90,25,67,45,80]
number = 0
for mark in marks:
number = number +1
if mark >= 60: continue # 문장이 한줄이라면 붙여서 써도 된다
else:
print("%d번 학생은 불합격입니다." % number)
# for + range함수
sum = 0
for i in range(1,11): # 첫번째 인자 부터, 두번째 인자 미만까지 리스트를 만듬
sum = sum + i
print(sum)
# 리스트 내포
rseult = [num*3 for num in a] #앞에 num은 뒤에 for문을 돌린 결과이다
# == 같은 말
result = []
for num in a:
result.append(num*3)
for i in range(2,10):
for j in range(1,10): # 이 for문이 끝나야 첫번째 for문이 다시 실행됨
print(i*j), end=" ") # print 두번째 인수에, end인자를 넣으면, print가 끝날때 나오는 출력을 정할수있다.(기본값은 엔터임)
print('')#빈 문자열 + 끝날땐 end