SlideShare a Scribd company logo
1 of 19
Python 101
 Kiattisak Anoochitarom
     NSC Camp #5
Who’s Invent ?



    Guido van Rossum
    Software Engineer at Google inc.
Programming Structure

 ‣ Indentation
 ‣ strong & dynamic type
 ‣ short and readable code
 ‣ interpreter style
PEP-8

เขียน Python โดยไม่อ่าน PEP-8 ถือว่าเป็น “บาป”
 - อ่านง่าย (Readability)
 - กลมกลืน (Consistency)

Guido บอกว่า Programmer ส่วนใหญ่มักจะอ่าน Code มากกว่า
เขียน ดังนั้นถ้าจะเขียนควรเขียนให้อ่านง่าย
PEP-8 Overview [1]

‣ 4 white spaces or 1 tab for indentation
‣ อย่าให้แต่ละบรรทัดเกิน 79 ตัวอักษร
‣ คั่นส่วนต่างๆ ของโปรแกรมด้วยการเว้นบรรทัด
PEP-8 Overview [2]
import library 1 ตัวต่อ 1 บรรทัด
import ที่ด้านบนของไฟล์เสมอ
import cv
import bs4                                          Yes:
from subprocess import Popen, PIPE                    x=1
                                                      y=2
                  Yes: spam(ham[1], {eggs: 2})        long_variable = 3
                  No: spam( ham[ 1 ], { eggs: 2})   No:
                                                      x            =1
                  Yes: dict[‘key’] = list[index]      y             =2
                  No: dict [‘key’] = list [index]     long_variable = 3

    อย่าใช้ whitespace พร่ําเพรื่อ (1 บรรทัดไม่ควรเกิน 79 ตัวอักษร)
Data Type

    String                 str = ‘message’
   Integer                   number = 20
Floating Point                pi = 3.14159
   Boolean              isParse = True, False
    None                          None
     List                    list = [1, 2, 3]
  Dictionary        me = {“name”: “Bas”, “No”: 1}
Class instance              a = MyClass()
Operator
Basic Operator
 +, -, *, /, %, **, &, |       warning!
                               - int/int == int
                               - int**(-int) == float
Comparison Operator
                               - int/float == float
>, >=, <, <=, ==, !=, is       - string + string == concat string
                               - string * int == multiple string
                               - list + list = list



shortly
i = i + 1 == i += 1
Logical Operator
ใน Python เราจะใช้ & และ | (Pipe) เพื่อทํา and, or Operation
แต่ใน Python ก็มี Operator พิเศษและนิยมใช้กันนั่นคือ


                        and , or

                                       shortly logical operation
                                              x &= (a == b)
                                                   ==
                                           x = x and (a == b)
Control Statement (if)
                                       If
                          condition statement
         เงื่อนไขของ if ไม่ต้องใส่วงเล็บ เว้นแต่ว่าเงื่อนไขจะมีหลายบรรทัด



number = int(input(“Enter Number: “))       number = int(input(“Enter Number: “))
if number > 0:                              if number % 2 == 0 or number % 4 == 0
   print “Number is Positive”                  or number % 5 == 0:
elif number < 0:                                  print ‘Number divided by 2, 4, 5’
   print “Number is Negative”
else:
   print “Number is Zero”
Control Statement (for)
  For (foreach)
    loop statement
                          list = [7, 8, 9, 10]
for x in xrange(10):      for index, value in enumerate(list):
  print x                      print index, value

for x in xrange(3, 20):
  print x
                           x=0                Endless Loop
name = [‘a’, ‘b’, ‘c’]     while (x <= 10):   while True:
for x in name               print x             if condition:
  print x                   x += 1                 break;
Play with List, Dictionary



       string method and slice
     list and list method demo
           dictionary demo
     mixed type list, dictionary
Function
Function Syntax:
   def function_name(set of parameter):
     statement ..
     statement ..

def fibonacci(n):
  fibo = 0;
  for k in xrange(0, int(math.floor((n - 1) / 2)) + 1):
     fibo += math.factorial(n - k - 1) / 
          (math.factorial(k) * math.factorial(n - k - 1 - k))
  return fibo


Muti-Return Data
Python OOP
- Class = แม่พิมพ์ขนม
- Object, Instance = ขนม
- Python Class ก็เช่นเดียวกัน
- Python Class จะ subclass จาก Class object เสมอ


                 Demo:
                 - create class
                 - constructor and destructor
                 - create class instance
                 - using class
