~cpp
>>> l = []
>>> dir(l)
>>> help(l.append)
Help on built-in function append:
append(...)
L.append(object) -- append object to end
~cpp
>>> raw_input('your name? ')
your name? zp
'zp'
>>> n = input('숫자 입력하세요. ')
숫자 입력하세요. 5
>>>
>>> n
5
~cpp a = 10 int(32bit) b = 2.5 float(64bit) c = 999999999999999999L long형(무제한) d = 4 + 5j complex(각 64bit)
~cpp x ** y power. x의 y제곱 divmod(x, y) returns (int(x/y), x % y) >>> divmod(5,3) (1, 2) round(x) 반올림 abs(-3) 3 int(3.15) 3 float(5) 5.0 complex(2, 5) 2 + 5j
~cpp
>>> t = (1,2,3)
>>> t * 2 반복
(1, 2, 3, 1, 2, 3)
>>> t + ('tuple',) 연결
(1, 2, 3, 'tuple')
>>> t[1:3] 슬라이싱
(2, 3)
>>> len(t) 길이
3
>>> 1 in t 멤버십 테스트
True
>>> l = list(t) 리스트로 변환
>>> l
[1, 2, 3]
>>> t = tuple(l) 튜플로 변환
>>> t
(1, 2, 3)
~cpp >>> L = [] 빈 리스트 생성 >>> L = [1,2,3] >>> len(L) 길이 3 >>> L[1] 인덱싱 2 >>> L[1:3] 슬라이싱 [2, 3] >>> L[-1] 3 >>> L + L 연결 [1, 2, 3, 1, 2, 3] >>> L * 3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> L = range(10) >>> L [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> L[::2] 확장 슬라이스 [0, 2, 4, 6, 8] >>> L[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> L.append(100) 추가 >>> L [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100] >>> L.insert(1, 50) >>> L [0, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100] >>> del L[2] 삭제 >>> L [0, 50, 2, 3, 4, 5, 6, 7, 8, 9, 100] >>> L.reverse() 순서를 바꾼다 >>> L [100, 9, 8, 7, 6, 5, 4, 3, 2, 50, 0] >>> L.sort() 정렬 >>> L [0, 2, 3, 4, 5, 6, 7, 8, 9, 50, 100]
~cpp
>>> dic = {'baseball':5, 'soccer':10, 'basketball':15}
>>> dic['baseball']
5
>>> dic['baseball'] = 20
>>> dic
{'basketball': 15, 'soccer': 10, 'baseball': 20}
~cpp
>>> dic.keys() 키들을 리스트로 리턴
['basketball', 'soccer', 'baseball']
>>> dic.values() 값들을 리스트로 리턴
[15, 10, 20]
>>> dic.items() (key, value)을 리스트로 리턴
[('basketball', 15), ('soccer', 10), ('baseball', 20)]
>>> if 'soccer' in dic: 사전이 key를 가지고 있는지 검사. 있으면 True리턴
print dic['soccer']
10
~cpp 인덱싱 s[1] 슬라이싱 s[1:3] 연결 s + 'abc' 반복 s * 2 멤버십 테스트 'a' in s 길이 len(s)
~cpp >>> sum = 0 >>> for x in range(1, 10): sum += x >>> sum 451, 3, 5, 7, 9의 합
~cpp >>> odd = 0 >>> for o in range(1, 10, 2): odd += o >>> odd 25
~cpp >>> number = [1,2,3,4,5] >>> for n in number: print n, 1 2 3 4 5
-- 재선