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
[[1, 2, 3], [4, 5, [1, 2, 3]], 100] [[1, 2, 3], [4, 5, [1, 2, 3]], 100, 200]
L = [1,2,3,4,5]
M = L[1:4] # 슬라이싱 - 실제 객체 복사의 한 방법
L[1] = 200
print L
print M # L의 변경이 M에 영향을 미치지 않는다.
[1, 200, 3, 4, 5] [2, 3, 4]
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
[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w'] [[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w'] [[1, 2, 3], [4, 1000, [1, 2, 3]], 200, 'y'] [[1, 2, 3], [4, 1000, [1, 2, 3]], 100, 'w']
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
[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w'] [[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w'] [[1, 2, 3], [4, 1000, [1, 2, 3]], 200, 'y'] [[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w']
s = '1234'
i = 10
f = 4.56
l = 12345678L
print int(f), int(l), int(s)
4 12345678 1234
print int(1.1)
print int(1.9)
print int(-1.1)
print int(-1.9)
1 1 -1 -1
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)
1.0 1.0 2.0 2.0 -1.0 -1.0 -2.0 -2.0
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)
1.0 1.0 1.0 -1.0 -2.0 -2.0
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)
1.0 2.0 2.0 -1.0 -1.0 -1.0
s = '1234'
i = 10
f = 4.56
l = 12345678L
print float(i), float(l), float(s)
print long(i), long(f), long(s)
10.0 12345678.0 1234.0 10 4 1234
t = (1,2,3,4)
l = [5,6,7,8]
s = 'abcd'
print list(t), list(s)
print tuple(l), tuple(s)
[1, 2, 3, 4] ['a', 'b', 'c', 'd'] (5, 6, 7, 8) ('a', 'b', 'c', 'd')
a = str([1, 2, 3])
b = repr([1, 2, 3])
print type(a), type(b)
print a
print b
print
<type 'str'> <type 'str'> [1, 2, 3] [1, 2, 3]
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)
[1, 2, 3] [4, 5, 6] abc [1, 2, 3] [4, 5, 6] 'abc' 123 123.34 123456 123 123.34 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
<type 'str'> [5, 6, 7, 8] ['[', '5', ',', ' ', '6', ',', ' ', '7', ',', ' ', '8', ']'] <type 'list'> [5, 6, 7, 8]
x = 1
print eval('x+1')
2
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
<type 'str'> {1: 'one', 2: 'two'} <type 'dict'> {1: 'one', 2: 'two'}
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
s: 파란하늘 str(s): 파란하늘 repr(s): '\xed\x8c\x8c\xeb\x9e\x80\xed\x95\x98\xeb\x8a\x98' 's': '\xed\x8c\x8c\xeb\x9e\x80\xed\x95\x98\xeb\x8a\x98' s: blue sky str(s): blue sky repr(s): 'blue sky' 's': 'blue sky' s: 1 str(s): 1 repr(s): 1 's': 1 s: 1234 str(s): 1234 repr(s): 1234L 's': 1234L s: 0.333333333333 str(s): 0.333333333333 repr(s): 0.3333333333333333 's': 0.3333333333333333
import string
s = 'Python is the first language'
L = s.split()
print L
print ' '.join(L)
print '-'.join(L)
['Python', 'is', 'the', 'first', 'language'] Python is the first language Python-is-the-first-language
d = {1:'one', 2:'two', 3:'three'}
print d.keys()
print d.values()
print d.items()
[1, 2, 3] ['one', 'two', 'three'] [(1, 'one'), (2, 'two'), (3, 'three')]
keys = ['a', 'b', 'c', 'd']
values = [1, 2, 3, 4]
L = zip(keys, values)
print L
print dict(L)
[('a', 1), ('b', 2), ('c', 3), ('d', 4)] {'a': 1, 'c': 3, 'b': 2, 'd': 4}
print chr(97) # ASCII코드 -> 문자
print ord('a') # 문자 -> ASCII코드
a 97
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진수로
100 100 47 9
print hex(100) #10진수 100을 16진수 문자열로 변환
print oct(100) #10진수 100을 8진수 문자열로 변환
0x64 0144
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진수로
27 010111
#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진수로
10111