Read and Write File
open(‘filename’, ‘mode’)
             r = read # เปิดไฟล์เพื่ออ่าน
             w = write # เปิดไฟล์เพื่อเขียนทับ
             a = append # เปิดไฟล์เพื่อเขียนต่อ

file = open(‘filename.txt’, r)
file.read() # อ่านไฟล์ทั้งหมดเป็น String
file.readline() # อ่านไฟล์ทีละบรรทัด
file.readlines() # อ่านไฟล์ทุกบรรทัดออกมาเป็น List of String

file.write(‘string’) # เขียน String ทั้งหมดลงไฟล์
file.writelines([list of string]) # เขียน List of String ลงไฟล์
1-liner
บรรทัดเดียวก็เสียวได้




                   - sum
                   - list comprehensive
                   - shorten if
Tools

Editor
 - Vim
 - Sublime              Interactive Shell
                         - python shell
                         - ipython
IDE                      - bpython
 - Eclipse + PyDev       - IDLE
 - Eric IDE
 - Komodo
Libraries



Libraries ของ Python มีเยอะมาก
หาได้จาก PyPI - http://pypi.python.org/pypi
It’s Application

Digital Image Processing (Python Imaging Library)
           Computer Vision (OpenCV)
            Web Framework (Django)
          Web Server (Gunicorn, Tornado)
  Web Client (Beautiful Soup, urllib3, html5lib)
       Content Management System (Plone)
       Natural Language Processing (NLTK)

More Related Content

What's hot

การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานNookky Anapat
 
พื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาพื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาSarocha Makranit
 
พื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาพื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาAeew Autaporn
 
PHP Tutorial (introduction)
PHP Tutorial (introduction)PHP Tutorial (introduction)
PHP Tutorial (introduction)Tinnakorn Puttha
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : ThreadIMC Institute
 
ความรู้เบื้องต้นภาษาจาวา
ความรู้เบื้องต้นภาษาจาวาความรู้เบื้องต้นภาษาจาวา
ความรู้เบื้องต้นภาษาจาวาThanachart Numnonda
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
การเขียนโปรแกรมเพื่องานอาชีพ 6.
การเขียนโปรแกรมเพื่องานอาชีพ 6.การเขียนโปรแกรมเพื่องานอาชีพ 6.
การเขียนโปรแกรมเพื่องานอาชีพ 6.Ploy StopDark
 
โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6
โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6
โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6Ploy StopDark
 
Java Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดJava Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดThanachart Numnonda
 
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)Thanachart Numnonda
 
Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์Thanachart Numnonda
 

What's hot (20)

การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
 
พื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาพื้นฐานภาษาจาวา
พื้นฐานภาษาจาวา
 
พื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาพื้นฐานภาษาจาวา
พื้นฐานภาษาจาวา
 
ภาษา C#
ภาษา C#ภาษา C#
ภาษา C#
 
PHP Tutorial (introduction)
PHP Tutorial (introduction)PHP Tutorial (introduction)
PHP Tutorial (introduction)
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : Thread
 
โปรแกรมย่อยและฟังก์ชัน
โปรแกรมย่อยและฟังก์ชันโปรแกรมย่อยและฟังก์ชัน
โปรแกรมย่อยและฟังก์ชัน
 
ความรู้เบื้องต้นภาษาจาวา
ความรู้เบื้องต้นภาษาจาวาความรู้เบื้องต้นภาษาจาวา
ความรู้เบื้องต้นภาษาจาวา
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
การเขียนโปรแกรมเพื่องานอาชีพ 6.
การเขียนโปรแกรมเพื่องานอาชีพ 6.การเขียนโปรแกรมเพื่องานอาชีพ 6.
การเขียนโปรแกรมเพื่องานอาชีพ 6.
 
โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6
โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6
โปรแกรมย่อยและฟังก์ชัน มาตรฐาน 6
 
Unit8
Unit8Unit8
Unit8
 
Java Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดJava Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาด
 
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
 
Unit7
Unit7Unit7
Unit7
 
Week7
Week7Week7
Week7
 
Week8
Week8Week8
Week8
 
C lang
C langC lang
C lang
 
Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์
 

Viewers also liked

DEVNET-1114 Automated Management Using SDN/NFV
DEVNET-1114	Automated Management Using SDN/NFVDEVNET-1114	Automated Management Using SDN/NFV
DEVNET-1114 Automated Management Using SDN/NFVCisco DevNet
 
