블로그 이미지
No pain, no gain!
lepoussin

Tag

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
  • total
  • today
  • yesterday
03-28 18:25
2007. 2. 20. 13:35 Lecture/열혈강의 Python
1. 변수명 및 예약어
  1) 변수명 : [a-zA-Z_][a-zA-Z0-9_]*

  2) 예약어
and         del         from       not      while   
as           elif         global     or         with    
assert     else       if             pass    yield   
break      except   import     print             
class       exec       in            raise             
continue  finally     is            return            
def          for          lambda   try


2. 파이썬 기초문
  1) 주석문 : # 다음에 나오는 문자들
import sys # 주석

  2) 연속 라인
>>> if (a == 1) and
SyntaxError: invalid syntax
>>> if (a == 1) and \
    (b == 3):
 print  'connected lines'


  3) 치환문 : 우변의 객체 혹은 을 좌변의 이름에 할당하는 것
>>> c, d = 3, 4 # 여러 개를 한꺼번에 치환
>>> x = y = z = 0 # 여러 개를 같은 값 0으로 치환한다.
>>> e = 3.5; f = 5.6 # ; 로 문들을 구분
>>> e, f = f, e # swap
>>> print e, f
5.6 3.5

  4) 확장 치환문
+=      -=      *=      /=      //=     %=
&=      |=      ^=      >>=     <<=     **=

  5) 이름과 객체

3. 문자열로 된 파이썬 코드 실행하기
  1) eval() : 문자열로된 파이썬 식(expression)을 실행
>>> a = 1
>>> a = eval('a + 4')
>>> a
5
  2) exec() : 문자열로 된 문(statement)을 수행
>>> a = 5
>>> exec 'a = a + 4'
>>> a
9

  3) compile() : 문자열을 컴파일하여 파이썬 코드를 리턴

>>> # 식
>>> code = compile('a + 1', '<string>', 'eval')
>>> a = 1
>>> for k in range(10):
 a = eval(code)
>>> a
11

>>> # 단일문
>>> code = compile('a = a + 1', '<string>', 'single')
>>> a = 1
>>> for k in range(10):
 exec code
>>> a
11

>>> # 복합문
>>> s = '''
a = 1
for k in range(10):
 a = a + 1
print a
'''
>>> code = compile(s, '<string>', 'exec')
>>> exec code
11

4. 콘솔 입ㆍ출력
  1) 콘솔 입력
>>> name = raw_input('name?') # 문자열 입력
name?홍순현
>>> print name
홍순현

>>> # 정수나 실수 등의 값 입력
>>> k = int(raw_input('int : '))
int : 89
>>> k
89
>>> 89
89

>>> i = input('int : ')
int : 45
>>> i
45

  2) 콘솔 출력
>>> print 4+5, 4-2
9 2
>>> print 1; print 2
1
2
>>> print 1,; print 2
1 2

5. 자료형의 종류 : 수치형, 문자열, 리스트, 사전, 튜플

6. 변경 가능성
  1) 변경 가능하다(Mutable) : 리스트, 사전
  2) 변경 가능하지 않다(Immutable) : 숫자, 문자열, 튜플

7. 자료형 확인과 기타 자료형 : type(), types 모듈 이용
>>> from types import *
>>> type(123) == IntType
True
>>> type('abc') == StringType
True


8. 메모리 관리 : 자동적으로 쓰레기 수집(Garbage Collection) - 객체의 Reference Count 값 이용

9. 파이썬 제어문
  1) if문

>>> order = 'spagetti'
if order == 'spam':
 price = 500
elif order == 'ham':
 price = 700
elif order == 'egg':
 price = 300
elif order == 'spagetti':
 price = 900
else:
 price = 100
>>> print price

100
  2) for문

>>> for x in range(2, 4):
 for y in range(2, 10):
  print x, '*', y, '=', x*y
 print

2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18

3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27

3) while
>>> while a < 10:
 a = a + 1
 if a < 3: continue
 if a > 10: break
else:
 print 'else block'
else block


10. 함수

>>> def add(a, b):
 return a+b
>>> print add(3, 4)
7
>>> print add([1, 2, 3], [4, 5, 6])
[1, 2, 3, 4, 5, 6]

posted by lepoussin