L = [1,2,3]
print(type(L))
print()
print(len(L))
print()
print(L[1])
print(L[-1])
print(L[1:3])
print()
print(L + L)
print(L * 3)
print()
print(L)
<class 'list'> 3 2 3 [2, 3] [1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 2, 3]
l1 = [4,5,6]
l1[0] = 10
print(l1)
[10, 5, 6]
l1 = [1,2,3]
l1 = [4,5,6]
print(l1)
[4, 5, 6]
range()
함수를 통한 인덱스 리스트 생성¶range(k)
: 0부터 k-1까지의 range object를 반환함 (python3)L = range(10)
print(L)
print(L[::2])
print(L[::-1])
print(4 in L)
print()
print(list(L))
print(list(L)[::2])
print(list(L)[::-1])
range(0, 10) range(0, 10, 2) range(9, -1, -1) True [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 2, 4, 6, 8] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
immutable
하다.)months = ('January','February','March','April','May','June','July','August','September','October','November',' December')
t = (1,2,3)
print(len(t))
print()
print(t[0])
print(t[-1])
print(t[0:2])
print(t[::2])
print()
print(t + t + t)
print(t * 3)
print()
print(3 in t)
3 1 3 (1, 2) (1, 3) (1, 2, 3, 1, 2, 3, 1, 2, 3) (1, 2, 3, 1, 2, 3, 1, 2, 3) True
immutable
)t = (1,2,3)
t[0] = 100
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-6-b050fc22719c> in <module>() 1 t = (1,2,3) 2 ----> 3 t[0] = 100 TypeError: 'tuple' object does not support item assignment
mutable
)L = [1,2,3]
L[0] = 100
print(L)
[100, 2, 3]
d = {'one': 'hana', 'two': 'dul', 'three': 'set'}
print(d['one'])
hana
d = {'one': 'hana', 'two': 'dul', 'three': 'set'}
d['four'] = 'net' # 새 항목의 삽입
print(d)
d['one'] = 1 # 기존 항목의 값 변경
print(d)
print('one' in d) # 키에 대한 멤버쉽 테스트
{'one': 'hana', 'two': 'dul', 'three': 'set', 'four': 'net'} {'one': 1, 'two': 'dul', 'three': 'set', 'four': 'net'} True
d = {'one': 1, 'two': 'dul', 'three': 'set', 'four': 'net'}
print(d.keys()) # 키만 dict_keys 객체로 추출(Python3.6), 키만 리스트로 추출(Python2.7)
print(d.values()) # 값만 dict_values 객체로 추출(Python3.6), 값만 리스트로 추출(Python2.7)
print(d.items()) # 키와 값의 튜플을 dict_items 객체로 추출(Python 3.6), 키와 값의 튜플을 리스트로 반환함(Python2.7)
# ['four', 'three', 'two', 'one']
# ['net', 'set', 'dul', 1]
# [('four', 'net'), ('three', 'set'), ('two', 'dul'), ('one', 1)]
dict_keys(['one', 'two', 'three', 'four']) dict_values([1, 'dul', 'set', 'net']) dict_items([('one', 1), ('two', 'dul'), ('three', 'set'), ('four', 'net')])
자료형 | 저장/접근 방법 | 변경 가능성 | 저장 모델 |
---|---|---|---|
수치형 | 직접(Direct) | 변경불가능(Immutable) | 리터럴 (Literal) |
문자열 | 시퀀스 (Sequence) | 변경불가능(Immutable) | 리터럴 (Literal) |
리스트 | 시퀀스 (Sequence) | 변경가능(Mutable) | 컨테이너 (Container) |
튜플 | 시퀀스 (Sequence) | 변경불가능(Immutable) | 컨테이너 (Container) |
사전 | 매핑 (Mapping) | 변경가능(Mutable) | 컨테이너 (Container) |
print(type(3)) #정수
print(type(3.3)) #실수
print(type('abc')) #문자열
# <type 'int'>
# <type 'float'>
# <type 'str'>
<class 'int'> <class 'float'> <class 'str'>
print(type([])) #리스트
print(type(())) #튜플
print(type({})) #사전(dict)
# <type 'list'>
# <type 'tuple'>
# <type 'dict'>
<class 'list'> <class 'tuple'> <class 'dict'>
a = 0
L = [1,2,3]
print(type(a) == type(0))
print(type(L) == type([]))
print(type(L[0]) == type(0))
True True True
print(type(None)) # NoneType 객체, 아무 값도 없음을 나타내는 객체
print()
a = None
print(a)
print(type(a))
# <type 'NoneType'>
# None
# <type 'NoneType'>
<class 'NoneType'> None <class 'NoneType'>
id()
: 객체의 식별자를 반환한다.a = 500
b = a
print(id(a))
print(id(b))
print()
# 작은 값의 정수는 계속하여 별도로 객체를 생성하지 않음
x = 1
y = 1
print(id(x))
print(id(y))
print()
# 작은 값의 정수는 별도로 객체를 생성
x = 1000000000000000000
y = 1000000000000000000
print(id(x))
print(id(y))
4469734928 4469734928 4430976352 4430976352 4469735056 4469734448
is
키워드: 두 객체의 식별자가 동일한지 테스트한다.c = [1,2,3]
d = [1,2,3]
print(c is d)
a = 500
b = a
print(a is b)
x = 1
y = 1
print(x is y)
e = f = [4,5,6]
print(e is f)
False True True True
c = [1,2,3]
d = [1,2,3]
print(c == d)
True
x = 1
y = 1
print(x is y)
print(id(x) == id(y))
print(x == y)
True True True
x = 900000000
y = 900000000
print(x is y)
print(id(x) == id(y))
print(x == y)
False False True
x = y = z = 100
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일