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

Django Girls Taipei: Python

Django Girls Taipei: Python

Python 教學(用於 Django Girls Taipei)

Tzu-ping Chung

May 30, 2015
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. C:\Users\Tzu-­‐ping  Chung>python3   Python  3.4.1  (v3.4.1:c0e311e010fc,  May  18   2014,

     10:38:22)  [MSC  v.1600  32  bit  (Intel)]   on  win32   Type  "help",  "copyright",  "credits"  or   "license"  for  more  information.   >>>
  2. uranusjr@ubuntu:~$  python3   Python  3.4.0  (default,  Apr  11  2014,  13:05:11)

        [GCC  4.8.2]  on  linux   Type  "help",  "copyright",  "credits"  or   "license"  for  more  information.   >>>  
  3. uranusjr:~  uranusjr$  python3   Python  3.4.1  (default,  Aug  24  2014,

     21:32:40)     [GCC  4.2.1  Compatible  Apple  LLVM  5.1   (clang-­‐503.0.40)]  on  darwin   Type  "help",  "copyright",  "credits"  or   "license"  for  more  information.   >>>
  4. >>>  4  *  5   ???   >>>  40  /

     (7  -­‐  3)  *  2   ???   >>>  2  **  3   ???
  5. >>>  'I  am  a  Django  Girl'   'I  am  a

     Django  Girl'   >>>  'Hello  '  +  'Django'   'Hello  Django'   >>>  'Django  '  *  3   'Django  Django  Django  '   >>>  len('Django')   6
  6. >>>  len(304023)   Traceback  (most  recent  call  last):    

     File  "<stdin>",  line  1,  in  <module>   TypeError:  object  of  type  'int'  has  no  len()
  7. >>>  len(304023)   Traceback  (most  recent  call  last):    

     File  "<stdin>",  line  1,  in  <module>   TypeError:  object  of  type  'int'  has  no  len()
  8. >>>  int('Hello!')   Traceback  (most  recent  call  last):    

     File  "<stdin>",  line  1,  in  <module>   ValueError:  invalid  literal  for  int()  with   base  10:  'Hello!'
  9. >>>  pet  =  'Kitty'   >>>  pet   'Kitty'  

    >>>  len(pet)   5 >>>  seven  =  7   >>>  six  =  6   >>>  answer  =  seven  *  six   >>>  answer   42
  10. >>>  pet  =  'Kitty'   >>>  pat   Traceback  (most

     recent  call  last):      File  "<stdin>",  line  1,  in  <module>   NameError:  name  'pat'  is  not  defined
  11. >>>  []   []   >>>  lottery  =  [59,  42,

     30,  19,  12,  3]   >>>  len(lottery)   6
  12. >>>  lottery[0]   59   >>>  lottery[1]   42  

    >>>  lottery[-­‐1]   ???   >>>  lottery[1000]   ???
  13. >>>  lottery  =  [59,  42,  30,  19,  12,  3]  

    >>>  print(lottery[1000])   Traceback  (most  recent  call  last):      File  "<stdin>",  line  1,  in  <module>   IndexError:  list  index  out  of  range
  14. >>>  lottery  =  [59,  42,  30,  19,  12,  3]  

    >>>  lottery[1]  =  22   >>>  lottery   [59,  22,  30,  19,  12,  3]  
  15. >>>  {}   {}   >>>  my_information  =  {  

    ...        'name':  'Pusheen  the  Cat',   ...        'country':  'USA',   ...        'favorite_numbers':  [42,  105]}   >>>  len(my_information)   3
  16. >>>  my_information['age']   Traceback  (most  recent  call  last):    

     File  "<stdin>",  line  1,  in  <module>   KeyError:  'age'
  17. >>>  my_information   {'name':  'Pusheen  the  Cat',  'country':   'USA',

     'favorite_numbers':  [42,  105]}   >>>  my_information['color']  =  'gray'   >>>  len(my_information)   4   >>>  del  my_information['farovite_numbers']   >>>  my_information   {'name':  'Pusheen  the  Cat',  'country':   'USA',  'color':  'gray'}
  18. >>>  5  >  2   True   >>>  3  <

     1   False   >>>  5  >  2  *  3   False   >>>  1  ==  1   True
  19. >>>  6  >=  12  /  2   True   >>>

     3  <=  2   False   >>>  6  >  2  and  2  <  3   True   >>>  3  >  2  and  2  <  1   False   >>>  3  >  2  or  2  <  1   True
  20. >>>  'Sherlock'  >  'John'   True   >>>  1  <

     'Django'   Traceback  (most  recent  call  last):      File  "<stdin>",  line  1,  in  <module>   TypeError:  unorderable  types:  int()  <   str()
  21. >>>  is_cool  =  True   >>>  is_cool   True  

    >>>  can_go_home  =  2  >  5   >>>  can_go_home   False
  22. >>>  True  and  True   ???   >>>  False  and

     True   ???   >>>  True  or  1  ==  1   ???   >>>  1  !=  2   ???
  23. $  python3  my_program.py   File  "my_program.py",  line  2    

                   ^   SyntaxError:  unexpected  EOF   while  parsing
  24. if  3  >  2:          print('It  works!')

      㔋⦐瑠涯 侄箺銲岣䠐♧♴㷷㆞涸箟鰿㐻剣鏣㹀䧭 tab with spaces ㄤ tab size = 4
  25. if  2  >  5:          print('2  >

     5')   else:          print('2  <=  5') $  python3  my_program.py   2  <=  5  
  26. if  2  >  5:          print('2  >

     5')   elif  2  <  5:          print('2  <  5')   else:          print('2  ==  5') $  python3  my_program.py   2  <  5  
  27. def  hi():          print('Hi  there!')    

         print('How  are  you?')   $  python3  my_program.py  
  28. def  hi():          print('Hi  there!')    

         print('How  are  you?')   hi() $  python3  my_program.py   Hi  there!   How  are  you?
  29. def  hi(name):          print('Hi'  +  name  +'!')

             print('How  are  you?')   hi()
  30. $  python3  my_program.py   Traceback  (most  recent  call  last):  

    File  "my_program.py",  line  4,  in        hi()   TypeError:  hi()  missing  1  required   positional  argument:  'name'
  31. def  hi(name):          print('Hi'  +  name  +'!')

             print('How  are  you?')   hi('Pusheen') $  python3  my_program.py   Hi  Pusheen!   How  are  you?
  32. class  Cat:          def  meow(self):    

                 print('Meow!')  
  33. class  Cat:          def  meow(self):    

                 print('Meow!')   pusheen  =  Cat()   pusheen.meow() $  python3  cat.py   Meow!
  34. class  Cat:          def  __init__(self,  name):  

                   self.name  =  name          def  meow(self):                  print(self.name  +  '  Meow!')   pusheen  =  Cat()   pusheen.meow()
  35. $  python3  cat.py   Traceback  (most  recent  call  last):  

       File  "cat.py",  line  8,  in            pusheen  =  Cat()   TypeError:  __init__()  missing  1  required   positional  argument:  'name'
  36. class  Cat:          def  __init__(self,  name):  

                   self.name  =  name          def  meow(self):                  print(self.name  +  '  Meow!')   pusheen  =  Cat()   pusheen.meow()  
  37. class  Cat:          def  __init__(self,  name):  

                   self.name  =  name          def  meow(self):                  print(self.name  +  '  Meow!')   pusheen  =  Cat('Pusheen')   pusheen.meow()   $  python3  cat.py   Pusheen  Meow!  
  38. class  Cat:          def  __init__(self,  name):  

                   self.name  =  name          def  meow(self):                  print(self.name  +  '  Meow!')   pusheen  =  Cat('Pusheen')   pusheen.meow()   print(pusheen) $  python3  cat.py   Pusheen  Meow!   <__main__.Cat  object  at  0x1006f15c0>
  39. class  Cat:          def  __init__(self,  name):  

                   self.name  =  name          def  __str__(self):                  return  'Cat:  '  +  self.name          def  meow(self):                  print(self.name  +  '  Meow!')   pusheen  =  Cat('Pusheen')   pusheen.meow()   print(pusheen)
  40. def  hi(name):          print('Hi  '  +  name

     +  '!')   girls  =  ['Michelle',  'Ola',  'you']   for  name  in  girls:          hi(name)          print('Next  girl')
  41. $  python3  say_hi.py   Hi  Michelle!   Next  girl  

    Hi  Ola!   Next  girl   Hi  you!   Next  girl
  42. class  Cat:          def  __init__(self,  name):  

                   self.name  =  name          def  __str__(self):                  return  'Cat:  '  +  self.name          def  meow(self):                  print(self.name  +  '  Meow!') pet.py
  43. import  pet   cats  =  [        

     pet.Cat('Kitty'),          pet.Cat('Pusheen'),          pet.Cat('Doraemon'),   ]   for  cat  in  cats:          cat.meow() say_meow.py
  44. $  ls   pet.py      say_meow.py   $  python3

     say_meow.py   Kitty  Meow!   Pusheen  Meow!   Doraemon  Meow!
  45. #  䪾  pet  垷穈⻼Ⰵ   import  pet   #  䪾

     pet  酬꬗涸  Cat  겲ⴽ⻼Ⰵ   from  pet  import  Cat
  46. import  re   addresses  =  [        

     '台北市信義區松仁路7號8樓',          '高雄市鹽埕區中正四路274號',          '屏東縣潮州鎮金陵街31號',          '新竹縣竹北市光明六路東一段183號',   ]   for  addr  in  addresses:          print(re.search(r'\w+市',  addr)) address.py
  47. import  re   addresses  =  [        

     '台北市信義區松仁路7號8樓',          '高雄市鹽埕區中正四路274號',          '屏東縣潮州鎮金陵街31號',          '新竹縣竹北市光明六路東一段183號',   ]   for  addr  in  addresses:          print(re.search(r'\w+市',  addr)) address.py
  48. for  addr  in  addresses:          match  =

     re.search(r'(?P<city>\w+市)',  addr)          if  match:                  print(match.group('city')) address.py $  python3  address.py   台北市   高雄市   新竹縣竹北市
  49. for  addr  in  addresses:          match  =

     re.search(r'(?P<city>\w{2}市)',  addr)          if  match:                  print(match.group('city')) address.py $  python3  address.py   台北市   高雄市   竹北市
  50. for  addr  in  addresses:          match  =

     re.search(r'^(?P<city>\w{2}市)',  addr)          if  match:                  print(match.group('city')) address.py $  python3  address.py   台北市   高雄市  
  51. for  addr  in  addresses:          match  =

     re.search(                  r'(?P<no>\d+號)$',                  addr,          )          if  match:                  print(match.group('no')) address.py $  python3  address.py   274號   31號   183號  
  52. for  addr  in  addresses:          match  =

     re.search(                  r'(?P<no>\d+號(\d+樓)?)$',                  addr,          )          if  match:                  print(match.group('no')) address.py $  python3  address.py   7號8樓   274號   31號   183號
  53. ^ Ꟛ걧 $ 穡㽵 \w ♧⦐㶶 \d ♧⦐꣚䬘⠭侸㶶 + ⵹꬗涸匌銯♧⦐⟃♳

    {num} ⵹꬗涸匌銯剤num⦐ ? ⵹꬗涸匌銯ꨫ䧴♧⦐ (  ...  ) 䪾匌銯穉ざ㖈♧饱 (?P<name>  ...) 䪾匌銯穉ざ㖈♧饱⚛《そ䧭name