a = 1 b = a a = [1,2,3] b = [4,5,a] x = [a,b,100] y = x print x x.append(200) print y L = [1,2,3,4,5] M = L[1:4] # 슬라이싱 - 실제 객체 복사의 한 방법 L[1] = 200 print L print M # L의 변경이 M에 영향을 미치지 않는다. import copy a = [1, 2, 3] b = [4, 5, a] x = [a, b, 100, 'w'] y = copy.copy(x) print x print y print x[1][1] = 1000 x[2] = 200 x[3] = 'y' print x print y import copy a = [1, 2, 3] b = [4, 5, a] x = [a, b, 100, 'w'] y = copy.deepcopy(x) print x print y print x[1][1] = 1000 x[2] = 200 x[3] = 'y' print x print y s = '1234' i = 10 f = 4.56 l = 12345678L print int(f), int(l), int(s) print int(1.1) print int(1.9) print int(-1.1) print int(-1.9) print round(1.1) print round(1.49) print round(1.5) print round(1.9) print print round(-1.1) print round(-1.49) print round(-1.5) print round(-1.9) import math print math.floor(1.0) print math.floor(1.1) print math.floor(1.9) print math.floor(-1.0) print math.floor(-1.1) print math.floor(-1.9) print math.ceil(1.0) print math.ceil(1.1) print math.ceil(1.9) print math.ceil(-1.0) print math.ceil(-1.1) print math.ceil(-1.9) s = '1234' i = 10 f = 4.56 l = 12345678L print float(i), float(l), float(s) print long(i), long(f), long(s) t = (1,2,3,4) l = [5,6,7,8] s = 'abcd' print list(t), list(s) print tuple(l), tuple(s) a = str([1, 2, 3]) b = repr([1, 2, 3]) print type(a), type(b) print a print b print print str([1,2,3]), str([4,5,6]), str('abc') print repr([1,2,3]), repr([4,5,6]), repr('abc') print print str(123), str(123.34), str(123456L) print repr(123), repr(123.34), repr(123456L) s1 = repr([5,6,7,8]) print type(s1) print s1 print list(s1) print s2 = eval(s1) # 문자열 '[5,6,7,8]'로부터 역으로 리스트 생성 print type(s2) print s2 x = 1 print eval('x+1') a = {1:"one", 2:"two"} b = repr(a) # a를 형식적인 문자열로 변환 print type(b) print b print c = eval(b) # b를 실행하여(즉, 사전이만들어진다) c에 치환 print type(c) print c L = ['파란하늘', 'blue sky', 1, 1234L, 1/3.0] for s in L: print 's:', s print 'str(s):', str(s) print 'repr(s):', repr(s) print '\'s\':', `s` print import string s = 'Python is the first language' L = s.split() print L print ' '.join(L) print '-'.join(L) d = {1:'one', 2:'two', 3:'three'} print d.keys() print d.values() print d.items() keys = ['a', 'b', 'c', 'd'] values = [1, 2, 3, 4] L = zip(keys, values) print L print dict(L) print chr(97) # ASCII코드 -> 문자 print ord('a') # 문자 -> ASCII코드 print int('64', 16) # 16진수 '64'를 10진수로 print int('144', 8) # 8진수 '144'를 10진수로 print int('101111', 2) # 2진수 '101111'을 10진수로 print int('14', 5) # 5진수 '14'를 10진수로 print hex(100) #10진수 100을 16진수 문자열로 변환 print oct(100) #10진수 100을 8진수 문자열로 변환 print "%o" % 23 #file : int2bin1.py octtab = {'0':'000', '1':'001', '2':'010', '3':'011', '4':'100', '5':'101', '6':'110', '7':'111', } def bin1(d): "integer to binary (string)" s = "%o" % d # 10진수 d를 8진법 수의 문자열로 포맷팅 b = '' for el in s: b += octtab[el] return b print bin1(23) # 10진수 23을 2진수로 #file : int2bin2.py def bin2(n): result = [] while 1: result[:0] = [str(n&1)] n >>= 1 if not n: break results = ''.join(result) return results print bin2(23) # 10진수 23을 2진수로