import keyword print keyword.kwlist print print len(keyword.kwlist) str(12345) str = 'abc' str(12345) #이것은 주석입니다. import sys #이것도 주석입니다. if (a == 1) and a = 1 b = 3 if (a == 1) and \ (b == 3): print 'connected lines' a = 1 b = a 1 + 3 = a a = 1 a = a + 1 4 == 5 4 == 4 c, d = 3, 4 x = y = z = 0 e = 3.5; f = 5.6 print a, b, c, d, e, f e, f = f, e print e, f a = (b = c + d) a = 1 type(a) a = 'dynamic' type(a) a = 1 a += 4 a a -= 3 a a *= 2+3 a a = [1,2,3] b = [10, a, 20] c = ['x', a, 'y'] print a print b print c a[1] = 1000 print a print b print c a = 1 a = eval('a + 4') a eval('a = a + 4') a = 5 exec('a = a + 4') a a = 5 exec 'a = a + 4' a s = ''' a = 1 if a > 0 : print 'Success' ''' exec(s) code = compile('a + 1', '', 'eval') print type(code) a = 1 for k in range(10): a = eval(code) a code = compile('a = a + 1', '', 'single') a = 1 for k in range(10): exec(code) a s = ''' a = 1 for k in range(10): a = a + 1 print a ''' code = compile(s, '', 'exec') exec(code) name = raw_input('name?') print name k = int(raw_input('int : ')) k i = input('int :') i k = input('expr:') k print 4+5, 4-2 print 1; print 2 print 1,; print 2 print 12 + 'spam' print '12' + 'spam' import pprint complicated = ['spam', (1,2,3), ('hanm', 'egg', ('ab', 'cd', ('abc', 'def')))] complicated = complicated * 3 pprint.pprint(complicated) print complicated complicated print 'Hello World!' print "Hello World!" s = "Hello world!" print s[0] print s[1] print s[-1] print s[-2] s = "Hello world!" print s[1:3] print s[0:5] s = 'Hello' print s[1:] print s[:3] print s[:] s = 'abcd' print s[::2] print s[::-1] print 'Hello' + '' + 'World' print 'Hello' * 3 print '-' * 60 s = 'Hello World' s[0] = 'h' s = 'Hello World' s = 'h' + s[1:] s s = 'Hello World' len(s) s = 'Hello World' print 'World' in s print 'World' not in s 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 L = range(10) print L print L[::2] print L[::-1] print 4 in L L = [1,2,3] L.append(4) print L print del L[0] print L print L.reverse() print L print L.append(5) print L L.sort() print L 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 t = (1,2,3) t[0] = 100 L = [1,2,3] L[0] = 100 print L d = {'one': 'hana', 'two': 'dul', 'three': 'set'} print d['one'] d = {'one': 'hana', 'two': 'dul', 'three': 'set'} d['four'] = 'net' # 새 항목의 삽입 print d d['one'] = 1 # 기존 항목의 값 변경 print d print 'one' in d # 키에 대한 멤버쉽 테스트 d = {'one': 1, 'two': 'dul', 'three': 'set', 'four': 'net'} print d.keys() # 키만 리스트로 추출함 print d.values() # 값만 리스트로 추출함 print d.items() # 키와 값의 튜플을 리스트로 반환함 s = [1,2,3] s[1] = 10 s s = (1,2,3) s[1] = 10 x = 1 x = 2 l1 = [1,2,3] l1 = [4,5,6] l1 = [4,5,6] l1[0] = 10 print type(3) #정수 print type(3.3) #실수 print type('abc') #문자열 print type(None) #None 객체, 아무 값도 없다(혹은 아니다)를 나타내는 객체 print a = None print a print type(a) print type([]) #리스트 print type(()) #튜플 print type({}) #사전(dict) print type(type({})) #'type'형 a = 0 L = [1,2,3] print type(a) == type(0) print type(L) == type([]) print type(L[0]) == type(0) import types print dir(types) from types import * print type(123) == IntType print type('abc') == StringType x = y = z = 100 del x y = 200 z = 300 import sys aaa = 191919 print sys.getrefcount(aaa) bbb = object() print sys.getrefcount(bbb) print sys.getrefcount('foobar') a = 500 b = a print id(a) print id(b) print x = 1 y = 1 print id(x) print id(y) 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 c = [1,2,3] d = [1,2,3] c == d score = 90 if score >= 90: print 'Congratulations!!! ' a = 10 if a > 5: print 'Big' else: print 'Small' a = 10 if a > 5: print 'Big' else: print 'Small' n = -2 if n > 0: print 'Positive' elif n < 0: print 'Negative' else: print 'Zero' order = 'spagetti' if order == 'spam': price = 500 elif order == 'ham': price = 700 elif order == 'egg': price = 300 elif order == 'spagetti': price = 900 print price order = 'spagetti' menu = {'spam':500, 'ham':700, 'egg':300, 'spagetti':900} price = menu[order] print price a = 1 # 성공 a = 1 # 실패 if a > 1: print 'big' print 'really?' a = ['cat', 'cow', 'tiger'] for x in a: print len(x), x for x in [1,2,3]: print x, print range(10) for x in range(10): print x, sum = 0 for x in range(1, 11): sum = sum + x print sum prod = 1 for x in range(1, 11): prod = prod * x prod l = ['cat', 'dog', 'bird', 'pig'] for k, animal in enumerate(l): print k, animal t = ('cat', 'dog', 'bird', 'pig') for k, animal in enumerate(t): print k, animal d = {'c':'cat', 'd':'dog', 'b':'bird', 'p':'pig'} for k, key in enumerate(d): print k, key, d[key] for x in range(10): if x > 3: break print x print 'done' for x in range(10): if x < 8: continue print x print 'done' for x in range(10): print x, # 콤마(,) 때문에 줄이 바뀌지 않는다. else: print 'else block' print 'done' for x in range(10): break print x, else: print 'else block' print 'done' for x in range(2, 4): for y in range(2, 10): print x, '*', y, '=', x*y print count = 1 while count < 11: print count count = count + 1 sum = 0 a = 0 while a < 10: a = a + 1 sum = sum + a print sum x = 0 while x < 10: print x, # 콤마(,) 때문에 줄이 바뀌지 않는다. x = x + 1 else: print 'else block' print 'done' x = 0 while x < 10: break print x, x = x + 1 else: print 'else block' print 'done' x = 0 while x < 10: if x < 8: x = x + 1 continue else: print x, x = x + 1 print 'done' def add(a, b): return a + b print add(3, 4) print add([1,2,3], [4,5,6]) def minus(a, b): return a - b print minus(a=12, b=20) print minus(b=20, a=12) def incr(x, y=1): return x + y print incr(5) print incr(5, 10)