DEVNET-2002 Coding 201: Coding Skills 201: Going Further with REST and Python...
DEVNET-2002	Coding 201: Coding Skills 201: Going Further with REST and Python...DEVNET-2002	Coding 201: Coding Skills 201: Going Further with REST and Python...
DEVNET-2002 Coding 201: Coding Skills 201: Going Further with REST and Python...Cisco DevNet
 
Manufacturingofmicroprocessor 120813121945-phpapp02
Manufacturingofmicroprocessor 120813121945-phpapp02Manufacturingofmicroprocessor 120813121945-phpapp02
Manufacturingofmicroprocessor 120813121945-phpapp02Waqar Mughal
 
O lectie de patriotism local
O lectie de patriotism localO lectie de patriotism local
O lectie de patriotism locallazardiana
 
Proposta di deliberazione prot. n. 259 del 2012 (2)
Proposta di deliberazione prot. n. 259 del 2012 (2)Proposta di deliberazione prot. n. 259 del 2012 (2)
Proposta di deliberazione prot. n. 259 del 2012 (2)Fballer77
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonKHNOG
 
Understanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple StepsUnderstanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple StepsTessa Mero
 
Simulation training in medicine and technology management
Simulation training in medicine and technology managementSimulation training in medicine and technology management
Simulation training in medicine and technology managementMCH-org-ua
 
Coding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using SparkCoding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using SparkCisco DevNet
 
Cisco CSR1000V, VMware, and RESTful APIs
Cisco CSR1000V, VMware, and RESTful APIsCisco CSR1000V, VMware, and RESTful APIs
Cisco CSR1000V, VMware, and RESTful APIsPrivate
 
Cisco application infrastracture controller (apic) billyjones
Cisco application infrastracture controller (apic) billyjonesCisco application infrastracture controller (apic) billyjones
Cisco application infrastracture controller (apic) billyjonesBilly jones Monarquia
 
DEVNET-1126 APIC-EM API
DEVNET-1126	APIC-EM APIDEVNET-1126	APIC-EM API
DEVNET-1126 APIC-EM APICisco DevNet
 
Bidirectional Forwarding Detection (BFD)
Bidirectional Forwarding Detection (BFD) Bidirectional Forwarding Detection (BFD)
Bidirectional Forwarding Detection (BFD) KHNOG
 
Network Mapper (NMAP)
Network Mapper (NMAP)Network Mapper (NMAP)
Network Mapper (NMAP)KHNOG
 
WorkShop SDN / ACI - Jeudi 12 Février 2015
WorkShop SDN / ACI - Jeudi 12 Février 2015WorkShop SDN / ACI - Jeudi 12 Février 2015
WorkShop SDN / ACI - Jeudi 12 Février 2015Dig-IT
 
Cach giai bai tap dai tu quan he
Cach giai bai tap dai tu quan heCach giai bai tap dai tu quan he
Cach giai bai tap dai tu quan heNgọc Long
 
Policy Based Routing (PBR)
Policy Based Routing (PBR)Policy Based Routing (PBR)
Policy Based Routing (PBR)KHNOG
 

Viewers also liked (20)

DEVNET-1114 Automated Management Using SDN/NFV
DEVNET-1114	Automated Management Using SDN/NFVDEVNET-1114	Automated Management Using SDN/NFV
DEVNET-1114 Automated Management Using SDN/NFV
 
DEVNET-2002 Coding 201: Coding Skills 201: Going Further with REST and Python...
DEVNET-2002	Coding 201: Coding Skills 201: Going Further with REST and Python...DEVNET-2002	Coding 201: Coding Skills 201: Going Further with REST and Python...
DEVNET-2002 Coding 201: Coding Skills 201: Going Further with REST and Python...
 
Manufacturingofmicroprocessor 120813121945-phpapp02
Manufacturingofmicroprocessor 120813121945-phpapp02Manufacturingofmicroprocessor 120813121945-phpapp02
Manufacturingofmicroprocessor 120813121945-phpapp02
 
O lectie de patriotism local
O lectie de patriotism localO lectie de patriotism local
O lectie de patriotism local
 
Git slide
Git slideGit slide
Git slide
 
Shasta county mou 2013
Shasta county mou 2013Shasta county mou 2013
Shasta county mou 2013
 
Proposta di deliberazione prot. n. 259 del 2012 (2)
Proposta di deliberazione prot. n. 259 del 2012 (2)Proposta di deliberazione prot. n. 259 del 2012 (2)
Proposta di deliberazione prot. n. 259 del 2012 (2)
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Understanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple StepsUnderstanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple Steps
 
