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

2dfd5e0acd70adff0e2efc745d992396?s=128

Yohei Munesada

August 17, 2016
Tweet

Transcript

  1. Hello Python World ʙָ͠Έͳ͕Βجૅ͔Β࣮ફ·Ͱֶ΅͏ʙ Yohei Munesada

  2.  https://goo.gl/wBcVt7

  3.  फఆ༸ฏ ΉͶͩ͞Α͏΁͍ (`T"$"%&.:50,:0ϝϯλʔ IUUQXXXZPIFJNOFU about me

  4.  concept 1ZUIPOΛָ͠Έͳ͕Βֶͼ·͠ΐ͏ جૅ͔Β࣮ફ·Ͱ੝Γͩ͘͞Μʹ͓఻͑͠·͢ ԋशͰखΛಈ͔ͯ͠਎ʹ͚ͭΔ

  5.  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/ Πϯετʔϧ͸ͪ͜Β͔Βˠ
  6.  course catalog 㾎 1ZUIPOͱ͸ 㾎 1ZUIPOجຊฤ 㾎 Ϟδϡʔϧͱύοέʔδ 㾎

    8FCεΫϨΠϐϯά 㾎 8FCαʔόʔ 㾎 1ZUIPOͱػցֶश Basic Advanced
  7.  apps you’ll create https://goo.gl/0v5dSj https://goo.gl/KMgK3e

  8.  What is Python ?

  9.  agenda 1ZUIPOͰͰ͖Δ͜ͱ 1ZUIPOͱ͸ 1ZUIPOͷϝϦοτ 1ZUIPOͷσϝϦοτ 1ZUIPOͱͷ෇͖߹͍ํ

  10.  Planner Web όονॲཧ Apache Python w ೔ৗతʹඞཁͳ෼ੳΛ1ZUIPOͰࣗಈ࣮ߦ w ෳ਺ͷσʔλιʔεΛѻ͏෼ੳ΋ࣗಈԽ͠࡞ۀޮ཰Ξοϓ

  11.  Crowling & Filtering etc Flask Application Python Python w

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

    Python w هࣄͷӾཡϩάΛ΋ͱʹϨίϝϯυΛఏڙ͢Δ Recommend System for YoheiM.net
  13.  Python used at.. Youtube Instagram Gunosy etc…

  14.  Python is ೥ʹάΠυɾϰΝϯϩοαϜʹΑΓެ։͞Εͨ ΠΪϦεͷίϝσΟ൪૊ʮۭඈͿϞϯςΟɾύΠιϯʯ͕༝དྷΒ͍͠ 1.0x (1994) 2.0x (2000) 3.0x

    (2008) 2.7x (2010~2020) 3.6x (2016) 1ZUIPO4PGUXBSF'PVOEBUJPO
  15.  good points ίʔυ͕γϯϓϧͰѻ͍΍͍͢ ਺ߦॻ͚͹৭ʑͱͰ͖Δ εΫϦϓτݴޠͰίϯύΠϧ͸ෆཁ ϚϧνϓϥοτϑΥʔϜ ಈతܕ෇͚ ػցֶशͷϥΠϒϥϦ͕ॆ࣮

  16.  not good points YͱYͷ໰୊ ӳޠͷυΩϡϝϯτʹ౰ͨΔ͜ͱ͕ଟ͍ ೔ຊޠ ϚϧνόΠτ ͰϋϚΔ͕࣌͋Δ ಈతܕ෇͚ͷͨΊϦϑΝΫλϦϯά͕େม

    ϨϯαόͰಈ͔͢ͷ͸େม
  17.  work with Python ਺ߦͰ΍Γ͍ͨ͜ͱ͕Ͱ͖Δͷ͸ૉఢ ·ͣ͸ॻ͍ͯΈΔɺͱ͍͏ελϯε ӳޠ͸ؤுΖ͏ YͱYͷͲͪΒͷ৘ใͳͷ͔ؾʹ͔͚Δ

  18.  Python Basic

  19.  agenda Basic 1 ϓϩάϥϜͷ࣮ߦ ΠϯσϯτελΠϧ ৚݅෼ذͱϧʔϓॲཧ ม਺ఆٛͱσʔλܕ σʔλߏ଄ -JTU

    %JDUJPOBSZ 4FU 5VQMF  ؔ਺ Basic 2
  20.  agenda Basic 1 ϓϩάϥϜͷ࣮ߦ ΠϯσϯτελΠϧ ৚݅෼ذͱϧʔϓॲཧ ม਺ఆٛͱσʔλܕ σʔλߏ଄ -JTU

    %JDUJPOBSZ 4FU 5VQMF  ؔ਺ Basic 2
  21.  how to execute ର࿩ܕ࣮ߦ ϓϩάϥϜ࣮ߦ ͦͷଞ KVQZUFSOPUFCPPLͳͲ

  22.  demo

  23.  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!!!")
  24.  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
  25.  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
  26.  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)
  27.  agenda Basic 1 ϓϩάϥϜͷ࣮ߦ ΠϯσϯτελΠϧ ৚݅෼ذͱϧʔϓॲཧ ม਺ఆٛͱσʔλܕ σʔλߏ଄ -JTU

    %JDUJPOBSZ 4FU 5VQMF  ؔ਺ Basic 2
  28.  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")
  29.  list comprehension ʢϦετ಺แදهʣ users = ["Yo", "Ken", "Nao", "Shin",

    "Lee"] users = [u.lower() for u in users if u.find("e") != -1]
  30.  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"]
  31.  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
  32.  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
  33.  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()
  34.  practice for basic จࣈྻͱΠϯσοΫεΞΫηε ʮ͜͡ʔͩ͢·͔͋͞ΜͰͷΈ͠ʔΎͬΈͯ͸ͨͳͷΜ͍ͩ͠Ζͳʯ͔Βح਺൪ ໨ͷจࣈྻͱɺۮ਺൪໨ͷจࣈྻΛൈ͖ग़ͯ͠ΈΑ͏ ࣸܦͯ͠ΈΑ͏ ؔ਺ Ҿ਺ʹYͱZΛड͚෇͚Δؔ਺Λ࡞੒͠ʮࢲͷ໊લ͸YͰZࡀͰ͢ʯͱग़ྗ͢Δؔ਺

    Λ࡞੒ͯ͠ΈΑ͏ɻʮY:PIFJ ZʯΛ୅ೖͯ͠ಈ͔ͯ͠ΈΑ͏ɻ೥ྸ͕লུ ͞Εͨ৔߹ʹzzࡀͱදࣔ͢ΔΑ͏ʹ͠Α͏ɻ 4FUͷ࿅श ʮۜՏమಓͷ໷ʯͷҰઅ͔Βɺར༻͞Ε͍ͯΔจࣈͷछྨ਺Λऔಘ͠Α͏ɻ ɹIUUQXXXBP[PSBHSKQDBSETpMFT@IUNM
  35. practice for basic Ϧετ಺แදه ʙͷ਺஋Λ࣋ͭ഑ྻΛ࡞੒͠ɺશͯͷཁૉΛʮ ਺஋ ԁʯʹ͠Α͏ɻ 1ZUIPOͷΫϥεΛֶ΅͏ 1ZUIPOʹ΋Ϋϥε͕ଘࡏ͠·͢ɻҎԼͷهࣄΛࢀߟʹֶΜͰΈ·͠ΐ͏ʂ ɹIUUQXXXZPIFJNOFUCMPHQIQ

    R
  36. practice for basic ͦͷଞʹ΋ 1ZUIPOʹ͸ศརͳ΋ͷ͕͍ͬͺ͍ɻ৭ʑͱ৮ΕͯΈ͍ͯͩ͘͞ɻ ɹɾҐஔҾ਺ͷλϓϧԽɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾΩʔϫʔυҾ਺ͷࣙॻԽɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾϦετͷ͍Ζ͍ΖɿIUUQXXXZPIFJNOFUCMPHQIQ

    R ɹɾίʔσΟϯάن໿ɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾσίϨʔλʔɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾԋࢉࢠͷఆٛɿIUUQTHPPHM4SW2 ɹɾϥϜμࣜɿIUUQVYNJMLKQ ɹɾͳͲ
  37.  agenda Basic 1 ϓϩάϥϜͷ࣮ߦ ΠϯσϯτελΠϧ ৚݅෼ذͱϧʔϓॲཧ ม਺ఆٛͱσʔλܕ σʔλߏ଄ -JTU

    %JDUJPOBSZ 4FU 5VQMF  ؔ਺ Basic 2
  38.  course catalog 㾎 1ZUIPOͱ͸ 㾎 1ZUIPOجຊฤ 㾎 Ϟδϡʔϧͱύοέʔδ 㾎

    8FCεΫϨΠϐϯά 㾎 8FCαʔόʔ 㾎 1ZUIPOͱػցֶश Basic Advanced
  39.  Python Advance Ϟδϡʔϧͱύοέʔδ

  40.  agenda Ϟδϡʔϧͷఆٛͱར༻ ύοέʔδΛ࢖͏ ඪ४ϞδϡʔϧΛ࢖͏ ֎෦ϞδϡʔϧΛ࢖͏

  41.  module ෳ਺ͷॲཧΛͭͷϑΝΠϧʹ·ͱΊͨ΋ͷ main.py my_module.py

  42.  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() ෳ਺ͷॲཧΛͭͷϑΝΠϧʹ·ͱΊͨ΋ͷ
  43.  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() ෳ਺ͷॲཧΛͭͷϑΝΠϧʹ·ͱΊͨ΋ͷ
  44.  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() ෳ਺ͷॲཧΛͭͷϑΝΠϧʹ·ͱΊͨ΋ͷ
  45.  package ෳ਺ͷϞδϡʔϧΛ·ͱΊͨ΋ͷ main.py gscommon __init__.py my_module.py my_special.py package module

    ʙ Python3.2
  46.  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()
  47.  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()
  48.  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()
  49.  python standard libraries ඪ४ϥΠϒϥϦ͸ඇৗʹॆ࣮͍ͯ͠·͢ https://docs.python.org/3/library/index.html

  50.  ྫ͑͹+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
  51.  ྫ͑͹)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
  52.  3rd party libraries ֎෦ϥΠϒϥϦ͸QJQͰΠϯετʔϧͰ͖·͢ # HTTPϦΫΤετΛൃߦ͢ΔϥΠϒϥϦͷྫ $ pip3 install

    requests https://github.com/requests/requests
  53.  practice for module and package Ϟδϡʔϧͷ࡞੒ ʮNZ@TQFDJBMQZʯͱ͍͏ϞδϡʔϧΛ࡞੒ͯ͠࢖ͬͯΈΑ͏ ࣸܦͯ͠ΈΑ͏ ύοέʔδͷ࡞੒

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

    R ͦͷଞʹ΋ ଞʹ΋ศརͳ΋ͷ͕͍ͬͺ͍͋Γ·͢ͷͰɺ৮ΕͯΈ͍ͯͩ͘͞ɻ ɹɾҾ਺Λѻ͏ᶃɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾҾ਺Λѻ͏ᶄɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾϢχοτςετɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾ(PPHMF"OBMZUJDTΛѻ͏ɿIUUQXXXZPIFJNOFUCMPHQIQ R ɹɾͳͲ
  55.  practice more ͦͷଞʹνΣοΫ͢΂͖ࣄ߲ ɹɾ૬ରΠϯϙʔτɿIUUQTHPPHM;D"37 ɹɾσϑΥϧτͷύεͱ௥ՃɿIUUQCMPHCPOQSPTPGUDPN ɹɾQJQGSFF[FͱSFRVJSFNFOUTUYUɿIUUQTHPPHM"/BSV

  56.  re: course catalog 㾎 1ZUIPOͱ͸ 㾎 1ZUIPOجຊฤ 㾎 Ϟδϡʔϧͱύοέʔδ

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