JINIers
[Python 100 day challenge ] Day 2 본문
day2는 데이터 타입을 배웠다.
# data types
#Data Types
#String
# 가장 기본적인 데이터 타입
print("hello"[4])
"123" # "" 안에 있으면 어쨌든 문자로 취급
print("123" + "345")
# integer : 정수
print(123 + 345)
123_456_789 → 123456789 로 인식
# float : 소수
3.14159
# boolean : 참/거짓
True
False
# 유형 오류, 유형 검사 및 유형 변환
num_char = len(input("what is your name?"))
# print("your name has " + num_char + " characters.") error : num_char가 정수가 아님
new_num_char = str(num_char)
print("your name has " + new_num_char + " characters.")
print(type(num_char))
a = 123
print(type(a))
print(70 + float("100.5"))
print(str(70) + str(100))
# 파이썬의 수학적 연산
3 + 5
10 - 3
7 * 4
6 / 3
2**10 # 2의 10승
# 같은 코드줄에 하나 이상의 연산을 하면 특정 우선순위가 있음
# pemdaslr
() # paresntheses
** # exponents
* # multiplication
/ # division
+ # addition
- # subtraction
print(3*3+3/3-3)
print(3*(3+3)/3-3)
# rounding numbers(반올림)
print(8 / 3)
print(int(8 / 3))
print(round(8 / 3))
print(round(8 / 3, 2))
print(round(2.66666666, 2))
print(8 // 3)
result = 4 / 2
result /= 2
print(result)
# user scored a point
score = 0
score += 1 #score = score + 1 와 같다.
# score -= 1 = score = score - 1
# score *= 1 = score = score * 1
# score /= 1 = score = score / 1
print(score)
print("your score is " + str(score))
score = 0
height = 1.8
isWinning = True
# f-string : 다양한 데이터를 복잡하지 않게 넣을 수 있다.
print(
f"your score is {score}, your heigh is {height}, you are winning is {isWinning}"
)
# 나눗셈 후 나머지 ' % '
# data type exercise / 유형오류, 유형검사, 유형변환
두자리 숫자의 숫자를 더하는 프로그램 작성
* 입력 : 34
3 + 4 = 7
출력 : 7
two_digit_number = input()
a = int(two_digit_number[0])
b = int(two_digit_number[1])
sum = a + b
print(sum)
실행 결과

# BMI calcuator / 파이썬의 수학적 연산
bmi 계산기 만들기
height = input("height: ") # 178 → 1.78로 입력
weight = input("weight: ")
h = float(height)
w = float(weight)
bmi = w / (h ** 2)
b = int(bmi)
print(b)
실행결과

# how many weeks my life? / 숫자조작과 f 문자
나이를 입력하면 몇주 살았는지 출력하기
age = input("age: ")
life_age = 90 - int(age)
life_week = life_age * 52
print(f"You have {life_week} weeks left.")
실행결과

# tip calcuator(팁 계산기) 만들기
계산서 : $150
인원 : 5명
팁 : 12%
(150.00 / 5) * 1.12
1. 결과를 소수점 이하 2자리로 만들어서 출력
print("Welcome to the tip calculator!")
# 변수설정
bill = float(input("what was the total bill? $"))
tip = int(input("what percantage tip would you like to give? 10, 12, or 15? "))
people = int(input("how many people to split the bill? "))
# 수식
tip_percent = tip / 100
tip_cost = bill * tip_percent
bill_with_tip = bill + tip_cost
pay = round(bill_with_tip / people, 2)
#결과 출력
print(f"Each person should pay: ${pay}")
실행결과

강사님은 12%에 1.12 를 곱하시던데
나는 바보라 그런건 모르겠고 ㅜㅜ
걍 tip / 100 해서 '0.10', '0.12', '0.15' 로 계산해서 할 수있게 했다.
'공부 > Python' 카테고리의 다른 글
[Python 100 day challenge ] Day 3 - 2 (1) | 2024.03.15 |
---|---|
[Python 100 day challenge ] Day 3 - 1 (0) | 2024.03.15 |
[Python 100 day challenge ] Day 1 (0) | 2024.03.15 |
1장 파이썬 기초 연습문제 (0) | 2022.06.11 |
[GCP] flask로 rest api 구현하기 (0) | 2022.05.09 |