Data Types
# string
print(“Hello”[0]) #H
print(“123” + “456”) #123456
# integer
print(123 + 456) # 579
print(1_123 + 99_234) # 100357 : ,대신 _입력가능 (추출할때는 무시함)
# float
3.141592
# boolean
True
False
Float - Float
# 4.2 - 1.3 등 float - float 을 할 때는 round()를 이용해 반올림 해주어야 한다.
print(4.2 - 1.3) # output : 2.900000000000004
print(round(4.2 - 1.3, 2)) # output : 2.9
두자릿수 더하기
two_digit_number = input(“Type a two digit number: ”)
내가 쓴 답
string = str(two_digit_number)
print(int(string[0]) + int(string[1]))
답안
first_digit = int(two_digit_number[0])
second_digit = int(two_digit_number[1])
two_digit_number = first_digit + second_digit
print(two_digit_number)
연산순서 : PEMDASLR
Parentheses : ()
Exponents : **
Multiplication : *
Division : /
Addtion : +
Subtraction : -
Left
Right
print(3 * 3 + 3 / 3 - 3) # 7.0
print(int(3 * 3 + 3 / 3 - 3)) # 7
BMI Calculator
height = input(“enter your height in m: ”)
weight = input(“enter your weight in kg: ”)
bmi = (int(weight) / float(height) ** 2)
print(int(bmi))
input function
은 stiring(문자열)
으로 되어있으니 연산자 입력 시 주의해야 한다.
extra 연산자
print(round(8 / 3, 2)) # 2.67 : round(, n)에서 n은 나타낼 소수점 자리수
print(round(8 // 3)) # 2 : print(int(8 / 3))과 값이 같다.
print(4 // 2) # 2 : print(4 / 2)의 값이 2.0인 것과 비교
result = 4 / 2
result /= 2
print(result) # 1.0
+=
-=
*=
/=
f-string
score = 0
height = 1.8
isWinning = True
print(f”your score is {score}, your height is {height}, you are winning is {isWinning}”)
# your score is 0, your height is 1.8, you are winning is True
f-string
을 이용하면 데이터 타입과 관계없이 문구 출력하기 간편하다.
({}.format()
을 이용하는 방법도 있음)
Life in Weeks
age = input(“What is your current age? ”)
age_remaining = 90 - int(age)
day = age_remaining * 365
week = age_remaining * 52
month = age_remaining * 12
message = f”You have {day} days, {week} weeks, and {month} months left.”
print(message)
Tip Calculator
print("Welcome to the tip calculator!")
bill = float(input("What was the total bill? $"))
tip = int(input("How much tip would you like to give? 10, 12, or 15? "))
people = int(input("How many people to split the bill?"))
내가 쓴 답
each = (bill * (1 + tip / 100)) / people
pay = round(each, 2)
답안
tip_as_percent = tip / 100
total_tip_amount = bill * tip_as_percent
total_bill = bill + total_tip_amount
bill_per_person = total_bill / people
final_amount = round(bill_per_person, 2)
# FAQ: How to round to 2 decimal places?
print(f"Each person should pay: ${final_amount}")