04 파이썬 기본 자료형
주의
한개 값을 저장 하는 자료형 Boolean, String, Number
1. Boolean 자료형
참고
- 참,거짓을 판단하는 불(boolean) 자료형을 알아보자
- 두 값의 관계를 판단하는 비교 연산자를 알아보자
- 두 값의 논리값을 판단하는 논리 연산자를 알아보자
1.1. 불과 비교연산자
불은 True, False 로 표현하는 참과 거짓을 뜻하는 값이다.
- Code
- Result
1print(3>1) #3은 1보다 크므로 true 가 출력된다.2print(10==10)3print(10!=5)4print("10"=="10")5print("ab"=="Ab")6print("ab"!="Ab")10==10→ 10과 10은 같으므로 True10!=5→ 10과 5는 다르므로 True"10"=="10"→ 문자열 “10”과 “10”은 같으므로 True"ab"=="Ab"→ 대소문자가 다르므로 False"ab"!="Ab"→ 대소문자가 다르므로 True
- Code
- Result
1print(10>10)2print(10<5)3print(10>=10)4print(10<=10)10>10→ 10은 10보다 크지 않으므로 False10<5→ 10은 5보다 작지 않으므로 False10>=10→ 10은 10보다 크거나 같으므로 True10<=10→ 10은 10보다 작거나 같으므로 True
요약
부등호 설명시
> 초과, < 미만, [값 포함 X]
>= 이상,<= 이하 [값 포함 O]
1.2. 객체의 비교 [is]
1print(1==1.0) #True2print(1 is 1.0) #False3print(1 is not 1.0) #True참고
==,!=값만 비교 (1과 1.0은 값이 같아서 True)is,is not완전히 똑같은지 비교 (1과 1.0은 다른종류라 False)
is,is not은 메모리주소를 비교 값을 비교할때는 사용금지
1.3. 논리연산자
- Code1
- Code2
1#and2print(True and True)3print(False and True)4print(True and False)5print(False and False)6
7#or8print(True or True)9print(False or True)10print(True or False)11print(False or False)12
13#not14print(not True)15print(not True)- True (둘 다 참이어야 참)
- False (하나라도 거짓이면 거짓)
- False (하나라도 거짓이면 거짓)
- False (둘 다 거짓이면 거짓)
- True (하나라도 참이면 참)
- True (하나라도 참이면 참)
- True (하나라도 참이면 참)
- False (둘 다 거짓이면 거짓)
- False (참의 반대는 거짓)
- True (거짓의 반대는 참)
1print(not True and False or not False)단계별 계산:
not True→ Falsenot False→ TrueFalse and False→ FalseFalse or True→ True
최종 결과: True
:::caution 연산자 우선순위:
not(가장 먼저)andor(가장 나중) :::
1print(((not True) and False) or (not False))위 코드와 완전히 같음!
1.4. 논리연산자와 비교연산자
1print(10==10 and 10!=5) # t and t = t2print(10>5 or 10<3) # t or f = t3print(not 10>5) # t=f4print(not 1 is 1.0) # f=t1.5. 정수,실수,문자열을 불로 바꾸기
참고
bool() 함수로 다른 자료형을 True/False로 바꿀 수 있다.
- 숫자:
0만 False, 나머지는 모두 True - 문자열: 빈 문자열
""만 False, 내용이 있으면 True
1print(bool(1)) #True2print(bool(0)) #False3print(bool(1.5)) #True4print(bool('False')) #True (문자열이 있으므로)1.6. 단락평가
참고
논리 연산에서 중요한 부분이 단락 평가(short-circuit evaluition) 이다.
단락 평가(short-circuit evaluation): 논리 연산에서 결과가 확정되면 나머지를 계산하지 않는 방식
and: 첫 번째가 False면 두 번째는 확인 안 함 (어차피 결과는 False)or: 첫 번째가 True면 두 번째는 확인 안 함 (어차피 결과는 True)
1# and 단락평가2print(False and True) # False (두 번째는 확인 안 함)3print(True and False) # False (첫 번째가 True라 두 번째까지 확인)4print(False and False) # False (두 번째는 확인 안 함)5
6# or 단락평가7print(True or True) # True (두 번째는 확인 안 함)8print(False or True) # True (첫 번째가 False라 두 번째까지 확인)9print(True or False) # True (두 번째는 확인 안 함)1.7. 문제
표준 입력으로 국어,영어,수학,과학 점수가 입력됩니다.
국어는 90점 이상, 영어는 80점 초과, 수학은 80점 초과, 과학은 80점 이상일때 합격입니다.
한 과목 이라도 조건에 만족하지 않을시 불합격).
합격이면 True, 불합격 이면 False 가 출력되게 하세요.
input 에서 안내 문자열은 출력하지 않아도 됨.
테스트케이스 예제
190 80 86 801True테스트케이스 예제
190 80 85 801False🐨 Table of contents
1kor,eng,math,sci=map(int,input().split(' '))2print(kor >= 90 and eng > 80 and math > 80 and sci >= 80)
2. String 자료형
2.1. 문자열의 사용
1hello="안녕"2print(hello)3helloo="""아안녕"""4print(helloo)5hellooo='''아o안녕'''6print(hellooo)2.1.1. 여러행 문자열 사용
1hellooo='''아o안녕2뭐3해4'''5print(hellooo)2.1.2. 문자열내에 따옴표 넣기
1word='py"thon"'2print(word)3word1="th'pyon'"4print(word1)5word2="th\"pyon""6print(word2)7br="th\n"pyon""8print(br)3. Number 자료형
참고
- 정수 (int): 1, 100, -5처럼 소수점이 없는 숫자
- 실수 (float): 3.14, -0.5처럼 소수점이 있는 숫자 로 나뉨
1print(273)2print(52.273)3print(type(273)) #<class 'int'>4print(type(52.273)) #<class 'float'>참고
자료형을 확인하면 int 와 float를 출력.
int는 integer의 앞 세 글자를 떼어 낸 것으로 정수를 의미
float는 floating point의 앞 단어에서 가져온 것으로 부동소수점(실수)을 의미.