Base/Python

[Python] 파이썬 기초 7 - 조건문(IF, elif ,else)에 대한 정의와 기본적인 함수 사용법

반응형

# 파이썬 제어문


if : if에 해당 조건에 맞을 경우 안에 로직을 실행한다.

if ~ else : if 조건이 맞을 경우 if를 실행하고 조건이 맞지 않을 경우에 else의 로직을 실행한다.

if ~ elif ~ else : if 조건이 맞을경우 if를 실행하고 조건이 맞지 않을 경우 elif를 실행하고 elif 조건이 맞지 않을 경우에 else의 로직을 실행한다.

bool True : 0이 아닌수 , 문자 , 데이터가 있는 리스트, 튜플, 딕셔너리
※ if 는 조건의 값이 True 일 때만 실행된다.

 

#조건문

if True:
	print("True") # True

if False:
	print("Bad") # Bad

if not False:
	print("Bad")

if False:
	print("Bad")
else:
	print("Good") # Good




# 아래의 값이 3의 배수인지 5의 배수인지
# 3의 배수도 5의 배수도 아닌지를 검정하고 싶다면?

number = 15

# 일반적인 if / elif /else 구문
if number % 3 == 0 :
	print("{}은 3의 배수 입니다.".format(number))
elif number % 5 == 0 :
	print("{}은 5의 배수 입니다.".format(number))
else:
	print("{}은 3과5의 배수가 아닙니다.".format(number))



# 15은 3의 배수입니다.

# 일반적인 if / elif /else + 논리연산자 구문
if number % 3 == 0 & number % 5 == 0: # and 구문이다.
	print("{}은 3과 5의 배수 입니다.".format(number))
else:
	print("{}은 3과5의 배수가 아닙니다.".format(number))




# 윤년의 조건
# 4의 배수이고 100의 배수가 아니거나
# 400의 배수일 때를 윤년으로 본다.
# if 구문을 사용하여 연도와 열을 입력받아서 월의 마지막 일을 출력하라

#year = int(input("연도 : ")) # 입력
#month = int(input("월 : ")) # 입력
year =2020
month = 2

if year % 4 == 0 and year % 100 != 0:
	if month in (1, 3, 5, 7, 8, 10, 12):
		print(year, "년 ", month, "월 ", "31일")
	elif month == 2:
		print(year, "년 ", month, "월 ", "29일")
	else:
		print(year, "년 ", month, "월 ", "30일")
elif year % 400 == 0:
	if month in (1, 3, 5, 7, 8, 10, 12):
		print(year, "년 ", month, "월 ", "31일")
	elif month == 2:
		print(year, "년 ", month, "월 ", "29일")
	else:
		print(year, "년 ", month, "월 ", "30일")
else:
	if month in (1, 3, 5, 7, 8, 10, 12):
		print(year, "년 ", month, "월 ", "31일")
	elif month == 2:
		print(year, "년 ", month, "월 ", "28일")
	else:
		print(year, "년 ", month, "월 ", "30일")

 

leap_year_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

if year % 4 == 0 and year % 100 != 0:
	print("{}년 {}월 마지막은 {} 입니다.".format(year, month, leap_year_month[month-1]))
elif year % 400 == 0:
	print("{}년 {}월 마지막은 {} 입니다.".format(year, month, leap_year_month[month - 1]))
else:
	print("{}년 {}월 마지막은 {} 입니다.".format(year, month, year_month[month - 1]))


상식


# show short cut & , | 같은 거 걸 때 주의해야 될 부분
# true가 있고 true가 있을 때
# | 와 or의 차이 - | 일 때 앞에서만 체크한다. or 해야 둘 다 체크한다.




# 중첩 조건문

A 학점이면서 90 이상의 점수면 장학금 100%
A 학점이면서 90 이상의 점수면 장학금 90%
A 학점이 아니면 장학금 50%

# grade = input("학점 입력 : ")
# total = int(input("점수 입력 : "))
grade = 'A'
total = 95
if grade == 'A':
	if total >= 95:
		print("장학금 100%") # 장학금 100%
	else:
		print("장학금 90%")
else:
	print("장학금 50%")

 

해당 지역이 있는지 확인하는 문제

area = ["서울", "부산", "제주"]
region = "수원"

if region in area:
	print(area)
else:
	print("{} 지역은 포함되지 않습니다".format(region)) # 수원 지역은 포함되지 않습니다

myDict = {"서울": 100, "광주": 200}
region = "서울"

if region in myDict:
	print("키값이 dict안에 있습니다.") # 키값이 dict안에 있습니다.
else:
	print("키값이 dict안에 없습니다.") # 키값이 dict안에 없습니다.


num = 9
result = 0
if num >= 5:
	result = num * 2
else:
	result = num + 2
	print(result)





# 삼항 연산자

( if 일 때 결과 / if 문 / else 문 ) - 이건 if else 일 때만 가능하다.

result = num*2 if(num >= 5) else num + 2



# 참, 거짓 판별 종류

 

city = " " # 공백도 문자열로 취급한다.
if city:
	print(city)
else :
	print("Please enter your city")

money = 1

if money :
	print("맛점하세요~")
else:
	print("더 맛저마세요~")




# 사용자로부터 하나의 값을 입력받아(input)
# 해당 값에 20을 뺀 값을 출력하라
# 단) 출력 값의 범위는 0 ~ 225이다.
# 예를 들어 결괏값이 0 보다 작은 값이 되는 경우 0을 출력하고
# 255 보다 큰 값이 되는 경우 255를 출력해야 한다.

value = int(input("값을 입력하세요 : "))
if value-20 >= 225:
	print(225)
elif value-20 < 0:
	print(0)
else:
	print(value-20)
반응형