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

Python Basics for G's ACADEMY TOKYO

Python Basics for G's ACADEMY TOKYO

G's Academy Tokyo にて行なっているPython講義の1日目の資料です。1日目はPythonのプログラミング基礎を学びます。
Presented by http://www.yoheim.net

Yohei Munesada

August 17, 2016
Tweet

More Decks by Yohei Munesada

Other Decks in Technology

Transcript

  1.  pre installed $ python3 --version Python 3.6.2 $ pip3

    —-version pip 9.0.1 from /Library/Frameworks/Python.framework/ Versions/3.6/lib/python3.6/site-packages (python 3.6) https://www.python.org/ Πϯετʔϧ͸ͪ͜Β͔Βˠ
  2.  course catalog 㾎 1ZUIPOͱ͸ 㾎 1ZUIPOجຊฤ 㾎 Ϟδϡʔϧͱύοέʔδ 㾎

    8FCεΫϨΠϐϯά 㾎 8FCαʔόʔ 㾎 1ZUIPOͱػցֶश Basic Advanced
  3.  Crowling & Filtering etc Flask Application Python Python w

    ೔ʑͷ৘ใऩूΛޮ཰తʹߦ͏ͨΊʹ࢓૊Έ w ػցֶश΍ਓ޻஌ೳʹؔ͢Δ৘ใ͕ࡶଟʹू·Δ Machine Learning Now
  4.  Item-to-Item Collaborate Filtering YoheiM.NET Recommend Data Flask Application Python

    Python w هࣄͷӾཡϩάΛ΋ͱʹϨίϝϯυΛఏڙ͢Δ Recommend System for YoheiM.net
  5.  indent style 1ZUIPO͸ΠϯσϯτΛ༻͍ͯϓϩάϥϜߏ଄Λදݱ͠·͢ def api(): search_no = get_search_no() if

    not search_no: Logger.get().warn("search_no doesn't exist.") return None, "error" else: try: result = special_execute_something(search_no) return result, None except: Logger.get().exception(“Oh my god!!!")
  6.  condition 1ZUIPOͷ৚݅෼ذͱͦͷϙΠϯτΛ঺հ͠·͢ if hour <= 6: print("so sleepy”) elif

    10 <= hour <= 12: print("good morning”) elif hour < 12 and day_of_week == “Sunday" or hour > 20: pass else: print("hi") 1 2 3 4
  7.  loop 1ZUIPOͷ܁Γฦ͠ॲཧΛ঺հ͠·͢ for i in range(10): print(i) for item

    in ["a", "b", "c"]: print(item) my_life = 5 while my_life: my_life -= 1 idx = 0 while True: if idx > 5: break idx += 1
  8.  variables 1ZUIPOͷม਺Λ঺հ͠·͢ # Define my_name = "Yohei" age =

    20 is_good = True unittest = 1 # Check type type(my_name) # Concat my_name + age # => Error my_name + str(age)
  9.  list Ϧετܕ͸഑ྻܗࣜͰσʔλΛอ࣋͠·͢ # Create users = [ "Yo", "Ken",

    “Nao”, "Shin", “Lee" ] # Index access users[0] # => ? users[0:2] # => ? users[1:] # => ? users[::2] # => ? users[::-1] # => ? # Add users.append("Miu") # Delete users.remove("Nao")
  10.  list comprehension ʢϦετ಺แදهʣ users = ["Yo", "Ken", "Nao", "Shin",

    "Lee"] users = [u.lower() for u in users if u.find("e") != -1]
  11.  dictionary ࣙॻܕ͸LFZWBMVFͰ஋Λอ࣋͠·͢ # Create user_dict = { "Yohei": 30,

    "John": 35 } # Get user_dict["Yohei"] user_dict.get("Nao", 20) # Get all for k, v in user_dict.items(): print(k, v) # Update / Delete user_dict["Yohei"] = 31 del user_dict["John"]
  12.  set ηοτܕ͸ॏෳ͕ͳ͘ɺॱংΛ࣋ͨͳ͍σʔλͷू߹Ͱ͢ # Create set_ = { "Tennis", "Ramen",

    “Programming" } # Get set_[0] # => Error!! for s in set_: print(s) # Add / Delete set_.add("Gs") set_.remove("Ramen") # Calc set1 = set([1, 2, 3, 4, 5]) set2 = set([3, 4, 5]) set1 - set2
  13.  tuple λϓϧ͸Πϛϡʔλϒϧͳσʔλߏ଄Ͱ͢ # Create nums = "One", "Two", “Three"

    nums = (“One", "Two", “Three") # Get nums[0] for n in nums: print(n) # Immutable nums[0] = "Zero" # => Error! # Assign a, b, c = nums
  14.  function 1ZUIPOͷؔ਺͸ඇৗʹڧྗͰɺ1ZUIPOͷྑ͞ͷͭͰ͢ # Basic def add(a, b): return a

    + b result = add(10, 20) # Argument default value def pow(a, b=2): return a ** b result = pow(10) # Keyword arguments def create_date( year=0, month=0, date=0): return "..." create_date(year=2017, date=10) # Multiple return def get_user(): return 'Yohei', 30 name, age = get_user()
  15.  practice for basic จࣈྻͱΠϯσοΫεΞΫηε ʮ͜͡ʔͩ͢·͔͋͞ΜͰͷΈ͠ʔΎͬΈͯ͸ͨͳͷΜ͍ͩ͠Ζͳʯ͔Βح਺൪ ໨ͷจࣈྻͱɺۮ਺൪໨ͷจࣈྻΛൈ͖ग़ͯ͠ΈΑ͏ ࣸܦͯ͠ΈΑ͏ ؔ਺ Ҿ਺ʹYͱZΛड͚෇͚Δؔ਺Λ࡞੒͠ʮࢲͷ໊લ͸YͰZࡀͰ͢ʯͱग़ྗ͢Δؔ਺

    Λ࡞੒ͯ͠ΈΑ͏ɻʮY:PIFJ ZʯΛ୅ೖͯ͠ಈ͔ͯ͠ΈΑ͏ɻ೥ྸ͕লུ ͞Εͨ৔߹ʹzzࡀͱදࣔ͢ΔΑ͏ʹ͠Α͏ɻ 4FUͷ࿅श ʮۜՏమಓͷ໷ʯͷҰઅ͔Βɺར༻͞Ε͍ͯΔจࣈͷछྨ਺Λऔಘ͠Α͏ɻ ɹIUUQXXXBP[PSBHSKQDBSETpMFT@IUNM
  16. practice for basic ͦͷଞʹ΋ 1ZUIPOʹ͸ศརͳ΋ͷ͕͍ͬͺ͍ɻ৭ʑͱ৮ΕͯΈ͍ͯͩ͘͞ɻ ɹɾҐஔҾ਺ͷλϓϧԽɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾΩʔϫʔυҾ਺ͷࣙॻԽɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾϦετͷ͍Ζ͍ΖɿIUUQXXXZPIFJNOFUCMPHQIQ

    R ɹɾίʔσΟϯάن໿ɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾσίϨʔλʔɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾԋࢉࢠͷఆٛɿIUUQTHPPHM4SW2 ɹɾϥϜμࣜɿIUUQVYNJMLKQ ɹɾͳͲ
  17.  course catalog 㾎 1ZUIPOͱ͸ 㾎 1ZUIPOجຊฤ 㾎 Ϟδϡʔϧͱύοέʔδ 㾎

    8FCεΫϨΠϐϯά 㾎 8FCαʔόʔ 㾎 1ZUIPOͱػցֶश Basic Advanced
  18.  module main.py my_module.py # my_module.py def get_genius(): return ["Yamazaki",

    "Kosuge"] def get_popular(): return "Python Language" # main.py import my_module my_module.get_genius() my_module.get_popular() ෳ਺ͷॲཧΛͭͷϑΝΠϧʹ·ͱΊͨ΋ͷ
  19.  module main.py my_module.py # my_module.py def get_genius(): return ["Yamazaki",

    "Kosuge"] def get_popular(): return "Python Language" # main.py import my_module as mm mm.get_genius() mm.get_popular() ෳ਺ͷॲཧΛͭͷϑΝΠϧʹ·ͱΊͨ΋ͷ
  20.  module main.py my_module.py # my_module.py def get_genius(): return ["Yamazaki",

    "Kosuge"] def get_popular(): return "Python Language" # main.py from my_module import get_genius get_genius() ෳ਺ͷॲཧΛͭͷϑΝΠϧʹ·ͱΊͨ΋ͷ
  21.  package ෳ਺ͷϞδϡʔϧΛ·ͱΊͨ΋ͷ main.py gscommon __init__.py my_module.py my_special.py # gscommon/my_module.py

    def get_genius(): return ["Yamazaki", "Kosuge"] # main.py import gscommon.my_module gscommon.my_module.get_genius()
  22.  package ෳ਺ͷϞδϡʔϧΛ·ͱΊͨ΋ͷ main.py gscommon __init__.py my_module.py my_special.py # gscommon/my_module.py

    def get_genius(): return ["Yamazaki", "Kosuge"] # main.py from gscommon import my_module my_module.get_genius()
  23.  package ෳ਺ͷϞδϡʔϧΛ·ͱΊͨ΋ͷ main.py gscommon __init__.py my_module.py my_special.py # gscommon/my_module.py

    def get_genius(): return ["Yamazaki", "Kosuge"] # main.py import gscommon gscommon.my_module.get_genius()
  24.  ྫ͑͹+40/Λѻ͏ϥΠϒϥϦ # A example of `json`. import json s

    = """ { "name": "G's Academy", "members": 500 } “"" school = json.loads(s) https://docs.python.org/3/library/json.html python standard libraries
  25.  ྫ͑͹)551௨৴Λѻ͏ϥΠϒϥϦ # A example of `urllib.request`. import urllib.request url

    = “http://www.yoheim.net” with urllib.request.urlopen(url) as res: html = res.read().decode('utf-8') https://docs.python.org/3/library/urllib.request.html python standard libraries
  26.  practice for module and package Ϟδϡʔϧͷ࡞੒ ʮNZ@TQFDJBMQZʯͱ͍͏ϞδϡʔϧΛ࡞੒ͯ͠࢖ͬͯΈΑ͏ ࣸܦͯ͠ΈΑ͏ ύοέʔδͷ࡞੒

    ʮNZVUJMTʯͱ͍͏ύοέʔδΛ࡞੒ͯ͠࢖ͬͯΈΑ͏ ඪ४ϥΠϒϥϦͷར༻ ҎԼͷ63-ΛࢀߟʹɺEBUFUJNFϞδϡʔϧͱSFϞδϡʔϧΛ࢖ͬͯΈΑ͏ɻ ɹEBUFUJNFIUUQXXXZPIFJNOFUCMPHQIQ R ɹSFIUUQXXXZPIFJNOFUCMPHQIQ R
  27.  practice more 1ZUIPOͷԾ૝؀ڥʹ௅ઓʂ ࣮ࡍͷ։ൃͰ͸ɺϓϩδΣΫτ͝ͱʹ1ZUIPOͷ؀ڥΛ෼͚ͯߦ͍·͢ɻ ҎԼͷهࣄΛࢀߟʹɺQZFOWͱQZFOWWJSUVBMFOWΛ࢖ͬͯσΟϨΫτϦ͝ͱʹ
 1ZUIPOͷ؀ڥΛม͑ͯΈ·͠ΐ͏ʔ?? ɹQZFOWIUUQXXXZPIFJNOFUCMPHQIQ R ɹQZFOWWJSUVBMFOWIUUQXXXZPIFJNOFUCMPHQIQ

    R ͦͷଞʹ΋ ଞʹ΋ศརͳ΋ͷ͕͍ͬͺ͍͋Γ·͢ͷͰɺ৮ΕͯΈ͍ͯͩ͘͞ɻ ɹɾҾ਺Λѻ͏ᶃɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾҾ਺Λѻ͏ᶄɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾϢχοτςετɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾ(PPHMF"OBMZUJDTΛѻ͏ɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾͳͲ
  28.  re: course catalog 㾎 1ZUIPOͱ͸ 㾎 1ZUIPOجຊฤ 㾎 Ϟδϡʔϧͱύοέʔδ

    㾎 8FCεΫϨΠϐϯά 㾎 8FCαʔόʔ 㾎 1ZUIPOͱػցֶश Basic Advanced