มาศึกษา python ผ่านตัว jupyter

จาก Blog ตอนที่แล้วเรื่อง มาลองเล่น "jupyter กัน" มันเป็นการศึกษา ลองเทสว่าใช้งานได้ก่อนมาเรียนครับ คราวนี้มาเรียนจริงและ โดย ดร. กานต์ (ไม่รู้เขียนชื่อ ถูกหรือป่าว ?) มาศึกษา python ผ่านตัว jupyter บ้าง โดยทำไมต้องใช้ jupyter

  • ง่าย - เขียนเสร็จ Run ดูผลลัพธ์ได้เลย
  • jupyter - สามารถเขียน Doc และ Code ร่วมกันได้
  • notebook - Workspace ที่เราใส่ Code ใส่ไฟล์ต่างๆลงไป เช่น TextFile หรือ JsonFile เป็นต้น
  • แต่ละช่องที่ให้เราพิมพ์อะไรเข้าไป มันเรียกว่า Cell จากรูปมี 3 Cell ครับ
    2016-08-20_101219

มาดู Key ลัดใน jupyter ดีกว่า

Screenshot นี้ผมเอามาจาก https://www.cheatography.com/weidadeyue/cheat-sheets/jupyter-notebook/
Screenshot นี้ผมเอามาจาก https://www.cheatography.com/weidadeyue/cheat-sheets/jupyter-notebook/
  • สำหรับในวันนี้ที่ผมใช้บ่อยๆ เป็นกลุ่มคำสั่งใน Command Mode เข้าได้โดยการกดปุ่ม ESC หรือ Ctrl + Enter ก็ได้นะ ตัว Cell เปลี่ยนเป็นสีฟ้า (ณ 2016-08-20 เผื่ออนาคตโปรแกรมมันเปลี่ยนสีครับ)
    • เพิ่ม Cell : A - Aboove, B - Below
    • ลบ Cell : D - Delete
    • Ctrl + Enter : Run Code ใน Cell นั้น
    • Shift + Enter : Run Code และให้ชี้ Cell ถัดไป
    • Alt Enter : Run Code และให้สร้าง Cell ใหม่
    • Y : เปลี่ยน Cell เอาไว้สำหรับเขียน Code
    • M : เปลี่ยน Cell ให้เป็น Mark Down เอาไว้ใส่ทำ Doc
  • กด Tab มันจะ Hint ด้วยนะ ว่ามี Method อะไรให้ใช้งาน
    2016-08-20_131632
  • กด Shift + Tab มันแสดงคำอธิบาย ว่า Function นั้นต้องการอะไร การกดแต่ละครั้งจะได้ Detail ที่ลึกขึ้น โดยกดได้สูงสุด 4 ครั้ง
    2016-08-20_173745New
  • ตัว jupyter มี Auto Save ด้วยนะ ลองดูจาก Log ใน Command Line นะ
    2016-08-20_104700_autosave
  • ถ้าไม่ใช้งานแล้ว ก็ Ctrl + C ที่ Command Line เพื่อให้มันหยุดรัน Notebook นั้นๆ ไม่งั้นมันจอง Port ยาวนะ เดี๋ยวไปชนกับ App อื่นนะ
    2016-08-20_102209
  • แต่ตัว jupyter เอง มันฉลาดพอที่จะขยับ Port หนีไปนะ
    2016-08-20_130714
  • สำหรับใน jupyter มันใช้ไฟล์ .ipynb ครับ เวลาเอาไปใช้จริงบน Production อย่าลืม Export ไปเป็น .py ด้วยนะครับ แต่มีหลาย format ที่น่าเล่นนะ
    2016-08-22_214557

