i = 1

j = 1


while i <= 9:

    j = 1

    while j <= 9:

        sol = i * j

        print("%d * %d = %d" % (i, j, sol))

        j = j + 1

    i = i + 1

def fibonacci(number):

    previous = 0

    current = 1

    i = 1

    

    while i <= number:

        print(current)

        temp = previous                 #temp는 임시저장 공간

        previous = current

        current = current + temp

        i = i + 1 #i += i

        

fibonacci(20)        

        

x의 n승을 구하고 싶으면

pow(x, n)

def calculate(number):

i = 1

count = 0

while i <= number:

if number % i == 0:

print(i)

count = count + 1

i = i + 1

print("%d의 약수는 총 %d개입니다" % (number, count))

#print(str(number) + "의 약수는 총 " + str(count) + "개입니다.")


calculate(120)

calculate(240)


def calculate():

i = 1

sum = 0

while i < 1000:

if i % 2 == 0 or i % 3 == 0:

sum = sum + i

i = i + 1

return sum


print(calculate())



100이하의 8의 배수이지만 12의 배수는 아닌 숫자를 출력하라



i = 1

while i <= 100:

if i % 8 == 0 and i % 12 != 0:

print(i)

i = i + 1




def notify_grade(midterm, finalterm): #함수 정의

    score = midterm + finalterm # 점수는 중간 + 기말

    if 90 <= score:

        print("A학점입니다.") # 90점 이상 A학점

    elif 80 <= score:

        print("B학점입니다.") # 80점 이상 90점 미만 B학점

    elif 70 <= score:

        print("C학점입니다.") # 70점 이상 80점 미만 C학점

    elif 60 <= score:

        print("D학점입니다.") # 60점 이상 70점 미만 D학점

    else:

        print("낙제입니다.")   # 60점 미만 낙제


notify_grade(100,20) # A학점

notify_grade(40,23)  # D학점

notify_grade(80,80)  # A학점

1. if

a = 10

b = 84

if a < b:
print("b가 a보다 크다")

2. else 

a = 10

b = 84

if a < b:
print("b가 a보다 크다")
else:
print("a가 b보다 크다")

3. elif

a = 10

b = 84

if a < b:
print("b가 a보다 크다")
elif a == b:
print("a랑 b랑 같다")
else:
print("a가 b보다 크다")


i = 100 # 특정값

while i % 27 != 0: #i를 27로 나눴을 때 나머지가 0이 될 때까지 = 27의 배수일 때까지

i = i + 1

print(i)


#특정 값을 초과하는 배수기 때문에 처음부터 while 반복 문 안에 i = 특정값을 설정해주고, n의 배수표현을 i % n 으로 해주는 것

i = 1

while i <= 50:

print(i * 2)

i = i + 1


def even_or_odd(number):

return number & 2 == 0


print(even_or_odd(3)) #결과값 False

print(even_or_odd(4)) #결과값 true

코드

설명

%s

문자열(String)

%c

문자 1개(character)

%d

정수(Integer)

%f

부동 소수(floating-point)

%o

8진수

%x

16진수

%%

Literal % (문자 '%'그 자체)


%s 는 문자열

%d 는 정수

%f 는 실수



PI = 3.14


def calculate_area(r):

retrun PI * r * r


radius = 6 # 반지름

print("반지름이 %.3f면 원 넓이는 %.3f" % (radius, calculate_area(radius)))


radius = 12 # 반지름

print("반지름이 %.3f면 원 넓이는 %.3f" % (radius, calculate_area(radius))) 



#%.3f = 실수 3자리까지 표시한다는 의미


#돈을 냈을 때, 5만원, 1만원, 5천원, 1천원의 지폐의 합이 최소가 되도록 거스름돈을 알려주는 예제

#payment=내가 내는 돈, cost=상품의 가격


#함수 정의

def calculate_change(payment, cost):

change = payment - cost

fifty_thousand = int(change / 50000)


change = change % 50000

ten_thousand = int(change / 10000)


change = change % 10000

five_thousand = int(change / 5000)


change = change % 5000

one_thousand = int(change / 1000)


print("거스름돈은 다음과 같습니다")

print() #공백

print("50000원짜리 지폐 : " + str(fifty_thousand) + "장")

print("10000원짜리 지폐 : " + str(ten_thousand) + "장")

print("5000원짜리 지폐 : " + str(five_thousand) + "장")

print("1000원짜리 지폐 : " + str(one_thousand )+ "장")


#100,000원을 내고 54,000원짜리 상품을 구매할 때 받는 거스름돈

calculate_change(100000, 54000)


#다른 방법으로 함수 정의하기

def calculate_change(payment, cost):

change_money = payment - cost
fifty_thousands = change_money//50000
ten_thousands = (change_money - fifty_thousands*50000)//10000
five_thousands = (change_money - fifty_thousands*50000 - ten_thousands*10000)//5000
thousands = (change_money - fifty_thousands*50000 - ten_thousands*10000 - five_thousands*5000)//1000

print("50000원 지폐: " + str(fifty_thousands) + "장")
print("10000원 지폐: " + str(ten_thousands) + "장")
print("5000원 지폐: " + str(five_thousands) + "장")
print("1000원 지폐: " + str(thousands) + "장")






+ Recent posts