파이썬 데이터 유형
내장 데이터 유형
프로그래밍에서 데이터 유형은 중요한 개념입니다.
변수는 다른 유형의 데이터를 저장할 수 있으며 다른 유형은 다른 작업을 수행할 수 있습니다.
Python에는 이러한 범주에 기본적으로 다음 데이터 유형이 내장되어 있습니다.
텍스트 유형: | str |
숫자 유형: | int , float ,
complex |
시퀀스 유형: | list , tuple ,
range |
매핑 유형: | dict |
세트 유형: | set ,frozenset |
부울 유형: | bool |
바이너리 유형: | bytes , bytearray ,
memoryview |
데이터 유형 가져오기
type()
다음 함수 를 사용하여 모든 객체의 데이터 유형을 가져올 수 있습니다 .
예시
변수 x의 데이터 유형을 인쇄하십시오.
x = 5
print(type(x))
데이터 유형 설정
Python에서 데이터 유형은 변수에 값을 할당할 때 설정됩니다.
Example | Data Type | Try it |
---|---|---|
x = "Hello World" | str | |
x = 20 | int | |
x = 20.5 | float | |
x = 1j | complex | |
x = ["apple", "banana", "cherry"] | list | |
x = ("apple", "banana", "cherry") | tuple | |
x = range(6) | range | |
x = {"name" : "John", "age" : 36} | dict | |
x = {"apple", "banana", "cherry"} | set | |
x = frozenset({"apple", "banana", "cherry"}) | frozenset | |
x = True | bool | |
x = b"Hello" | bytes | |
x = bytearray(5) | bytearray | |
x = memoryview(bytes(5)) | memoryview |
특정 데이터 유형 설정
데이터 유형을 지정하려면 다음 생성자 함수를 사용할 수 있습니다.
Example | Data Type | Try it |
---|---|---|
x = str("Hello World") | str | |
x = int(20) | int | |
x = float(20.5) | float | |
x = complex(1j) | complex | |
x = list(("apple", "banana", "cherry")) | list | |
x = tuple(("apple", "banana", "cherry")) | tuple | |
x = range(6) | range | |
x = dict(name="John", age=36) | dict | |
x = set(("apple", "banana", "cherry")) | set | |
x = frozenset(("apple", "banana", "cherry")) | frozenset | |
x = bool(5) | bool | |
x = bytes(5) | bytes | |
x = bytearray(5) | bytearray | |
x = memoryview(bytes(5)) | memoryview |