
조건문
IF - 어느 프로그래밍에 다 있는 조건문
If(만약)
조건부 실행을 수행하는데 사용하는 제어문
print(True) # 0이 아닌수
print(False) # 0
if 조건1:
print('조건 1은 참')
elif 조건2:
print('조건 2는 참')
else:
# 위의 모든 조건이 거짓일때 실행되는 코드
print('모든 조건이 거짓이다.')
# if 는 참일 경우 실행한다.
if True:
print('run')
# 거짓일 경우 실행되지 않는다.
if False:
print('run')
# if - else 구문으로 실행해보자
# 반드시 True 값일 경우에만 실행되는것을 볼 수 있다.
if True:
print('run')
else:
print('no run')
# 반대로 해봤는데, else: 구문이 True로 인식되니까 run 이 실행된다.
if False:
print('no run')
else:
print('run')
# 1. == '동등'
if a == 10:
print('a is 10')
#%%
# 2. < '미만'
if a < b:
print("a is less than b") # 출력: "a is less than b"
#%%
# 3. > '초과'
if a > b:
print("a is greater than b") # 이 줄은 출력되지 않음
#%%
# 4. <= '이하'
if a <= 10:
print("a is less than or equal to 10") # 출력: "a is less than or equal to 10"
#%%
# 5. >= '이상'
if a >= 10:
print("a is greater than or equal to 10") # 출력: "a is greater than or equal to 10"
#%%
# 6. != '같지 않음'
if a != b:
print("a is not equal to b") # 출력: "a is not equal to b"
#%%
user = ""
if user:
print('사용자 인증',user)
else:
print('사용자 미 인증',user) # 출력
# 변수 및 조건 값이 혼자 들어가 있으면 그 값은 True 값이 됩니다.
user = 'Nickyou'
if user:
print('나는 닉네임 유저 : ',user)
else:
print('그 무엇도 아님',user)
x = 0
if x > 0:
print('x is positive')
elif x < 0:
print("x is negative")
else:
print("x is zero") # 출력: "x is zero"
fruits = ["apple", "banana", "orange"]
if "apple" in fruits:
print("사과가 리스트에 포함되어 있습니다.")
else:
print("사과가 리스트에 포함되어 있지 않습니다.")
if 'grape' not in fruits:
print('포도는 과일 리스트에 없습니다.')
else:
print('포도는 과일 리스트에 있습니다.')
x = [1,2,3]
y = x
if x is y:
print('x와 y는 동일한 객체입니다.')
else:
print('x와 y는 다른 객체입니다.')
if x is not y:
print('x와 y는 다른 객체입니다.')
else:
print('x와 y는 동일한 객체입니다.')
if True and True:
print("Both are true") # 출력: "Both are true"
if True and False:
print("Won't print this line")
num = int(input("숫자를 입력해 주세요 : "))
if num > 0 and num < 10:
print("0에서 10까지")
else:
print("0에서 10까지가 아님")
if True or False:
print("At least one is true") # 출력: "At least one is true"
if False or False:
print("Won't print this line")
num = int(input("숫자를 입력해 주세요 : "))
if num == 0 or num == 10:
print("0에서 10까지")
else:
print("0에서 10까지가 아님")
if not False:
print('Not False is true') # 출력
if not True:
print('참이면 반환하지 않습니다.')
num = int(input("Enter a number: "))
if not(num > 0 and num < 10):
print("The number is not between 0 and 10.")
else:
print("The number is between 0 and 10.")
a = 5
b = 10
c = 15
# 산술 연산자 우선 순위: 10 * 15 = 150
# 관계 연산자 우선 순위: 150 > 20는 False
# 논리 연산자 우선 순위: not False는 True
result = not a * b > c
print(result) # False
money = int(input("보유하고 있는 돈은 얼마입니까? (단위: 만원) "))
has_laptop = input("노트북을 이미 가지고 있나요? (y/n): ").lower() == 'y'
if money >= 300 and not has_laptop:
print("맥북을 살 수 있습니다.")
elif money >= 200 and not has_laptop:
print("아이패드를 살 수 있습니다.")
elif money >= 100 and not has_laptop:
print("아이폰을 살 수 있습니다.")
elif money >= 20 and not has_laptop:
print("아이팟을 살 수 있습니다.")
else:
print("현금이 부족하여 또는 이미 노트북을 가지고 있어서 살 수 있는 제품이 없습니다.")
money = int(input("보유하고 있는 돈은 얼마입니까? (단위: 만원) "))
has_laptop = input("노트북을 이미 가지고 있나요? (y/n): ").lower() == 'y'
if not has_laptop:
if money >= 300:
print("맥북을 살 수 있습니다.")
elif money >= 200:
print("아이패드를 살 수 있습니다.")
elif money >= 100:
print("아이폰을 살 수 있습니다.")
elif money >= 20:
print("아이팟을 살 수 있습니다.")
else:
print("현금이 부족하여 살 수 있는 제품이 없습니다.")
else:
print("이미 노트북을 가지고 있어서 살 수 있는 제품이 없습니다.")
age = 20
status = "성인" if age >= 20 else "미성년자"
print(f"당신은 {status}입니다.")
# x 가 10인 경우에는 AseertionError
x = 5
if x > 0:
assert x != 10, "x should not be 10"
# x 가 양수일때는 아무런 작업도 수행하지 않습니다.
x = 5
if x > 0:
pass # TODO: