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) 형태로 출력양식을표현
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
'[파이썬] 개념정리 > [파이썬] 구문,문법정리' 카테고리의 다른 글
[Python] Scoping Rule의 종류 (0) | 2021.01.20 |
---|---|
[Python] Str과 Scoping Rule (0) | 2021.01.20 |
[Python] list와 str의 슬라이싱(Slicing) (0) | 2020.07.10 |
[Python] 객체지향 문법2 (생성자와 소멸자 method) (0) | 2020.07.08 |
[Python] 객체지향 문법 (class와 object) (0) | 2020.07.08 |