
Sub-scriptLarge Integer# 문자열에 정수를 연결
num_char = len(input("what is your name?"))
print("your name has " + num_char + " characters.")
type( data )
num_char = len(input("what is your name?"))
print(type(num_char))
형 변환 함수 : 변환하려는 형식(형 변환을 하려는 데이터)
num_char = len(input("what is your name?"))
new_num_char = str(num_char) # 정수를 문자열로 변환
print("your name has " + new_num_char + " characters.")print(70 + float("100.5")) # 70이 자동으로 70.0으로 변환되어 덧셈 계산print(bool("")) # 빈 문자열 : False
print(bool("0")) # 내용이 있는 문자열 : True
print(bool(0)) # 숫자 0 : False
print(bool(-12.56)) # 0 이외의 숫자 : TrueData Types
입력값에서 십의 자리의 숫자와 일의 자리 숫자를 더한 값을 구하는 프로그램
two_digit_number = input()
first_digit = int(two_digit_number[0])
second_digit = int(two_digit_number[1])
print(first_digit + second_digit)
print(3 + 9) # 덧셈
print(7 - 4) # 뺄셈
print(5 * 2) # 곱셈
print(6 / 3) # 나눗셈 (항상 float 형태로 출력)
print(2 ** 2) # 거듭제곱
BMI Calculator
BMI를 계산하는 프로그램
- 계산 공식 :
height = input()
weight = input()
BMI = float(weight) / float(height) ** 2
print(int(BMI))
print(round(2.66666666, 2))
print(round(8 / 3, 2))
int // int : int 반환float // int, int // float, float // float : float(정수.0) 반환형식 예시
format( data, ".nf" ) 또는 "{:.nf} {:.nf} …".format( data1, data2… )
print('{:.0f}'.format(10.05)) # 소수점 첫째자리를 반올림하여 정수값 반환
print('{:.3f}'.format(0.0543)) # 소수점 셋째자리까지만 반환
print('{:.3g}'.format(0.0543)) # 유효숫자 3개까지만 반환
print('{:.2s}'.format('Python')) # 문자열의 앞 2글자까지만 반환
print('{:<6s}'.format('Py')) # 문자열을 6칸에 맞춰 왼쪽 정렬
print('{:>6s}'.format('Py')) # 문자열을 6칸에 맞춰 오른쪽 정렬
print('{:^6s}'.format('Py')) # 문자열을 6칸에 맞춰 중앙에 정렬
print("{} {:.2s} {:.1f}".format("apple", "banana", 3.14)) # 여러 값 혼합
[ 출력 결과 ]
10
0.054
0.0543
Py
Py
Py
Py
apple ba 3.1
이전 값에 기반해서 계속 연산을 할 수 있음
score = 4
score += 1 # score = score + 1 과 동일
print(score)
score -= 1 # score = score - 1 과 동일
print(score)
score *= 2 # score = score * 2 와 동일
print(score)
score /= 2 # score = score / 2 와 동일
print(score)
score **= 2 # score = score ** 2 와 동일
print(score)
score //= 2 # score = score // 2 와 동일
print(score)
Life in Weeks
90살까지 산다고 가정했을 때, 남은 주(week) 수를 계산하는 프로그램
age = input()
years = 90 - int(age) # 남은 년수를 계산
weeks = years * 52 # 1년 = 52주
print(f"You have {weeks} weeks left.")
청구 금액에 팁을 추가한 총 금액을 여러 명이서 똑같이 나눠 낼 때, 1인당 지불해야 하는 돈을 계산하는 프로그램
🔍 유의 사항
- 팁은 백분율
- 예시) 나온 금액에 팁 12%를 더한 총액
- 팁 = 금액 * (12 / 100), 총액 = 금액 + 팁
- 총액 = 금액 * 1.12
(1번과 거의 같은 결과지만 더 간단히 계산 가능)- 최종 금액은 소수점 둘째 자리까지만 계산하면 됨
- 정수 또는 소수점 첫째 자리까지만 있는 경우에도 소수점 둘째 자리까지 있는 것이 보기 편함
- 예시) 12는 12.00로, 12.4는 12.40로 0을 넣어 소수점 둘째 자리로 통일
- round() 함수로는 자리수를 맞추는 것이 불가능하므로, format() 함수 이용하기
- 참고 링크
⌨️ 작성한 코드
#If the bill was $150.00, split between 5 people, with 12% tip.
#Each person should pay (150.00 / 5) * 1.12 = 33.6
#Format the result to 2 decimal places = 33.60
#Write your code below this line 👇
print("welcome to the tip calculator.")
bill = float(input("what was the total bill? $"))
tip = int(input("what percentage tip would you like to give? (10, 12, 15) "))
people = int(input("how many people to split the bill? "))
bill_per_person = (bill / people) * (1 + (tip / 100))
final_amount = round(bill_per_person, 2)
#If you want the final_amount to always have 2 decimal places.
#e.g. $12 becomes $12.00
#This is how you can implement it:
final_amount = "{:.2f}".format(bill_per_person)
print(f"each person should pay : ${final_amount}")