Simulation training in medicine and technology management
Simulation training in medicine and technology managementSimulation training in medicine and technology management
Simulation training in medicine and technology management
 
Coding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using SparkCoding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using Spark
 
Cisco CSR1000V, VMware, and RESTful APIs
Cisco CSR1000V, VMware, and RESTful APIsCisco CSR1000V, VMware, and RESTful APIs
Cisco CSR1000V, VMware, and RESTful APIs
 
SDN_NFV_Course
SDN_NFV_CourseSDN_NFV_Course
SDN_NFV_Course
 
Cisco application infrastracture controller (apic) billyjones
Cisco application infrastracture controller (apic) billyjonesCisco application infrastracture controller (apic) billyjones
Cisco application infrastracture controller (apic) billyjones
 
DEVNET-1126 APIC-EM API
DEVNET-1126	APIC-EM APIDEVNET-1126	APIC-EM API
DEVNET-1126 APIC-EM API
 
Bidirectional Forwarding Detection (BFD)
Bidirectional Forwarding Detection (BFD) Bidirectional Forwarding Detection (BFD)
Bidirectional Forwarding Detection (BFD)
 
Network Mapper (NMAP)
Network Mapper (NMAP)Network Mapper (NMAP)
Network Mapper (NMAP)
 
WorkShop SDN / ACI - Jeudi 12 Février 2015
WorkShop SDN / ACI - Jeudi 12 Février 2015WorkShop SDN / ACI - Jeudi 12 Février 2015
WorkShop SDN / ACI - Jeudi 12 Février 2015
 
Cach giai bai tap dai tu quan he
Cach giai bai tap dai tu quan heCach giai bai tap dai tu quan he
Cach giai bai tap dai tu quan he
 
Policy Based Routing (PBR)
Policy Based Routing (PBR)Policy Based Routing (PBR)
Policy Based Routing (PBR)
 

Similar to Python101

RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptxRTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptxnkrafacyberclub
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีNattapon
 
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
บทที่  5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1บทที่  5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1Little Tukta Lita
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.Mink Kamolwan
 
การสร้างแบบสอบถาม
 การสร้างแบบสอบถาม การสร้างแบบสอบถาม
การสร้างแบบสอบถามkruthanyaporn
 

Similar to Python101 (20)

C language
C languageC language
C language
 
C language
C languageC language
C language
 
Dw ch05 basic_php
Dw ch05 basic_phpDw ch05 basic_php
Dw ch05 basic_php
 
05 loops
05 loops05 loops
05 loops
 
Python Course #1
Python Course #1Python Course #1
Python Course #1
 
Introduction toc
Introduction tocIntroduction toc
Introduction toc
 
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptxRTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
 
Unit9
Unit9Unit9
Unit9
 
บทที่ 7
บทที่ 7บทที่ 7
บทที่ 7
 
08 arrays
08 arrays08 arrays
08 arrays
 
Computer Programming 2.1
Computer Programming 2.1Computer Programming 2.1
Computer Programming 2.1
 
Computer Programming 2.2
Computer Programming 2.2Computer Programming 2.2
Computer Programming 2.2
 
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
บทที่  5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1บทที่  5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
 
207
207207
207
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.
 
Midterm
MidtermMidterm
Midterm
 
Tec4
Tec4Tec4
Tec4
 
การสร้างแบบสอบถาม
 การสร้างแบบสอบถาม การสร้างแบบสอบถาม
การสร้างแบบสอบถาม
 
Java-Answer Chapter 05-06
Java-Answer Chapter 05-06Java-Answer Chapter 05-06
Java-Answer Chapter 05-06
 

