a=10001wk-2: 파이썬의 자료형 (1)
강의영상
youtube: https://youtube.com/playlist?list=PLQqh36zP38-w17wsQ3-WMvDNNWEX52GOX
Intro
- 파이썬의 기본자료형은 int, float, bool, str, list, tuple, dict, set 등이 있다.
- 0차원 자료형: int, float, bool
- 1차원 자료형: str, list, tuple, dict, set
int, float, bool
- int형
type(a)int
- float형
a=1.2*3
a3.5999999999999996
type(a)float
a?Type: float String form: 3.5999999999999996 Docstring: Convert a string or number to a floating point number, if possible.
- bool형
a=True ## 숫자1으로 생각할 수 있음
b=False ## 숫자0으로 생각할 수 있음type(a)bool
type(b)bool
a?Type: bool String form: True Docstring: bool(x) -> bool Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.
b?Type: bool String form: False Docstring: bool(x) -> bool Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.
- bool형의 연산
a=True ## 1
b=False ## 0 a+b1
a*b 0
- complex형
a=1+2j
b=2-2jtype(a)complex
type(b)complex
a?Type: complex String form: (1+2j) Docstring: Create a complex number from a real part and an optional imaginary part. This is equivalent to (real + imag*1j) where imag defaults to 0.
b?Type: complex String form: (2-2j) Docstring: Create a complex number from a real part and an optional imaginary part. This is equivalent to (real + imag*1j) where imag defaults to 0.
c=a+bc(3+0j)
- 형태변환: float \(\to\) int
(예시1)
a=3.0
type(a)float
a=int(a)type(a)int
(예시2) 이경우는 정보의 손실이 발생
a=3.14
int(a)3
- 형태변환: int \(\to\) float
a=3
type(a)int
a=float(a)
type(a)float
- 형태변환: bool \(\to\) int/float, int/float \(\to\) bool
(예시1)
a=True
type(a)bool
int(a)1
float(a)1.0
(예시2)
a=1
bool(a)True
a=0
bool(a)False
(예시3)
a=1.0
bool(a)True
a=0.0
bool(a)False
- 이상한 형태변환도 가능하다. (이런것도 바꿔주나 싶은것도 바꿔줌)
bool(-3.14)True
- 저는 이런 코드를 의도적으로 사용하지 않아요..
int(3.14)3
- 형태변환이 항상가능한것도 아님
float(3+0j) # 사실상 3+0j=3 이므로 float으로 형변환하면 3.0이 되어야 할 것 같은데 변환불가능하다. TypeError: can't convert complex to float
- 암묵적형변환 (implicit)
(예비학습) implicit의 의미
- 추운날씨 -> 보일러좀 틀자! (explicit) / 오늘 날씨 좀 추운 것 같지 않아? (implicit)
- 짜장면 먹을래? -> 싫어! (explicit) / 난 어제 짜장면 먹었는데.. (implicit)
(예제)
True * 1 # 1을 곱할건데 너 계속 True로 있을꺼야? 1
1 * 1.0 # 1.0을 곱할건데 너 계속 int로 있을꺼야? 1.0
True+True # +연산을 할건데 계속 True로 있을꺼야? 2
숙제
아래 강의노트의 영상 1-3을 참고하여 주피터랩을 설치하고 설치성공한 화면을 스크린샷으로 LMS에 제출