본문 바로가기

여러가지/Python

Day 2 - 내장함수 type(), int(), float(), round(), 연산, f 문자열

● 학습 내용

 

1) 데이터 타입

# type(): 데이터 타입을 출력하는 내장 함수

age = input("How old are you? ")
number = input("What is your favriote number? ")
print("Locker Number: " + str(int(age[0]) + int(number[1])))

ex) age = 25

age[0] = 2, age[1] = 5

# int(): 문자열을 정수로 변경하는 내장 함수

# float(): 문자열을 소수로 변경하는 내장 함수

 

height = input()
weight = input()
print(int(int(weight) / float(height) ** 2))
height = float(input())
weight = int(input())
print(int(weight / height ** 2))
height = float(input())
weight = int(input())
BMI = int(weight / height ** 2)
print(BMI)

※ 위의 세개의 코드는 모두 같은 코드입니다.

 

2) 연산

** : 지수

ex) 2 ** 3 = 8

/ : 나누기

// : 몫

% : 나머지

 

# round(): 반올림하는 내장함수

ex) round((8 / 3),2) = 2.67

n : 소수 n+1 자리에서 반올림

ex) 2 : 소수 셋째 자리에서 반올림

 

3) f 문자열

score = 5
value = 7.5
isWinning = True
print(f"Score: {score}, Value: {value}, Winning: {isWinning}")

 

 

● Day 2 프로젝트

- URL : https://github.com/yooyijun15/Python/tree/main/Day2

 

print(f"Each person should pay: ${bill_per_person:.2f}")

: 소숫점 2번째 자리까지 표시