Python101

  • 1. Python 101 Kiattisak Anoochitarom NSC Camp #5
  • 2. Who’s Invent ? Guido van Rossum Software Engineer at Google inc.
  • 3. Programming Structure ‣ Indentation ‣ strong & dynamic type ‣ short and readable code ‣ interpreter style
  • 4. PEP-8 เขียน Python โดยไม่อ่าน PEP-8 ถือว่าเป็น “บาป” - อ่านง่าย (Readability) - กลมกลืน (Consistency) Guido บอกว่า Programmer ส่วนใหญ่มักจะอ่าน Code มากกว่า เขียน ดังนั้นถ้าจะเขียนควรเขียนให้อ่านง่าย
  • 5. PEP-8 Overview [1] ‣ 4 white spaces or 1 tab for indentation ‣ อย่าให้แต่ละบรรทัดเกิน 79 ตัวอักษร ‣ คั่นส่วนต่างๆ ของโปรแกรมด้วยการเว้นบรรทัด
  • 6. PEP-8 Overview [2] import library 1 ตัวต่อ 1 บรรทัด import ที่ด้านบนของไฟล์เสมอ import cv import bs4 Yes: from subprocess import Popen, PIPE x=1 y=2 Yes: spam(ham[1], {eggs: 2}) long_variable = 3 No: spam( ham[ 1 ], { eggs: 2}) No: x =1 Yes: dict[‘key’] = list[index] y =2 No: dict [‘key’] = list [index] long_variable = 3 อย่าใช้ whitespace พร่ําเพรื่อ (1 บรรทัดไม่ควรเกิน 79 ตัวอักษร)
  • 7. Data Type String str = ‘message’ Integer number = 20 Floating Point pi = 3.14159 Boolean isParse = True, False None None List list = [1, 2, 3] Dictionary me = {“name”: “Bas”, “No”: 1} Class instance a = MyClass()
  • 8. Operator Basic Operator +, -, *, /, %, **, &, | warning! - int/int == int - int**(-int) == float Comparison Operator - int/float == float >, >=, <, <=, ==, !=, is - string + string == concat string - string * int == multiple string - list + list = list shortly i = i + 1 == i += 1
  • 9. Logical Operator ใน Python เราจะใช้ & และ | (Pipe) เพื่อทํา and, or Operation แต่ใน Python ก็มี Operator พิเศษและนิยมใช้กันนั่นคือ and , or shortly logical operation x &= (a == b) == x = x and (a == b)
  • 10. Control Statement (if) If condition statement เงื่อนไขของ if ไม่ต้องใส่วงเล็บ เว้นแต่ว่าเงื่อนไขจะมีหลายบรรทัด number = int(input(“Enter Number: “)) number = int(input(“Enter Number: “)) if number > 0: if number % 2 == 0 or number % 4 == 0 print “Number is Positive” or number % 5 == 0: elif number < 0: print ‘Number divided by 2, 4, 5’ print “Number is Negative” else: print “Number is Zero”
  • 11. Control Statement (for) For (foreach) loop statement list = [7, 8, 9, 10] for x in xrange(10): for index, value in enumerate(list): print x print index, value for x in xrange(3, 20): print x x=0 Endless Loop name = [‘a’, ‘b’, ‘c’] while (x <= 10): while True: for x in name print x if condition: print x x += 1 break;
  • 12. Play with List, Dictionary string method and slice list and list method demo dictionary demo mixed type list, dictionary
  • 13. Function Function Syntax: def function_name(set of parameter): statement .. statement .. def fibonacci(n): fibo = 0; for k in xrange(0, int(math.floor((n - 1) / 2)) + 1): fibo += math.factorial(n - k - 1) / (math.factorial(k) * math.factorial(n - k - 1 - k)) return fibo Muti-Return Data
  • 14. Python OOP - Class = แม่พิมพ์ขนม - Object, Instance = ขนม - Python Class ก็เช่นเดียวกัน - Python Class จะ subclass จาก Class object เสมอ Demo: - create class - constructor and destructor - create class instance - using class
  • 15. Read and Write File open(‘filename’, ‘mode’) r = read # เปิดไฟล์เพื่ออ่าน w = write # เปิดไฟล์เพื่อเขียนทับ a = append # เปิดไฟล์เพื่อเขียนต่อ file = open(‘filename.txt’, r) file.read() # อ่านไฟล์ทั้งหมดเป็น String file.readline() # อ่านไฟล์ทีละบรรทัด file.readlines() # อ่านไฟล์ทุกบรรทัดออกมาเป็น List of String file.write(‘string’) # เขียน String ทั้งหมดลงไฟล์ file.writelines([list of string]) # เขียน List of String ลงไฟล์
  • 17. Tools Editor - Vim - Sublime Interactive Shell - python shell - ipython IDE - bpython - Eclipse + PyDev - IDLE - Eric IDE - Komodo
  • 18. Libraries Libraries ของ Python มีเยอะมาก หาได้จาก PyPI - http://pypi.python.org/pypi
  • 19. It’s Application Digital Image Processing (Python Imaging Library) Computer Vision (OpenCV) Web Framework (Django) Web Server (Gunicorn, Tornado) Web Client (Beautiful Soup, urllib3, html5lib) Content Management System (Plone) Natural Language Processing (NLTK)