SPARCS 파이썬 세미나 2010 -...

198
발로 하는 파이썬 세미나 1

Transcript of SPARCS 파이썬 세미나 2010 -...

Page 1: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

발로 하는파이썬 세미나

1

Page 2: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

강성훈

2

Page 3: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

http://j.mp/2010sparcspython

3

Page 4: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

시작하기 전에:

4

Page 5: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

왜?

5

Page 6: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• C• C++• 자바

6

Page 7: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• C• C++• 자바• 파이썬?

7

Page 8: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

스크립팅 언어

8

Page 9: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

빠르게 짜고빠르게 확인하고빠르게 고친다

9

Page 10: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

print "Hello, world!"

10

Page 11: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

http://python.org/

11

Page 12: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

12

Page 13: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

계산기

13

Page 14: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 3 + 4 * (5 - 6)

-1

14

Page 15: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 8/5

1

15

Page 16: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 8/5

1

>>> 8%5

3

16

Page 17: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 8.0 / 5.0

1.6000000000000001

17

Page 18: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 8.0 / 5

1.6000000000000001

>>> 8 / 5.0

1.6000000000000001

18

Page 19: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

5 → 5.0 (O)5 ← 5.0 (X)

19

Page 20: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

자료형

20

Page 21: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> "foo" + 'bar'

'foobar'

21

Page 22: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> "foo" * 4

'foofoofoofoo'

22

Page 23: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 1 + 1 == 2

True

>>> 1 + 1 == 3

False

23

Page 24: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• 참과 거짓: True, False• 정수: 1, 2, -3, -4, …• 실수: 1.0, -2.34, …• 문자열: 'x', "Hello", …• 기타 등등

24

Page 25: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 1600000 + (3.0 - 2.05) * 6300000

7585000.0000000009

25

Page 26: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

내가 니 학점을어찌 아니?

26

Page 27: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> grade = 2.05

>>> 1600000 + (3.0 - grade) * 6300000

7585000.0000000009

27

Page 28: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> grade = 2.05

>>> 1600000 + (3.0 - grade) * 6300000

7585000.0000000009

>>> grade = 2.89

>>> 1600000 + (3.0 - grade) * 6300000

2292999.9999999991

28

Page 29: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

변수

29

Page 30: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> grade = 3.5

>>> 1600000 + (3.0 - grade) * 6300000

-1550000.0

30

Page 31: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> grade = 3.5

>>> if grade < 3.0:

... 1600000 + (3.0 - grade) * 6300000

... else:

... 1600000

...

1600000

31

Page 32: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> grade = 1.86

>>> if grade < 3.0:

... 1600000 + (3.0 - grade) * 6300000

... else:

... 1600000

...

8782000.0

32

Page 33: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> grade = 1.86

>>> if grade < 2.0:

... 1600000 + (3.0 - 2.0) * 6300000

... elif grade < 3.0:

... 1600000 + (3.0 - grade) * 6300000

... else:

... 1600000

...

7900000.0

33

Page 34: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> grade = 1.86

>>> if grade < 3.0:

... if grade < 2.0:

... 1600000 + (3.0-2.0) * 6300000

... else:

... 1600000 + (3.0-grade) * 6300000

... else:

... 1600000

...

7900000.0

34

Page 35: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> for i in range(13):

... print i+1,'인의아해가거리를질주하오.'

...

1 인의 아해가 거리를 질주하오.

2 인의 아해가 거리를 질주하오.

(생략)

13 인의 아해가 거리를 질주하오.

35

Page 36: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

for i in range(1,101):

one = (i%10==3 or i%10==6 or i%10==9)

ten = (i/10==3 or i/10==6 or i/10==9)

if one and ten:

print '짝짝'

elif one or ten:

print '짝'

else:

print i

36

Page 37: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

너무 복잡하네

37

Page 38: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

Refactoring?

38

Page 39: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

# x가 3, 6, 9이면 True 아니면 False

def is369(x):

return x==3 or x==6 or x==9

39

Page 40: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def is369(x):

return x==3 or x==6 or x==9

for i in range(1,101):

one = is369(i%10)

ten = is369(i/10)

if one and ten: print '짝짝'

elif one or ten: print '짝'

else: print i

40

Page 41: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def is369(x):

