
제어문
# 파이썬 제어문
# IF 실습
IF구문 실습
관계 연산자 실습
논리 연산자 실습
일반 조건문
다중 조건문
중첩 조건문
# 기본 형식(불린)
print(type(True)) #<class 'bool'> True는 0이 아닌 수,"abc", [1,2,3..], (1,2,3...) ...
print(type(False)) #<class 'bool'> False는 0, "", [], (), {}...
# 예1
if True: #Good #if "a": 이라고 적어도 실행된다.
print("Good")
#들여쓰기(Indent) 파이썬은 들여쓰기를 하지 않으면 에러가 발생한다.
#파이썬의 원칙은 읽기 좋고 똑같은 형식의 코드가 나오도록 하는 것 1원칙이기 때문이다.
if False: #if "": 이라고 적어도 실행하지 않는다.
# 실행 X
print("Bad")
# 예2
if False:
# 여기는 실행되지 않음.
print("Bad")
else:
# 여기가 실행된다.
print("Good") #Good
# 관계연산자 종류
# >, >=, <, <=, ==, !=
x = 15
y = 10
# == 양 변이 같을 때 참.
print(x == y) #False
# != 양 변이 다를 때 참.
print(x != y) #True
# > 왼쪽이 클때 참.
print(x > y) #True
# >= 왼쪽이 크거나 같을 때 참.
print(x >= y) #True
# < 오른쪽이 클 때 참.
print(x < y) #False
# <= 오른쪽이 크거나 같을 때 참.
print(x <= y) #False
# 참 거짓 판별 종류
# 참 : "values", [values], (values), {values}, 1
# 거짓 : "", [], (), {}, 0, None
city = ""
if city:
print("You are in:", city)
else:
# 출력
print("Please enter your city") #Please enter your city
city2 = "Seoul"
if city2:
print("You are in:", city2) #You are in: Seoul
else:
# 출력
print("Please enter your city")
# 논리연산자(중요)
# and, or, not
# 참고 : https://www.tutorialspoint.com/python/python_basic_operators.htm
a = 75
b = 40
c = 10
print('and : ', a > b and b > c) #and : True a > b > c and는 모두만족해야함
print('or : ', a > b or b > c) #or : True or는 하나만 만족하면 됨
print('not : ', not a > b) #not : False not은 반대로 출력
print('not : ', not b > c) #not : False
print(not True) #False
print(not False) #True
# 산술, 관계, 논리 우선순위
# 산술 > 관계 > 논리 순서로 적용
print('e1 : ', 3 + 12 > 7 + 3) #e1 : True
print('e2 : ', 5 + 10 * 3 > 7 + 3 * 20) #e2 : False
print('e3 : ', 5 + 10 > 3 and 7 + 3 == 10) #e3 : True
print('e4 : ', 5 + 10 > 0 and not 7 + 3 == 10) #e4 : False
score1 = 90
score2 = 'A'
# 복수의 조건이 모두 참일 경우에 실행.
if score1 >= 90 and score2 == 'A':
print("Pass") # Pass
else:
print("Fail")
#print들여쓰기를 안하면 오류가 난다 들여쓰기는 탭기로 탭키가 싫으면 4번 띄어쓰기를 하자.
# 예제
id1 = "vip"
id2 = "admin"
grade = 'platinum'
if id1 == "vip" or id2 == "admin":
print("관리자 인증") #관리자 인증
if id2 == "admin" and grade == "platinum":
print("최상위 관리자") #최상위 관리자
# 다중 조건문(elif)
num = 90
if num >= 90:
print('Grade : A') #Grade : A
elif num >= 80:
print('Grade : B')
elif num >= 70:
print('Grade : C')
else:
print('과락')
# 중첩 조건문(if문 안에 if문)
grade = 'A'
total = 95
if grade == 'A':
if total >= 90:
print("장학금 100%") #장학금 100%
elif total >= 80:
print("장학금 80%")
else:
print("장학금 50%")
else:
print("장학금 없음")
# in, not in
q = [10, 20, 30] #리스트
w = {70, 80, 90, 90} #집합
e = {"name": 'Lee', "city": "Seoul", "grade": "A"} #딕셔너리
r = (10, 12, 14) #튜플
print(15 in q) #False
print(90 in w) #True
print(12 not in r) #False
print("name" in e) #True key 검색
print("seoul" in e.values()) #False value 검색
'Python > Today I learned' 카테고리의 다른 글
| TIL#22 제어문 - 반복문(While문) (0) | 2021.05.15 |
|---|---|
| TIL#21 제어문 - 반복문(for문) (0) | 2021.05.15 |
| TIL#19 데이터타입(자료형)-집합(Set)형 (0) | 2021.05.15 |
| TIL#18 데이터타입(자료형)-딕셔너리형 (0) | 2021.05.14 |
| TIL#17 데이터타입(자료형)-튜플형 (0) | 2021.05.14 |