728x90
Python에서 사용되는 Data type을 정리해 보았습니다.
데이터 타입 | 선언 | 확인 방법 |
정수 | a = 10 | print(a) print(type(a)) |
실수 | a = 1.12 | print(a) print(type(a)) |
복소수 | a = 1+2j | print(a) print(type(a)) print(a.imag) print(a.real) print(a.conjugate()) |
bool | a = True b = False |
print(a) print(type(a)) print(b) print(type(b)) |
string | a = 'abcdefg' b = "abcdefg" c = '''a b''' d = """a b""" e = " abc def g " |
print(a) print(b) print(c) print(d) print(e) # 인덱스 print(a[0]) print(a[1]) print(a[-1]) # [시작:끝:증가치] print(a[0:3:1]) print(a[-1:-2:-1]) # [시작:끝] print(a[1:3]) print(a[:1:3]) print(a[1:]) print(a+b) # a 2회 반복 print(a*2) print(c.split()) print(e.strip()) |
bytes | a = b'abc' | print(a) print(type(a)) print(a.hex()) |
encode decode |
a = 'abc' a = a.encode('utf-8') b = a.decode('utf-8') |
print(a) print(type(a)) print(b) print(type(b)) |
list | a = [1, 1.23, 'abc', True, 1.23] #unpacking x, y, z = (10, 20, 30) |
print(a) print(type(a)) print(a[1]) print(a[-1]) print(a[1:4:2]) ### a.append(10) print(a) print(a.count(1.23)) a.extend([5, 6]) print(a) print(a.index('abc')) a.insert(3, 'z') print(a) a.pop(2) print(a) a.remove('z') print(a) a.reverse() print(a) print(len(a)) del(a[0]) print(a) a = a + ['a', 'b'] print(a) a = a * 2 print(a) print(x, y, z) #int int int |
tuple | a = (1, 2, 3, 4, 5) #packing b = 10, 20, 30 #unpacking x, y, z = (10, 20, 30) |
print(a) print(type(a)) print(a[0]) print(a[0:2]) print(b) #tuple print(x, y, z) #int int int |
dictionary {key:value} | a = {1:'a', 2:'b', 3:'c'} b = {'key':'12345', 'value':'myhouse'} b['key'] = '34345' b['door'] = True |
print(a) print(type(a)) print(a[2]) print(b) print(b['key']) print(b.get('value')) print(b.keys()) print(b.values()) print(a.items()) print(a.pop(1)) print(a) del(a[2]) print(a) |
multi index | a = [1, 2, (3, 4, 5)] b = (1, 2, [3, 4, 5]) c = [1, 2, (3, 4, [5, 9, 10]), [6, 7, 8]] |
print(a) print(type(a)) print(a[2]) print(type(a[2])) print(a[2][0]) print(b) print(type(b)) print(b[2]) print(type(b[2])) print(c[2][2][2]) |
set | #중복 데이터는 자동으로 제거됨 a = {1, 2, 3, 4, 2, 4, 5} b = {1, 2, 3, 7, 8, 9} |
print(a) print(type(a)) a.add(10) print(a) a.remove(3) print(a) #교집합 (intersection) print(a.intersection(b)) #교집합 (intersection) print(a & b) #합집합 (union) print(a | b) #차집합 (difference) print(a - b) #대칭 차집합 (symmetric difference) print(a ^ b) |
'프로그래밍 언어 > Python' 카테고리의 다른 글
Python 함수 (function) -def- (0) | 2021.02.27 |
---|---|
Python 반복문 (for, while) (0) | 2021.02.27 |
Python의 조건 판단 (if-elif-else) (0) | 2021.02.26 |
Python의 연산자 (Operator) (0) | 2021.02.26 |
Python 환경설정 -Python 3.9, PyCharm 설치- (0) | 2021.02.26 |