if문 까지 공부
This commit is contained in:
33
10. Style.py
Normal file
33
10. Style.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# https://www.python.org/dev/peps/pep-0008 참고
|
||||
# https://www.python.org/dev/peps/pep-0009/
|
||||
|
||||
# 안좋은 스타일 코드
|
||||
print(6.28*4)
|
||||
print(3.14*4*4)
|
||||
print(6.28*8)
|
||||
|
||||
# 좋은 스타일 코드 {화이트 스페이스(빈칸, 빈줄) 필수}
|
||||
PI = 3.14 # 원주율(파이)
|
||||
radius = 4 # 반지름
|
||||
|
||||
print(2 * PI * 4)
|
||||
print(PI * radius * radius)
|
||||
|
||||
# 개 좋은 스타일 코드
|
||||
|
||||
# 반지름이 r인 원의 둘레 계산
|
||||
def calculate_circumference(r):
|
||||
return 2 * PI * r
|
||||
|
||||
# 반지름이 r인 원의 넓이 계산
|
||||
def calculate_area(r):
|
||||
return PI * r * r
|
||||
|
||||
radius = 4 # 반지름
|
||||
print(calculate_circumference(radius))
|
||||
print(calculate_area(radius))
|
||||
|
||||
radius = 8 # 반지름
|
||||
print(calculate_circumference(radius))
|
||||
print(calculate_area(radius))
|
||||
|
||||
36
11. While.py
Normal file
36
11. While.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# while 반복문
|
||||
|
||||
i = 1
|
||||
while i <= 10:
|
||||
print("왓더 헬")
|
||||
i += 1
|
||||
|
||||
# 1 ~ 100 수 중 짝수만 출력
|
||||
i = 1
|
||||
while i <= 100:
|
||||
if i % 2 == 0:
|
||||
print(i)
|
||||
i += 1
|
||||
|
||||
i = 1
|
||||
while i <= 50:
|
||||
print(i * 2)
|
||||
i += 1
|
||||
|
||||
# 100 이상의 자연수 중 가장 작은 23의 배수 출력
|
||||
i = 100
|
||||
|
||||
while i:
|
||||
if i % 23 == 0:
|
||||
print(i)
|
||||
break
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
i = 100
|
||||
|
||||
while i % 23 != 0:
|
||||
i += 1
|
||||
|
||||
print(i)
|
||||
66
12. If.py
Normal file
66
12. If.py
Normal file
@@ -0,0 +1,66 @@
|
||||
# if문 개념
|
||||
temperature = 15
|
||||
|
||||
if temperature <= 10:
|
||||
print("자켓을 입는다")
|
||||
else:
|
||||
if temperature <= 15:
|
||||
print("긴팔을 입는다.")
|
||||
else:
|
||||
print("반팔을 입는다.")
|
||||
|
||||
# else if문은 elif
|
||||
if temperature <= 10:
|
||||
print("자켓을 입는다")
|
||||
elif temperature <= 15:
|
||||
print("긴팔을 입는다.")
|
||||
else:
|
||||
print("반팔을 입는다.")
|
||||
|
||||
|
||||
def print_grade(midterm_score, final_score):
|
||||
total = midterm_score + final_score
|
||||
if total >= 90:
|
||||
print('A')
|
||||
elif total >= 80:
|
||||
print('B')
|
||||
elif total >= 70:
|
||||
print('C')
|
||||
elif total >= 60:
|
||||
print('D')
|
||||
else:
|
||||
print('F')
|
||||
|
||||
# 코드를 쓰세요.
|
||||
|
||||
|
||||
# 테스트
|
||||
print_grade(40, 45)
|
||||
print_grade(20, 35)
|
||||
print_grade(30, 32)
|
||||
print_grade(50, 45)
|
||||
|
||||
# while문과 if문을 활용하여, 100 이하의 자연수 중 8의 배수이지만 12의 배수는 아닌 것을 모두 출력하세요.
|
||||
#
|
||||
# 예를 들어서 16은 8의 배수이지만 12의 배수가 아니니까 조건에 부합합니다. 하지만 48은 8의 배수이면서 12의 배수이기도 해서 조건에 부합하지 않습니다.
|
||||
#
|
||||
# 실행하면 콘솔에 아래와 같이 출력되어야 합니다.
|
||||
i = 1
|
||||
|
||||
while i <= 100:
|
||||
if i % 8 == 0 and i % 12 != 0:
|
||||
print(i)
|
||||
i += 1
|
||||
|
||||
# 10보다 작은 2 또는 3의 배수는 2, 3, 4, 6, 8, 9이며, 이들의 합은 32입니다.
|
||||
#
|
||||
# while문과 if문을 활용하여, 1,000보다 작은 자연수 중 2 또는 3의 배수의 합을 출력하는 프로그램을 써 보세요.
|
||||
i = 1
|
||||
sum = 0
|
||||
|
||||
while i <= 1000:
|
||||
if i % 2 == 0 or i % 3 == 0:
|
||||
sum += i
|
||||
i += 1
|
||||
|
||||
print(sum)
|
||||
30
2. String.py
30
2. String.py
@@ -1,12 +1,22 @@
|
||||
# 문자열 반복 출력
|
||||
print("hello""\n" * 3)
|
||||
|
||||
# 문자열에 '나 " 표시
|
||||
print("'응답하라 1988'은 많은 시청자들에게 사랑을 받은 드라마예요." + "\n" + '데카르트는 "나는 생각한다. 고로 존재한다."라고 말했다.')
|
||||
print("'응답하라 1988'은 많은 시청자들에게 사랑을 받은 드라마예요." + "\n" + "데카르트는 \"나는 생각한다. 고로 존재한다.\"라고 말했다.")
|
||||
print("영화 '신세계'에서 \"드루와~\"라는 대사가 유행했다.")
|
||||
|
||||
# 문자열 포맷팅
|
||||
year = 2021
|
||||
month = 4
|
||||
day = 2
|
||||
|
||||
print("오늘은 " + str(year) + "년 " + str(month) + "월 " + str(day) + "일입니다.")
|
||||
print("오늘은", str(year), "년", str(month), "월", str(day), "일입니다.")
|
||||
print("오늘은 {}년 {}월 {}일입니다.".format(year, month, day))
|
||||
data = "오늘은 {}년 {}월 {}일입니다."
|
||||
print(data.format(year, month, day))
|
||||
|
||||
# 앞에 0 표시
|
||||
print("오늘은 {}년 {:02d}월 {:02d}일입니다.".format(year, month, day))
|
||||
|
||||
@@ -35,4 +45,22 @@ print("{}시간에 {}{} 벌었습니다.".format(wage, wage * wage, "달러"))
|
||||
print("{}시간에 {}{} 벌었습니다.".format(1, exchange_rate * wage, "원")) # 코드를 채워 넣으세요.
|
||||
|
||||
# "5시간에 28554.0원 벌었습니다." 출력
|
||||
print("{}시간에 {:.01f}{} 벌었습니다.".format(wage, exchange_rate * wage ** 2, "원")) # 코드를 채워 넣으세요.
|
||||
print("{}시간에 {:.01f}{} 벌었습니다.".format(wage, exchange_rate * wage ** 2, "원")) # 코드를 채워 넣으세요.
|
||||
|
||||
# 가장 오래된 방식(% 기호)
|
||||
name = "최지웅"
|
||||
age = 32
|
||||
|
||||
print("제 이름은 %s이고 %d살입니다." % (name, age))
|
||||
|
||||
# 현재 가장 많이 쓰는 방식 (format 메소드)
|
||||
name = "최지웅"
|
||||
age = 32
|
||||
|
||||
print("제 이름은 {}이고 {}살입니다.".format(name, age))
|
||||
|
||||
# 새로운 방식 (f-string)
|
||||
name = "최지웅"
|
||||
age = 32
|
||||
|
||||
print(f"제 이름은 {name}이고 {age}살입니다.")
|
||||
22
3. Type_conversion.py
Normal file
22
3. Type_conversion.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# 형 변환(Type Conversion or Type Casting라 함)
|
||||
|
||||
# 소수(float)를 정수(integer)로 변환
|
||||
print(int(3.8))
|
||||
|
||||
# 정수를 소수로 변환
|
||||
print(float(3))
|
||||
|
||||
# 문자를 정수로 변환
|
||||
print(int("2") + int("5"))
|
||||
|
||||
# 문자를 소수로 변환
|
||||
print(float("1.1") + float("2.5"))
|
||||
|
||||
# 숫자를 문자로 변환
|
||||
print(str(2) + str(5))
|
||||
|
||||
# 문자열에 정수 넣어 출력
|
||||
age = 7
|
||||
print("제 나이는 " + str(age) +"살입니다.")
|
||||
print("제 나이는 {}살입니다.".format(age))
|
||||
|
||||
42
4. Boolean.py
Normal file
42
4. Boolean.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# 불린 (Boolean)
|
||||
print(True)
|
||||
print(False)
|
||||
|
||||
# AND
|
||||
print(True and True)
|
||||
print(True and False)
|
||||
print(False and True)
|
||||
print(False and False)
|
||||
|
||||
# OR
|
||||
print(True or True)
|
||||
print(True or False)
|
||||
print(False or True)
|
||||
print(False or False)
|
||||
|
||||
# Not
|
||||
print(not True)
|
||||
print(not False)
|
||||
|
||||
# 숫자 비교
|
||||
print(2 > 1)
|
||||
print(2 < 1)
|
||||
print(3 >= 2)
|
||||
print(3 <= 3)
|
||||
print(2 == 2)
|
||||
print(2 != 2)
|
||||
|
||||
# 문자 비교
|
||||
print("Hello" == "Hello")
|
||||
print("Hello" != "Hello")
|
||||
|
||||
# 예제
|
||||
print( 2> 1 and "Hello" == "Hello")
|
||||
|
||||
print(not not True) # not False
|
||||
print(not not False) # not True
|
||||
|
||||
print(7 == 7 or (4 < 3 and 12 > 10))
|
||||
|
||||
x = 3
|
||||
print(x > 4 or not(x < 2 or x == 3))
|
||||
13
5. Type_function.py
Normal file
13
5. Type_function.py
Normal file
@@ -0,0 +1,13 @@
|
||||
# type 함수로 자료형 타입 확인
|
||||
print(type(3))
|
||||
print(type(3.0))
|
||||
print(type("3"))
|
||||
|
||||
print(type("True"))
|
||||
print(type(True))
|
||||
|
||||
def hello():
|
||||
print("Hello World!")
|
||||
|
||||
print(type(hello)) # function 함수
|
||||
print(type(print)) # builtin_function_or_method 파이선에 내장된 함수
|
||||
10
6. Optional_parameter.py
Normal file
10
6. Optional_parameter.py
Normal file
@@ -0,0 +1,10 @@
|
||||
# 옵셔널 파리미터 {옵셔널 파라미터는 꼭 마지막에!(참고로 옵셔널 파라미터는 모두 마지막에 있어야 합니다.)}
|
||||
def myself(name, age, nationality="한국"):
|
||||
print("내 이름은 {}".format(name))
|
||||
print("나이는 {}살".format(age))
|
||||
print("국적은 {}".format(nationality))
|
||||
|
||||
|
||||
myself("코드잇", 1, "미국") # 옵셔널 파라미터를 제공하는 경우
|
||||
print()
|
||||
myself("코드잇", 1) # 옵셔널 파라미터를 제공하지 않는 경우
|
||||
24
7. Syntactic_suger.py
Normal file
24
7. Syntactic_suger.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# syntactic sugar (자주 쓰이는 표현을 더 간략하게 쓸 수 있게 해주는 문법을 'syntactic sugar'라고 합니다.)
|
||||
# 다음 두 줄은 같습니다
|
||||
x = x + 1
|
||||
x += 1
|
||||
|
||||
# 다음 두 줄은 같습니다
|
||||
x = x + 2
|
||||
x += 2
|
||||
|
||||
# 다음 두 줄은 같습니다
|
||||
x = x * 2
|
||||
x *= 2
|
||||
|
||||
# 다음 두 줄은 같습니다
|
||||
x = x - 3
|
||||
x -= 3
|
||||
|
||||
# 다음 두 줄은 같습니다
|
||||
x = x / 2
|
||||
x /= 2
|
||||
|
||||
# 다음 두 줄은 같습니다
|
||||
x = x % 7
|
||||
x %= 7
|
||||
52
8. Scope.py
Normal file
52
8. Scope.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# scope(범위)
|
||||
# 변수가 사용 가능한 범위
|
||||
# 로컬 변수 : 변수를 정의한 함수내에서만 사용가능 (파라메터 = 로컬변수-)
|
||||
# 글로벌 변수 : 모든 곳에서 사용 가능
|
||||
# 함수에서 변수를 사용하려면, 로컬 변수를 먼저 찾고 나서 글로벌 변수를 찾음
|
||||
|
||||
# 함수 안에만 x가 정의되었고 밖에는 정의되어 있지 않음
|
||||
def my_function():
|
||||
x = 3
|
||||
print(x)
|
||||
|
||||
my_function()
|
||||
#print(x)
|
||||
|
||||
# 함수가 밖에도 정의되어 있고 함수 안에도 정의되어 있음
|
||||
x = 2
|
||||
|
||||
def my_function():
|
||||
x = 3
|
||||
print(x)
|
||||
|
||||
my_function()
|
||||
print(x)
|
||||
|
||||
# 글로벌 변수에 접근해서 값 변경(비추천)
|
||||
x = 2
|
||||
|
||||
def my_function():
|
||||
global x
|
||||
x = 3
|
||||
print(x)
|
||||
|
||||
my_function()
|
||||
print(x)
|
||||
|
||||
# 글로벌 변수 접근(추천은 리턴으로 하는것)
|
||||
x = 2
|
||||
|
||||
def my_function():
|
||||
global x
|
||||
x = 3
|
||||
print(x)
|
||||
return x
|
||||
|
||||
my_function()
|
||||
print(x)
|
||||
|
||||
# 함수에 파라미터를 넘겨주는 것도 로컬 변수
|
||||
def square(x):
|
||||
return x * x
|
||||
|
||||
print(square(3))
|
||||
18
9. Constants.py
Normal file
18
9. Constants.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# 상수 (constant)
|
||||
# 상수는 변수명을 지을때 모두 대문자로
|
||||
# 상수는 수정하지 않는다는 의지
|
||||
|
||||
PI = 3.14 # 원주율 '파이'
|
||||
|
||||
# 반지름을 받아서 원의 넓이 계산
|
||||
def calculate_area(r):
|
||||
return PI * r * r
|
||||
|
||||
radius = 4 # 반지름
|
||||
print("반지름이 {}면, 넓이는 {}".format(radius, calculate_area(radius)))
|
||||
|
||||
radius = 6 # 반지름
|
||||
print("반지름이 {}면, 넓이는 {}".format(radius, calculate_area(radius)))
|
||||
|
||||
radius = 7 # 반지름
|
||||
print("반지름이 {}면, 넓이는 {}".format(radius, calculate_area(radius)))
|
||||
77
ex) Test.py
77
ex) Test.py
@@ -1,9 +1,9 @@
|
||||
print("goodbye world!")
|
||||
|
||||
# 와우 언빌리버블
|
||||
# 반복문 와우 언빌리버블
|
||||
for i in range(3): print(12)
|
||||
|
||||
# 함수
|
||||
# 함수 (줄바꿈 출력)
|
||||
def hello(name):
|
||||
print("""Hello!
|
||||
Welcome to Codeit""" + name)
|
||||
@@ -29,3 +29,76 @@ b = int(3);
|
||||
|
||||
print(a * b);
|
||||
print(10 * 3);
|
||||
|
||||
def is_evenly_divisible(number):
|
||||
# 코드를 작성하세요
|
||||
TrueFalse = number % 2 == 0
|
||||
return TrueFalse
|
||||
|
||||
# 테스트
|
||||
print(is_evenly_divisible(3))
|
||||
print(is_evenly_divisible(7))
|
||||
print(is_evenly_divisible(8))
|
||||
print(is_evenly_divisible(218))
|
||||
print(is_evenly_divisible(317))
|
||||
|
||||
def calculate_change(payment, cost):
|
||||
# 코드를 작성하세요.
|
||||
change = payment - cost
|
||||
|
||||
fif = change / 50000
|
||||
ten = (change - (50000 * int(fif))) / 10000
|
||||
five = (change - (50000 * int(fif)) - (10000 * int(ten))) / 5000
|
||||
one = (change - (50000 * int(fif)) - (10000 * int(ten)) - (5000 * int(five))) / 1000
|
||||
|
||||
print("50000원 지폐: {}장".format(int(fif)))
|
||||
print("10000원 지폐: {}장".format(int(ten)))
|
||||
print("5000원 지폐: {}장".format(int(five)))
|
||||
print("1000원 지폐: {}장".format(int(one)))
|
||||
|
||||
|
||||
# 테스트
|
||||
calculate_change(100000, 33000)
|
||||
print()
|
||||
calculate_change(500000, 378000)
|
||||
|
||||
|
||||
def calculate_change(payment, cost):
|
||||
# 코드를 작성하세요.
|
||||
change = payment - cost
|
||||
|
||||
fif = change / 50000
|
||||
ten = change % 50000 / 10000
|
||||
five = change % 50000 % 10000 / 5000
|
||||
one = change % 50000 % 10000 % 5000 / 1000
|
||||
|
||||
print("50000원 지폐: {}장".format(int(fif)))
|
||||
print("10000원 지폐: {}장".format(int(ten)))
|
||||
print("5000원 지폐: {}장".format(int(five)))
|
||||
print("1000원 지폐: {}장".format(int(one)))
|
||||
|
||||
|
||||
# 테스트
|
||||
calculate_change(100000, 33000)
|
||||
print()
|
||||
calculate_change(500000, 378000)
|
||||
|
||||
def calculate_change(payment, cost):
|
||||
change = payment - cost # 거스름돈 총액
|
||||
|
||||
fifty_count = change // 50000 # 50,000원 지폐
|
||||
ten_count = (change % 50000) // 10000 # 10,000원 지폐
|
||||
five_count = (change % 10000) // 5000 # 5,000원 지폐
|
||||
one_count = (change % 5000) // 1000 # 1,000원 지폐
|
||||
|
||||
# 답 출력
|
||||
print("50000원 지폐: {}장".format(fifty_count))
|
||||
print("10000원 지폐: {}장".format(ten_count))
|
||||
print("5000원 지폐: {}장".format(five_count))
|
||||
print("1000원 지폐: {}장".format(one_count))
|
||||
|
||||
|
||||
# 테스트
|
||||
calculate_change(100000, 33000)
|
||||
print()
|
||||
calculate_change(500000, 378000)
|
||||
|
||||
Reference in New Issue
Block a user