def calculate_rectangle_area(x , y):
    return x * y
rectangle_x = 10
rectangle_y = 20
print ("사각형 x의 길이: ", rectangle_x)
print ("사각형 y의 길이: ", rectangle_y)
# 넓이를 구하는 함수 호출
print ("사각형의 넓이: ", calculate_rectangle_area(rectangle_x, rectangle_y))
사각형 x의 길이:  10
사각형 y의 길이:  20
사각형의 넓이:  200

함수 2개 중첩사용

def f(x):
    return 2 * x + 7
def g(x):
    return x ** 2
x = 2
print (f(x) + g(x) + f(g(x)) + g(f(x)))
151

input() 함수는콘솔창에서문자열을입력받는함수

print ("Enter your name:")
somebody = input() # 콘솔창에서 입력한 값을 somebody에 저장
print ("Hi", somebody, "How are you today?")
Enter your name:
Moon
Hi Moon How are you today?

콤마(,) 사용할경우 print 문이연결됨

print ("Hello World!", "Hello Again!!!")
Hello World! Hello Again!!!
temperature = float(input("온도를 입력하세요 :")) 
print(temperature)
온도를 입력하세요 :36.5
36.5

print formatting

print("a" + " " + "b" + " " + "c")
print("%d %d %d" % (1,2,3)) # %-format
print("{} {} {}".format("a","b","c")) # str.fomrat

value='1 2 3'
print(f"value is {value}") # f-string
a b c
1 2 3
a b c
value is 1 2 3

%-format

  • %datatype” % (variable) 형태로 출력양식을표현

image.png

print("I eat %d apples." % 3)
print("I eat %s apples." % "five")
number = 3; day="three"
print("I ate %d apples. I was sick for %s days."
% (number, day))
print("Product: %s, Price per unit: %f." % ("Apple", 5.243))
I eat 3 apples.
I eat five apples.
I ate 3 apples. I was sick for three days.
Product: Apple, Price per unit: 5.243000.

실습 : 화씨 변화기

  • 화씨 온도 변환 공식은: ((9/5) * 섭씨온도 ) + 32
c = input('섭씨 온도 : ')
섭씨 온도 : 32.2
c = float(c)
F = ((9/5)*c)+32
print('화씨온도 : {:.2f}'.format(f))
화씨온도 : 89.96
print('화씨온도 : {answer:.2f}'.format(answer=F))
화씨온도 : 89.96

+ Recent posts