return x==2 or x==3 or x==5 or x==7

for i in range(1,101):

one = is369(i%10)

ten = is369(i/10)

if one and ten: print '짝짝'

elif one or ten: print '짝'

else: print i

41

Page 42: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def needsclap(x):

return x==2 or x==3 or x==5 or x==7

for i in range(1,101):

one = needsclap(i%10)

ten = needsclap(i/10)

if one and ten: print '짝짝'

elif one or ten: print '짝'

else: print i

42

Page 43: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

“함수”단순히 코드를 묶는 것이 아닌

43

Page 44: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

range도 함수인가요?

44

Page 45: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> range(10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

45

Page 46: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> [1, 2, 3]

[1, 2, 3]

>>> [4, 5] + ['hello?']

[4, 5, 'hello?']

>>> [41, 42] * 3

[41, 42, 41, 42, 41, 42]

>>> []

[]

46

Page 47: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> (1, 2, 3)

(1, 2, 3)

>>> (4, 5) + ('hello?',)

(4, 5, 'hello?')

>>> (41, 42) * 3

(41, 42, 41, 42, 41, 42)

>>> ()

()

47

Page 48: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> a = [1,2,3]

>>> a[0] + a[1] + a[2]

6

>>> a[1] = 5

>>> a

[1, 5, 3]

48

Page 49: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

49

a

a[0]a[-9]

a[1]a[-8]

a[2]a[-7]

a[3]a[-6]

a[4]a[-5]

a[5]a[-4]

a[6]a[-3]

a[7]a[-2]

a[8]a[-1]

Page 50: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> b = (1,2,3)

>>> b[0] + b[1] + b[2]

6

>>> b[1] = 5

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: 'tuple' object does notsupport item assignment

50

Page 51: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> b = (1,2,3)

>>> b = b[:1] + (5,) + b[2:]

>>> b

(1, 5, 3)

51

Page 52: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

52

a[3:8]

a[0]a[-9]

a[1]a[-8]

a[2]a[-7]

a[3]a[-6]

a[4]a[-5]

a[5]a[-4]

a[6]a[-3]

a[7]a[-2]

a[8]a[-1]

Page 53: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

53

a[-3:0:-2]

a[0]a[-9]

a[1]a[-8]

a[2]a[-7]

a[3]a[-6]

a[4]a[-5]

a[5]a[-4]

a[6]a[-3]

a[7]a[-2]

a[8]a[-1]

Page 54: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

사실 이런 거몰라도 돼요

54

Page 55: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

55

a[::-1]

a[0]a[-9]

a[1]a[-8]

a[2]a[-7]

a[3]a[-6]

a[4]a[-5]

a[5]a[-4]

a[6]a[-3]

a[7]a[-2]

a[8]a[-1]

Page 56: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

◀◀

56

Page 57: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

리스트와 튜플을함께 쓸 이유

57

Page 58: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

birthdays = [

('강성훈', 1987, 9, 13),

('정재성', 1987, 2, 23),

('김준기', 1987, 5, 12),

]

58

Page 59: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

for entry in birthdays:

name, year, month, day = entry

print name, month, '월', day, '일생'

59

Page 60: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

for name, year, month, day in birthdays:

print name, month, '월', day, '일생'

60

Page 61: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

for name, _, month, day in birthdays:

print name, month, '월', day, '일생'

61

Page 62: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

for name, _, month, day in birthdays:

print '%s - %d월 %d일생' % \

(name, month, day)

62

Page 63: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

for name, _, month, day in birthdays:

print '{0} - {1}월 {2}일생'.format(

name, month, day)

63

Page 64: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

메소드

64

Page 65: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

자료형이나 값과연관된 함수

65

Page 66: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

formatfun = '{0} - {1}월 {2}일생'.format

for name, _, month, day in birthdays:

print formatfunc(name, month, day)

66

Page 67: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

자료형 = 값

67

Page 68: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> type(4)

<type 'int'>

>>> type('hello?')

<type 'str'>

>>> type([1,2,3])

<type 'list'>

68

Page 69: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> type(4) == int

True

>>> type('hello?') == str

True

>>> type([1,2,3]) == list

True

69

Page 70: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> str.format

<method 'format' of 'str' objects>

70

Page 71: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

fmt = '{0} - {1}월 {2}일생'

for name, _, month, day in birthdays:

# 엄밀하게는…

print str.format(fmt, name,

month, day)

71

Page 72: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 'hello'.upper()

'HELLO'

>>> _.lower()

'hello'

>>> ' '.join(['we','are','the','world'])

'we are the world'

>>> _.split(' ')

['we', 'are', 'the', 'world']

72

Page 73: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> len('hello')

5

>>> str(42)

'42'

>>> '%d' % 42

'42'

73

Page 74: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> print str(1/5.0)

0.2

>>> 1/5.0

0.20000000000000001

>>> print repr(1/5.0)

0.20000000000000001

74

Page 75: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def names(birthdays):

result = []

for name, _, _, _ in birthdays:

result.append(name)

result.sort()

return result

75

Page 76: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> names(birthdays)

['\xb0\xad\xbc\xba\xc8\xc6','\xb1\xe8\xc1\xd8\xb1\xe2','\xc1\xa4\xc0\xe7\xbc\xba']

76

Page 77: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> for name in names(birthdays):

... print name

...

강성훈

김준기

정재성

77

Page 78: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

원래 세상다 이런 거지

78

Page 79: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printbirthdays(birthdays):

for name, _, month, day in birthdays:

print '%s - %d월 %d일생' % \

(name, month, day)

79

Page 80: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def filterbyyear(birthdays, targetyear):

result = []

for entry in birthdays:

_, year, _, _ = entry

if year == targetyear:

result.append(entry)

return result

80

Page 81: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def filterbyyear(birthdays, targetyear):

def func(entry):

_, year, _, _ = entry

return year == targetyear

return filter(func, birthdays)

81

Page 82: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def filterbyyear(birthdays, targetyear):

def func((name, year, month, day)):

return year == targetyear

return filter(func, birthdays)

82

Page 83: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def filterbyyear(birthdays, targetyear):

return filter(

lambda (n,y,m,d): y==targetyear,

birthdays)

83

Page 84: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def filterbyyear(birthdays, targetyear):

return [(name, year, month, day)

for name, year, month, day

in birthdays

if year == targetyear]

84

Page 85: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

85

Page 86: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

스크립팅 언어?

86

Page 87: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

스크립팅 언어

87

Page 88: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

“스크립팅핛때 쓰면스크립팅 언어”

88

Page 89: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

목표

89

Page 90: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

생년월일 검색하는프로그램

90

Page 91: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• 보통은 이름과 생년월일을모두 보여 준다

• 이름만 보여 줄 수도 있다• 이름으로 검색핛 수 있다• 생년별 목록도 볼 수 있다

91

Page 92: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

birthdays = [

('강성훈', 1987, 9, 13),

('정재성', 1987, 2, 23),

('김준기', 1987, 5, 12),

('안병욱', 1989, 10, 14),

('강철', 1990, 3, 11),

('유수형', 1991, 3, 13),

('조유정', 1990, 4, 18),

('김도국', 1990, 3, 11),

]

92

Page 93: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

데이터는 data.py

프로그램은 birthday.py

93

Page 94: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

출력핛 때는꼭 print 명령을

써야 핚다

94

Page 95: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

95

Page 96: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

# coding=cp949 ← utf-8이어야 할 수도 있음

birthdays = [

('강성훈', 1987, 9, 13),

('정재성', 1987, 2, 23),

('김준기', 1987, 5, 12),

('안병욱', 1989, 10, 14),

('강철', 1990, 3, 11),

('유수형', 1991, 3, 13),

('조유정', 1990, 4, 18),

('김도국', 1990, 3, 11),

]96

Page 97: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

원래 세상다 이런 거지

(2)

97

Page 98: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> import data

>>> print '%d명' % len(data.birthdays)

8명

98

Page 99: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

# coding=cp949

import data

print '%d명' % len(data.birthdays)

99

Page 100: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

# coding=cp949

from data import birthdays

print '%d명' % len(birthdays)

100

Page 101: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

모듈

101

Page 102: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

102

import data

data.py

birthday.py

data

__main__

Page 103: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

103

import birthday

import data

birthday.py

persons.py

birthday

__main__

data.py data

Page 104: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

# coding=cp949

from data import birthdays

def main():

print '%d명' % len(birthdays)

if __name__ == '__main__': main()

104

Page 105: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

105

import data

data.py

birthday.py

data

__main__

data.pyc

Page 106: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def main():

choice = showmenu()

if choice == 1:

printnames(birthdays)

elif choice == 2:

printbirthdays(birthdays)

elif choice == 3:

printbyname(birthdays)

elif choice == 4:

printbyyear(birthdays)

106

Page 107: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printnames(birthdays):

pass # 이름만 출력할 것

def printbirthdays(birthdays):

pass # 이름과 생년월일을 출력할 것

def printbyname(birthdays):

pass # 이름을 입력받아서

# 해당하는 생년월일을 출력

def printbyyear(birthdays):

pass # 생년을 입력받아서

# 해당하는 목록을 출력107

Page 108: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printnames(birthdays):

print 'TODO: 이름 목록을 출력'

def printbirthdays(birthdays):

print 'TODO: 이름과 생년월일 출력'

def printbyname(birthdays):

print ('TODO: 이름을 입력받아 '

'해당하는 생년월일을 출력')

def printbyyear(birthdays):

print ('TODO: 생년을 입력받아 '

'해당하는 목록을 출력')

108

Page 109: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def showmenu():

print '---- 메뉴 ----'

print '1. 이름 보기'

print '2. 이름과 생년월일 보기'

print '3. 이름으로 찾기'

print '4. 생년으로 찾기'

return input('>>> ')

109

Page 110: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

---- 메뉴 ----

1. 이름 보기

2. 이름과 생년월일 보기

3. 이름으로 찾기

4. 생년으로 찾기

>>> 2

TODO: 이름과 생년월일 출력

110

Page 111: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

---- 메뉴 ----

1. 이름 보기

2. 이름과 생년월일 보기

3. 이름으로 찾기

4. 생년으로 찾기

>>> 5

111

Page 112: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

---- 메뉴 ----

1. 이름 보기

2. 이름과 생년월일 보기

3. 이름으로 찾기

4. 생년으로 찾기

>>> 3*4-11

TODO: 이름 목록을 출력

112

Page 113: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

---- 메뉴 ----

1. 이름 보기

2. 이름과 생년월일 보기

3. 이름으로 찾기

4. 생년으로 찾기

>>> end

Traceback (most recent call last):

(생략)

File "<string>", line 1, in <module>

NameError: name 'end' is not defined

113

Page 114: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

---- 메뉴 ----

1. 이름 보기

2. 이름과 생년월일 보기

3. 이름으로 찾기

4. 생년으로 찾기

>>> [엔터]

Traceback (most recent call last):

(생략)

File "<string>", line 0

^

SyntaxError: unexpected EOF while parsing114

Page 115: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

input으로는안 된다

115

Page 116: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> dir()

['__builtins__', '__doc__', '__name__','__package__']

>>> dir(__builtins__)

['ArithmeticError', 'AssertionError',(…생략…), 'xrange', 'zip']

116

Page 117: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> [x for x in dir(__builtins__)

... if 'input' in x]

['input', 'raw_input']

117

Page 118: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> help(raw_input)

Help on built-in function raw_input in module __builtin__:

raw_input(...)

raw_input([prompt]) -> string

Read a string from standard input. The trailing newline is

stripped. If the user hits EOF (Unix: Ctl-D, Windows:

Ctl-Z+Return), raise EOFError. On Unix, GNU readline is

used if enabled. The prompt string, if given, is printed

without a trailing newline before reading.

118

Page 119: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

모르는 게 있으면우선 help를!

119

Page 120: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def inputnum(prompt):

return input(prompt)

def showmenu():

print '---- 메뉴 ----'

print '1. 이름 보기'

print '2. 이름과 생년월일 보기'

print '3. 이름으로 찾기'

print '4. 생년으로 찾기'

return inputnum('>>> ')

120

Page 121: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def inputnum(prompt):

return int(raw_input(prompt))

121

Page 122: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def inputnum(prompt):

while True: # 무한반복 (사용에 주의!)

line = raw_input(prompt)

try:

return int(line)

except:

print '숫자를 입력하시죠.'

122

Page 123: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

예외 (exception)“일반적이지 않다”

123

Page 124: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> 3/0

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ZeroDivisionError: integer division ormodulo by zero

124

Page 125: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> asdf

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'asdf' is not defined

125

Page 126: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> int('notanumber')

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: invalid literal for int()with base 10: 'notanumber'

126

Page 127: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

---- 메뉴 ----

1. 이름 보기

2. 이름과 생년월일 보기

3. 이름으로 찾기

4. 생년으로 찾기

>>> exit

숫자를 입력하시죠.

>>> quit

숫자를 입력하시죠.

>>> 나 좀 나가게 해 줘 ㅆㅂ

숫자를 입력하시죠.

127

Page 128: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• 나가는 메뉴가 없다• Ctrl-C 같은 강제 종료키도try~except가 먹어버린다

128

Page 129: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def showmenu():

print '---- 메뉴 ----'

print '1. 이름 보기'

print '2. 이름과 생년월일 보기'

print '3. 이름으로 찾기'

print '4. 생년으로 찾기'

print '0. 끝내기'

return inputnum('>>> ')

129

Page 130: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def inputnum(prompt):

while True:

line = raw_input(prompt)

try:

return int(line)

except ValueError:

print '숫자를 입력하시죠.'

130

Page 131: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

방어적 프로그래밍

131

Page 132: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def names(birthdays):

result = []

for name, _, _, _ in birthdays:

result.append(name)

result.sort()

return result

def printnames(birthdays):

print ', '.join(names(birthdays))

132

Page 133: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printbirthdays(birthdays):

for name, _, month, day in birthdays:

print '%s - %d월 %d일생' % \

(name, month, day)

133

Page 134: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printbyname(birthdays):

name = raw_input('이름을입력하세요: ')

for n, y, m, d in birthdays:

if n == name:

print '%s - %d월 %d일생' % \

(n, m, d)

134

Page 135: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printbyname(birthdays):

name = raw_input('이름을입력하세요: ')

count = 0

for n, y, m, d in birthdays:

if n == name:

print '%s - %d월 %d일생' % \

(n, m, d)

count += 1

if count == 0:

print '그런 사람이 없습니다.'

135

Page 136: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

똑같은 기능에똑같은 성능이면갂결핚 게 낫다

136

Page 137: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def filterbyname(birthdays, targetname):

return [(name, year, month, day)

for name, year, month, day

in birthdays

if name == targetname]

def printbyname(birthdays):

name = raw_input('이름을입력하세요: ')

filtered = filterbyname(birthdays,

name)

printbirthdays(filtered)137

Page 138: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def filterbyyear(birthdays, targetyear):

return [(name, year, month, day)

for name, year, month, day

in birthdays

if year == targetyear]

def printbyyear(birthdays):

year = inputnum('생년을 입력하세요: ')

filtered = filterbyyear(birthdays,

year)

printbirthdays(filtered)138

Page 139: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

---- 메뉴 ----

1. 이름 보기

2. 이름과 생년월일 보기

3. 이름으로 찾기

4. 생년으로 찾기

0. 끝내기

>>> 4

생년을 입력하세요: 1990

강철 - 3월 11일생

조유정 - 4월 18일생

김도국 - 3월 11일생

139

Page 140: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

새 기능을 만들려면?

140

Page 141: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def main():

choice = showmenu()

if choice == 1:

printnames(birthdays)

elif choice == 2:

printbirthdays(birthdays)

elif choice == 3:

printbyname(birthdays)

elif choice == 4:

printbyyear(birthdays)

141

def showmenu():

print '---- 메뉴 ----'

print '1. 이름 보기'

print '2. 이름과 생년월일 보기'

print '3. 이름으로 찾기'

print '4. 생년으로 찾기'

print '0. 끝내기'

return inputnum('>>> ')

여기다새 조건을 추가

여기다새 메뉴를 추가

Page 142: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

하나의 함수에하나의 기능을

142

Page 143: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def showmenu():

print '---- 메뉴 ----'

print '1. 이름 보기'

print '2. 이름과 생년월일 보기'

print '3. 이름으로 찾기'

print '4. 생년으로 찾기'

print '0. 끝내기'

choice = inputnum('>>> ')

if choice == 1: return printnames

elif choice == 2: return printbirthdays

elif choice == 3: return printbyname

elif choice == 4: return printbyyear143

Page 144: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

CHOICES = {1: printnames, 2: printbirthdays,

3: printbyname, 4: printbyyear}

def showmenu():

print '---- 메뉴 ----'

print '1. 이름 보기'

print '2. 이름과 생년월일 보기'

print '3. 이름으로 찾기'

print '4. 생년으로 찾기'

print '0. 끝내기'

choice = inputnum('>>> ')

if choice in CHOICES:

return CHOICES[choice]144

Page 145: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def main():

routine = showmenu()

if routine: routine(birthdays)

145

Page 146: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def main():

while True:

routine = showmenu()

if not routine: return

routine(birthdays)

print

146

Page 147: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• main: 시작 함수• showmenu: 메뉴를 보여줌• inputnum: 숫자를 입력 받음• names: 목록에서 이름만 반홖• filterby…: 목록을 조건에 따라 걸러냄• printbirthdays: 목록을 출력• print…: 각 메뉴를 구현함

147

Page 148: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

여전히 중복이있지 않나요?

148

Page 149: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

for name,_,_,_ in birthdays: …

for name,_,month,day in birthdays: …

[(name,year,month,day)

for name,year,month,day in birthdays

if …]

149

Page 150: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

순서를 하나라도삐끗핚다면?

150

Page 151: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

새 자료형을 만들기

151

Page 152: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

class person(object):

def __init__(self, name, year,

month, day):

self.name = name

self.year = year

self.month = month

self.day = day

152

Page 153: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

자료형과 연관된함수?

153

Page 154: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

즉 그냥 함수(첫 인자만 빼고)

154

Page 155: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

class person(object):

def __init__(you, name, year,

month, day):

you.name = name

you.year = year

you.month = month

you.day = day

155

Page 156: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> p = person('강성훈', 1987, 9, 13)

>>> p

<__main__.person object at 0x00B12110>

>>> print p.name

강성훈

>>> p.year, p.month, p.day

(1987, 9, 13)

156

Page 157: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

class person(object):

def __init__(self, name, year,

month, day):

self.name = name

self.year = year

self.month = month

self.day = day

def __str__(self):

return '%s - %d월 %d일생' % \

(self.name, self.month,

self.day)157

Page 158: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> print str(p)

<__main__.person object at 0x00B12110>

158

Page 159: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

파이썬 메모리의구조

159

Page 160: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

160

person p

환경

미지의 세계

자료형 person

(클래스)name

값 person

yearmonth

day

미지의세계

정수 1987미지의세계

정수 9

미지의세계

정수 13

미지의세계

문자열 '강성훈'

Page 161: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

161

person p

환경

미지의 세계

자료형 person

(클래스)

name

값 person

yearmonth

day미지의 세계

자료형 person

Page 162: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

모든 것은

요 레퍼런스로

162

Page 163: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> a = [[]] * 3

>>> a

[[], [], []]

>>> a[0].append(4)

>>> a

[[4], [4], [4]]

163

Page 164: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> a = [[] for i in range(3)]

>>> a

[[], [], []]

>>> a[0].append(4)

>>> a[1].append(5)

>>> a[2].append(6)

>>> a

[[4], [5], [6]]

164

Page 165: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

제자리에서 변경이가능핚가?

165

Page 166: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• 그런 것: 리스트, 사전• 안 그런 것: 문자열, 튜플

166

Page 167: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

하여튼…

167

Page 168: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

168

망한 값들

값 person

(클래스)* * * *

person p

환경

미지의 세계

자료형 person

미지의 세계

자료형 person 값 person

(클래스)* * * *

Page 169: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

169

person p

환경

미지의 세계

자료형 person 값 person

(클래스)* * * *

Page 170: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

가비지 컬렉션= 망핚 값 처리하기

170

Page 171: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> p = person('강성훈', 1987, 9, 13)

>>> print p.__str__()

강성훈 - 9월 13일생

>>> print str(p)

강성훈 - 9월 13일생

>>> print p

강성훈 - 9월 13일생

171

Page 172: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

마무리

172

Page 173: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

# coding=cp949

class person(object):

def __init__(self, …): …

def __str__(self, …): …

birthdays = [

person('강성훈', 1987, 9, 13),

person('정재성', 1987, 2, 23),

person('김준기', 1987, 5, 12),

]173

Page 174: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printnames(birthdays):

names = [p.name for p in birthdays]

names.sort()

print ', '.join(names)

def printbirthdays(birthdays):

for p in birthdays: print p

174

Page 175: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

def printbyname(birthdays):

name = raw_input('이름을입력하세요: ')

filtered = [p for p in birthdays

if p.name == name]

printbirthdays(filtered)

def printbyyear(birthdays):

year = inputnum('생년을 입력하세요: ')

filtered = [p for p in birthdays

if p.year == year]

printbirthdays(filtered)175

Page 176: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

더 고칠 거리를생각해 보세요!

176

Page 177: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

끝…?

177

Page 178: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

세상 모든 일이이렇게 잘 풀리짂

않겠지만…

178

Page 179: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• 리팩토링• 테스팅• 디버깅• 문서화 ← 이건 안했지만

179

Page 180: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

모듈을 꼭 우리만만들란 법이 있나

180

Page 181: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

“바퀴를 재발명하기”

181

Page 182: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

182

Page 183: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> import sys

>>> sys.version

'2.6.2 (r262:71605, Apr 14 2009,22:40:02) [MSC v.1500 32 bit (Intel)]'

183

Page 184: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> import math

>>> math.sqrt(2)

1.4142135623730951

>>> 2 ** 0.5

1.4142135623730951

>>> math.factorial(16)

20922789888000L

184

Page 185: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> import datetime

>>> datetime.date.today()

datetime.date(2010, 3, 12)

>>> _.weekday()

4

>>> print ['월','화','수','목','금',

... '토','일'][_]

185

Page 186: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> import random

>>> menus = ['짜장', '짬뽕', '짜짬면']

>>> print menus[random.randint(0,

... len(menus)-1)]

짜장

>>> print random.choice(menus)

짬뽕

186

Page 187: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

>>> import urllib

>>> for line in urllib.urlopen(

... 'http://www.census.gov/ipc/'

... 'www/popclockworld.html'):

... if 'worldnumber' in line:

... print line[45:-14]

...

6,807,870,286

187

Page 188: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

188

Page 189: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

레퍼런스는 정독해도부족함이 없습니다

189

Page 190: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

그 밖에Numpy라거나Django라거나

Pyglet이라거나…190

Page 191: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

안 다룬 것들

191

Page 192: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

• 클래스에 대핚 자세핚 내용• set, unicode 같은 자료형들• 외부 라이브러리 쓰기• 패키지 다루기• 그 밖에 파이썬을 쓰면서 필요

하게 될 수많은 조언들

192

Page 193: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

두 시갂 안에 하긴좀 벅찬지라…

193

Page 194: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

질문과 답변?

194

Page 195: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

감사합니다.

195

너무 길어서정말로 죄송…

Page 196: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

196

Page 197: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

슬라이드 목차#1 시작#4 왜 파이썬을 배우는가?#13 계산기로 쓰기, 자료형#25 변수, 조건문, 반복문#36 리팩토링, 함수#44 리스트, 튜플#58 메소드, 값으로서의 자료형#72 내장 함수 및 메소드#86 아젠다 설정#92 모듈#106 생년월일 프로그램의 뼈대

#116 내장 도움말#120 리팩토링 (2), 방어적 프로

그래밍, 예외#132 생년월일 프로그램의 뼈대

(2)#140 리팩토링 (3)#148 클래스#159 파이썬 메모리의 구조#172 생년월일 프로그램의 마무

리#180 파이썬 내장 라이브러리#191 안 다룬 것들

197

Page 198: SPARCS 파이썬 세미나 2010 - mearie.orgmearie.org/documents/how-not-to-teach-python/seminar.pdf · 파이썬 세미나 1. 강성훈 2 ...

표기 컨벤션

• 코드 및 대화식 홖경은 노란 배경으로,• 프로그램 출력은 파란 배경으로,• 직접 입력하지 않은 내용은 옅은 색으로,• 이벆 슬라이드에 처음 등장하는 문법·심볼은

붉은 색으로, (단, 공백 등은 별도 표시 안 함)• 파이썬 프롬포트 및 예약어는 굵게 표기했음.

198