Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python

nctunba
May 06, 2012
510

 Python

nctunba

May 06, 2012
Tweet

Transcript

  1. Python 設計哲學 「優雅」、「明確」、「簡單」 「最好是只有一種方法來做一件事」 import this Beautiful is better than

    ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.
  2. Sample Code //c++ int age = 18; if(age < 20)

    { cout<<"You can't drive car\n"; } else { cout<<"You can drive car\n"; } cout<<“ 這句在 if 的外面 \n"; #python age = 18; if age < 20: print "You can't drive car" else: print "You can drive car“ print “ 這句在 if “ 的外面 “”” 我是註解 “””
  3. Python 物件型態 • 數字 Number • 字串 String • 串列

    List • 辭典 Dictoinary • 元組 Tuple • 布林 boolean
  4. 物件型態 - 字串 (2 /5) >>> S[0] 'p' >>> S[-1]

    'n' >>> S[0:2] 'py' >>> S[0:] 'python' >>> S[1:] 'ython'
  5. 物件型態 - 字串 (3 /5) >>> S + " is

    good!!" 'python is good!!' >>> S * 4 'pythonpythonpythonpython'
  6. 物件型態 - 字串 (4 /5) >>> S = "python" >>>

    S[0] = 'z' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment
  7. 物件型態 - 字串 (5 /5) >>>S = " python" >>>

    S.find("on") 4 >>> S.replace("p", "XYZ") 'XYZython' >>>help(str)
  8. 物件型態 - 字串 (5 /5) >>>S = " python" >>>

    S.find("on") 4 >>> S.replace("p", "XYZ") 'XYZython' >>>help(str)
  9. 物件型態 - 字串 (5 /5) >>>S = " python" >>>

    S.find("on") 4 >>> S.replace("p", "XYZ") 'XYZython' >>>help(str)
  10. 物件型態 - 串列 (1 /6) >>> L = [123, "python",

    12.3] >>> L [123, 'python', 12.3] >>> len(L) 3
  11. 物件型態 - 串列 (2 /6) >>> L = [123, "python",

    12.3] >>> L[0] 123 >>> L[1:] ['python', 12.3]
  12. 物件型態 - 串列 (3 /6) >>> L + [3, 4,

    5] [123, 'python', 12.3, 3, 4, 5] >>> L[0] = "hello world" >>> L ['hello world', 'python', 12.3]
  13. 物件型態 - 串列 (4 /6) >>> L[99] Traceback (most recent

    call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
  14. 物件型態 - 串列 (5 /6) >>> L = [123, "python",

    12.3] >>> L.append("last") >>> L [123, 'python', 12.3, 'last'] >>> L.pop() 'last‘
  15. 物件型態 - 串列 (5 /6) >>> L = [3,4,2,5,1] >>>

    L.sort() >>> L [1, 2, 3, 4, 5] >>> L.reverse() >>>L [5, 4, 3, 2, 1]
  16. 物件型態 - 串列 (6 /6) >>> L2 = [[1, 2,

    3], [4, 5, 6], [7, 8, >>> L2 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> L2[0][0] 1
  17. 物件型態 - 辭典 (1 /3) >>> D = {"python" :

    "good", "hello" : "world", "number" : 1} >>> D['python'] 'good'
  18. 物件型態 - 辭典 (2 /3) >>> info = { "name":{"first":"Bob",

    "last":"Sm "children":["John", "Peter"], "age":40}
  19. 物件型態 - 辭典 (3 /3) >>> D = {“A" :

    1, " B" : 2} >>> D.keys() ['A', 'B'] >>> D.has_key("B") True
  20. DIY >>> D = {“A" : 1, " B" :

    2, “C”:3} >>> E = {“1”:”A”, “2”: “B”, “3”:”C” D + E 是?
  21. 物件型態 -tuple >>> list = [123, "python", 12.3] >>> tuple

    = (123, "python", 12.3) >>> list[0] = 456 >>> tuple[0] = 456 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
  22. 物件型態 - 布林 >>> True True >>> true Traceback (most

    recent call last): File "<stdin>", line 1, in <module> NameError: name 'true' is not defined
  23. If 判斷式 a = 10 b = 11 if a

    < b: print " a<b" elif a == b: print " a = b" else: print “a > b"
  24. 推薦用法 D = { 'a':1, "b":2, "c":3} 較差的用法 if D.has_key(‘a’):

    print ‘haha’ 較好的用法 if ‘a’ in D: print ‘haha’
  25. 流程控制 -For(3/3) Animals = [(0, ‘dog’), (1, ‘cat’), ] for

    index, animal in Animals: print index, animal
  26. DIY D = {'a':' IndexError ', 'b':' KeyEr ror '}

    請問? D[‘c’] 是 IndexError or KeyError
  27. 基本輸入輸出 (2/2) #open 讀檔 f = open("test.txt", "r") print f.readline()

    f = open(“output.txt”, “w”) f.write(“hahahah”) f.close()
  28. 自定義函式 -def(1/ 2) >>> def func(): ... : print ‘I

    am chicken’ >>> func() I am chicken