มาที่ Python กันบ้าง (มี Code ปนๆกับ Comment อธิบายแทรกไปด้วยกัน)

  • Comment
    # one line comment
    
    '''
    Multi-line Comment
    '''
    
    """
    Multi-line Comment
    """
  • Numbers and Math
    25 + 30 / 6
    #output is 30.0
    (25 + 30) / 6
    #output is 9.166666666666666
    3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
    #output is 6.75
    3 + 2 < 5 - 7
    #output is False
    
  • Variables and Names
    cars = 100
    space_in_a_car = 4.0
    drivers = 30
    passengers = 90
    
    cars_not_driven = cars - drivers
    cars_driven = drivers
    carpool_capacity = cars_driven * space_in_a_car
    average_passengers_per_car = passengers / cars_driven
    
    print(cars_not_driven)
    print(cars_driven)
    print(carpool_capacity)
    print(average_passengers_per_car)
    
    '''
    Output
    - cars_not_driven = 70
    - cars_driven = 30
    - carpool_capacity = 120.0
    - average_passengers_per_car = 3.0
    '''
    
  • Data Types
    • int
      type(5)
      #output is int
      
    • float
      type(5.5)
      #output is float
      
      type(9999999999999999999999999999999999999999999999955555555555.5555)
      #output is float
      
      type(55555555555.555555555555555555555555555555555555555555555555555)
      #output is float
      
    • string
      type('hello')
      #output is str
      
    • boolean
      type(True)
      #Output is bool
      
    • turple - immutable object (unchangeable)ชุดข้อมูล แก้ไขไม่ได้นะ
      t= (5, 6, 7)
      type(t)
      #output is tuple
      
      t[0]
      #output is 5
      
      t[0] = 99
      '''
      ---------------------------------------------------------------------------
      TypeError                                 Traceback (most recent call last)
       in ()
      ----> 1 t[0] = 99
      
      TypeError: tuple object does not support item assignment
      '''
      
    • List - mutable
      l = [1,2,3]
      type(l)
      #output is list
      
      l[0]
      #output is 1
      
      l[0] = 99
      
      print(l)
      #output is [99, 2, 3]
      
    • Dictionary - เก็บเป็น Key, Value ข้อมูลข้างในไม่ได้ เรียงลำดับ
  • Input & Output
  • Unpacking
  • Functions
    def just_hello()
        print('Hello')
        
    just_hello()
    
    '''
    #Output is "ERROR"
    File "", line 1
        def just_hello()
                        ^
    SyntaxError: invalid syntax
    '''
    
    def just_hello():
        print('Hello')
        
    just_hello()
    
    #Output is "Hello"
    
    def hello(name):
        print('Hello,', name)
        
    hello('PingkungA')
    #Output is "Hello, PingkungA"
    
    def print_something(a, b, c):
        print('Your input is', a, b, c)
        
    print_something(1, 2, 3)
    
    #Output is "Your input is 1 2 3"
    
    '''
    * เท่ากับ Pointer ใน C, C++ และ
    * arguments
    อันตราย คนรับต้องทำค่าให้พอดีกับ ที่ได้รับมา
    '''
    def print_something_again(*args):
        arg1, arg2, arg3 = args
        print('Your input is', arg1, arg2, arg3)
        
    print_something_again(1, 2, 3)
    
    def print_something_again(*args):
        print('Your input is', args[0])
        
    print_something_again(1, 2, 3)
    #Output is "Your input is 1"
    
    def print_something_again(*args):
        print('Your input is', args[999])
    
    print_something_again(1, 2, 3)
    
    """
    #Output is 'ERROR'
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
     in ()
          2     print('Your input is', args[999])
          3 
    ----- 4 print_something_again(1, 2, 3)
    
     in print_something_again(*args)
          1 def print_something_again(*args):
    ----- 2     print('Your input is', args[999])
          3 
          4 print_something_again(1, 2, 3)
    
    IndexError: tuple index out of range
    """
    
    def get_the_mean(a, b, c, d, f):
        return (a + b + c + d + f) / 5
    
    print(get_the_mean(1, 2, 3, 4, 5))
    
    #Output is "3.0"
    #Note for Python 3.X
    
  • Making Decisions
  • Loops
  • Slicing
  • Reading and Writing Files
  • Modules
  • Class

Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.