1. 파이썬의 기본 데이터 형식
print("Hello"[0])
print(123_456_789)
- output : H (Hello의 첫번째 글자) - subscript
- 문자열 (string) : a,b,c, 항상 큰 따옴표로 묶기
- float : ex)3.145
- boolean : TRUE or FALSE (주로 테스트를 할 때 사용됨)
- integer : 긴 숫자의 경우엔 _ 로 구분하기, 컴퓨터는 이를 무시하고 계산함.
2. 형식 오류와 형식 확인 그리고 형 변환
num_char = len(input("What is your name?"))
print("Your name has" + num_char + "characters.")
num_char = len(input("What is your name?"))
print("Your name has" + str(num_char) + "characters.")
- output : type error / 문자+정수를 바로 연결시킬 수 없음
- type() : 괄호 안의 데이터의 형식에 대해 확인 가능함
- str() : 데이터 형식을 문자열로 변경해줌.
- float() : 부동 소수 형식으로 바꾸어줌. (소수점 형태)
3. quiz
two_digit_number = input("Type a two digit number: ")
f_num = int((two_digit_number)[0])
s_num = int((two_digit_number)[1])
print(f_num+s_num)
4. 파이썬의 수학 연산
print(3*2)
print(6/2)
print(2**2)
- 파이썬에서 나눗셈을 연산하게 되면 결과값은 float 형태로 추출됨.
- 연산 순서(PEMDAS) : () -> * -> ->/ -> + -> -
- 계산은 왼쪽에서 오른쪽으로 진행됨.
5. quiz
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
bmi = (float(weight) / (float(height)**2))
print(int(bmi))
6. 파이썬의 숫자 처리 및 F-string
print(round(8/3),2)
print(8//3)
score = 0
score += 1
print(score)
- += or -= or *= or /= : 이전 값에 기반해서 계산해야 할 때 유용함.
score = 0
height = 1.8
isWinning = True
print(f"your score is {score}")
print(f"your score is {score}, your height is {height}, you are winning is {isWinning}")
- 문자열 앞에 f를 붙이면 뒤에 어떤 형태의 데이터와 합칠 수 있음. 단, 중괄호{} 사용!
7. quiZ
age = input("What is your current age?")
cal = 90 - int(age)
remain_d = round(cal * 365)
remain_w = round(cal*52)
remain_m = round(cal*12)
message = f"You have {remain_d}days, {remain_w} weeks and {remain_m} months left."
print(message)
8. project_2nd
print("Welcome to the tip calculator.")
total_bill = float(input("What was the total bill? $"))
tip = float(input("What percentage tip would you like to give? 10,12, or 15?"))
n_people = int(input("How many people to split the bill?"))
tip_as_per = tip/100
pay_amount = (total_bill/n_people)*(tip_as_per+1)
final_amount = round(pay_amount,2)
final_amount = "{:.2f}".format(pay_amount)
print(f"Each person should pay : $ {final_amount}")
- "{:.2f}".format(input_data) : 소수점 몇 자리까지 표시할 건지!!!!