Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Slide 24-9-61

Slide 24-9-61

Published by pakwanball, 2018-11-19 03:04:27

Description: Slide 24-9-61

Search

Read the Text Version

การเขียนโปรแกรมเชิงวตั ถุด้วยภาษา pythonObject Oriented Programmingwith Python

Applications using pythonแอพพเิ คชนั ท่ใี ช้ pythonThe Python Package Index (PyPI) hosts thousands ofthird-party modules for Python. Both Python's standardlibrary and the community-contributed modules allow forendless possibilities.Web and Internet DevelopmentDatabase AccessDesktop GUIsScientific & NumericEducationNetwork ProgrammingSoftware & Game DevelopmentSource: https://www.python.org/about/apps/#web-and-internet-development

Python toolsเครอ่ื งมือในการเขียน python PyDev – Run as Eclipse PyCharm – Run with JVM (Not free) Python for Visual Studio (2013, 2015) IDLE – Standard Installation Python

Example of Pythonตัวอย่างการใช้งาน python SciPy – Data Science in Python WiPy – IoT using Python Pyro – Robot with Python Raspberry pi with Python for Robotics Python Education - Google

Python websiteเว็บไซต์เกย่ี วกบั pythonhttp://www.programiz.com/python-programming/http://www.pythonforbeginners.com/https://developers.google.com/edu/python/https://wiki.python.org/moin/SimpleProgramshttp://golfapipol.github.io/angularjs-th/lesson/lesson5.htmlhttp://www.python-course.eu/

Programming with Pythonการเขียนโปรแกรมดว้ ยภาษา python Part I – Basic of Python Syntax (ไวยากรณ)์ , Variable (ตวั แปร), Data type (ชนิดขอ้ มูล), Assignment (การกาหนดคา่ ), Operator (เครอื่ งหมายดาเนินการ) Part II – Control Statement (คาสง่ั ควบคมุ ) IF, ELIF, FOR , WHILE Part III – Function (การเขยี นฟั งกช์ นั ) Part IV – I/O File (การอา่ นและเขยี นไฟล)์

Part I – Basic of Pythonการเขียนโปรแกรมด้วยภาษา pythonSyntax (ไวยากรณ)์โครงสร้างภาษา หรือ รูปแบบการเขียนของภาษาคอมพิวเตอร์.>> a=3>> b=5>> print(“a+b = ”, a+b) #commenta+b = 8>> a==bFalse

Part I – Basic of Pythonการเขียนโปรแกรมด้วยภาษา pythonVariables (ตวั แปร)เป็ นการตงั้ ชอื่ (name) เพอ่ื ใชเ้ ก็บขอ้ มลู ทต่ี อ้ งการรบั คา่ ประมวลผล หรอื ใชแ้ ทนคา่ใดๆ>> a=3>> b=5>> c=b>> b=8>> print(“a=”,a,”,b=”,b,”c=”,c)a= 3 ,b= 8 c= 5

Part I – Basic of Pythonการเขยี นโปรแกรมดว้ ยภาษา pythonVariables (ตวั แปร)กฏการตง้ั ชอื่ ตวั แปร ขนึ้ ตน้ ดว้ ยตวั อกั ษรภาษาองั กฤษหรอื เครอื่ งหมาย _ เท่านั้น ไม่ใชอ้ กั ขระพเิ ศษเชน่ $ @ ! ไมใ่ ชค้ าสงวนเชน่ print, def, return

Part I – Basic of Pythonการเขยี นโปรแกรมดว้ ยภาษา pythonData type (ชนิดขอ้ มูล) String หรอื ขอ้ ความ สายอกั ขระ เชน่ “Mr.Tongpool Heeptaisong 123456” Integer หรอื ตวั เลขจานวนเต็ม บวกและลบ เชน่ 123, -1, 0 , 4, -4 Floating Point หรอื เลขจานวนจรงิ ทมี่ ที ศนิยม เชน่ 3.14, 3.00, -4.67, 9.9 Boolean หรอื ขอ้ มูลทเ่ี ป็ นจรงิ (True) หรอื เท็จ (False) Complex: 3 + 2j, 1j Lists: l = [1,2,3] Tuples: t = (1,2,3) Dictionaries: d = {‘hello’ : ‘there’, 2 : 15}

Part I – Basic of Pythonการเขียนโปรแกรมด้วยภาษา pythonData type (ชนิดขอ้ มูล)>> a=3>> b=5.5>> c=b>> b=8>>d=3a+b>> s=“Hello”>> print(“a+a = ”,a+a) ??>> print(“a+b = ”,a+b) ??>> print(“a+c = ”,a+c) ??>> print(d) ??>> print(“a+s = ”, a+s) ??

Part I – Basic of Pythonการเขยี นโปรแกรมด้วยภาษา pythonData type (ชนิดขอ้ มูล)>> l=[1,2,3,4] #list likes array, You can used l[0-n-1],>> print(l) #print all values>> print(l[0]) #index start=0>> print(l[3]) #index end =n-1>> months =('January','February','March','April','May','June',\'July','August','September','October','November','December')>> print(months) #print all values>> print(months[0]) #index start=0>> print(months[3]) #index end =n-1

Part I – Basic of Pythonการเขยี นโปรแกรมด้วยภาษา pythonData type (ชนิดขอ้ มูล)#Data type - Dictionary{}>> phonebook = {'Tong':8806336, \'Dee':6784346, 'Peter':7658344, \'Lame':1122345}>> print(phonebook)#Add the person 'Man' to the phonebook:>> phonebook['Man'] = 1234567>> print(phonebook)>> phonebook['Lex'] = 7989878>> print(phonebook)#Delete 'Lex' from the phonebook:>> del phonebook['Lex']>> print(phonebook)

Part I – Basic of Pythonการเขียนโปรแกรมดว้ ยภาษา pythonAssignment (การกาหนดคา่ ใหก้ บั ตวั แปร) #จะใชเ้ ครอื่ งหมายเทา่ กบั>> a=3>> b=5.5 #กาหนดให้ b=5.5 เป็ นชนิดเลขจานวนจรงิ>> c=b>> b=8>> s=“Hello”

Part I – Basic of Pythonการเขยี นโปรแกรมดว้ ยภาษา pythonOperator (ตวั ดาเนินการ)+ บวก a+b- ลบ a-b* คูณ a*b/ หาร a/b** ยกกาลงั a**a% หารเอาเศษ a%b

Part I – Basic of Pythonการเขยี นโปรแกรมดว้ ยภาษา pythonSample – ตวั อยา่ ง>> a=input(“Please input number 1”)>> b=input(“Please input number 2”)>> a+b>> a-b>> a*b>> a/b>> a**b>> a%b>> -a>> -a-b** input เป็ นฟั งกช์ นั ทรี่ บั คา่ จากแป้ นพมิ พ ์ (Keyboard) ซงึ่ ตวั ภาษาจะมองวา่ เป็ นชนิดขอ้ มูล String เราตอ้ งแปลง String เป็ น Integer ดว้ ยการเพมิ่ คาสง่ั a =int(input(“Please input number 1”))

Part I – Basic of Pythonการเขียนโปรแกรมด้วยภาษา pythonSample – ตวั อย่าง>>a=2>>b=3>>c=5>>(a+b+c) –a+b+c**c/c # มคี า่ เท่าไหร่** ลาดบั การประมวลผล1. () 2. ** 3. *, / 4. +, -

Part I – Basic of Pythonการเขียนโปรแกรมด้วยภาษา pythonMath function - ฟั งกช์ นั เกยี่ วกบั คณิตศาสตร ์>>import math>>a=16>>math.pi>>math.sqrt(a)>>math.factorial(a)>>help(math)

Part I – Basic of Pythonการเขียนโปรแกรมดว้ ยภาษา pythonฟังกช์ นั ความหมายfabs(x) คืนคา่ สมั บรู ณข์ อง xsin(x), cos(x), ฟังกช์ นั ตรีโกณมติ ิของ x (มมุ เป็ นหน่วยtan(x) เรเดียล)pi คา่ พายe ค่า elog(x),log10(x) ค่า log ฐานธรรมชาติ, ค่า log ฐาน 10exp(x) ค่า exsqrt(x) รากที่สองของ x

Part I – Basic of Pythonการเขียนโปรแกรมด้วยภาษา pythonแบบฝึ กหดั1) เขยี นโปรแกรมคานวณหาพนื้ ทขี่ องวงกลม 1.1 รบั คา่ รศั มี (r) จาก keyboard 1.2 นาคา่ r ทไี่ ดไ้ ปคานวณในสูตร area_circle = r**2 * math.pi2) เขยี นโปรแกรมคานวณหาพนื้ ทสี่ ามเหลยี่ ม 2.1 รบั คา่ h และ w จาก keyboard 2.2 นาคา่ h และ w ทไี่ ดไ้ ปคานวณในสูตร area_triangle = ½*w*h

Programming with Pythonการเขยี นโปรแกรมดว้ ยภาษา python Part I – Basic of Python Syntax (ไวยากรณ)์ , Variable (ตวั แปร), Data type (ชนิดขอ้ มูล), Assignment (การกาหนดคา่ ), Operator (เครอื่ งหมายดาเนินการ)

Part II – Control Statementคาสง่ั ควบคมุ Part II – Control Statement (คาสง่ั ควบคุม) IF, ELIF, FOR , WHILEเคร่อื งหมาย ชื่อ ความหมาย ตวั อย่าง ผลลพั ธ์< Less than น้อยกวา่ $a<$c False $a<$d True> Greater than มากกว่า False $a>$c False<= Less than or equal to น้อยกวา่ หรอื เทา่ กบั $a>$d True True>= Greater than or equal to มากกวา่ หรอื เทา่ กบั $a<=$c True $a<=$d False $a>=$c $a>=$d

Part II – Control Statementคาสั่งควบคุม Part II – Control Statement (คาสง่ั ควบคมุ )IF, ELIF, FOR ,WHILEเครือ่ งหมาย ชื่อ ความหมาย ตวั อย่าง ผลลพั ธ์== Equal เทา่ กบั $a==$b True $a==$c True $a==$d False=== Identical เหมอื นกนั $a===$b True False $a===$c False $a===$d False True!= Not equal ไมเ่ ทา่ กบั $a!=$c $a!=$d False True<> Not equal ไมเ่ ทา่ กบั $a<>$c $a<>$d

Part II – Control Statementคาสัง่ ควบคุม Part II – Control Statement (คาสง่ั ควบคมุ )IF, ELIF, SWITCH, FOR , DO, DO WHILE, WHILEเคร่อื งหมาย ช่ือ ความหมาย ตวั อย่าง ผลลพั ธ์! Not คนื ค่า True ถา้ ตวั แปรเป็น False !$a False คนื ค่า False ถา้ ตวั แปรเป็น True&& And คนื ค่า True ถา้ เป็นจรงิ ทงั้ 2 ค่า $a && $b False กรณอี ่นื ๆ คนื คา่ Falseand And คนื ค่า True ถา้ เป็น True ทงั้ 2 คา่ $a and $b True กรณอี ่นื ๆ คนื ค่า False True|| Or คนื ค่า Falseถา้ เป็นFalseทงั้ 2 ค่า $a || $b True กรณอี น่ื ๆ คนื คา่ Trueor Or คนื คา่ Falseถา้ เป็นFalseทงั้ 2 คา่ $a or $b True กรณอี น่ื ๆ คนื ค่า True

Part II – Control Statementคาสง่ั ควบคุม IF, IF ELIF เท็จ เงื่อนไข จริง#EXAMPLE 1 คำสั่งต่ำง ๆ เมื่อy=1 เง่ือนไขเป็ นจริงif y == 1: print(\"y still equals 1“) คำสั่งถดั ไป

Part II – Control Statementคาส่งั ควบคมุ IF, IF ELIF#EXAMPLE 2 IF ELIF เท็จ จริงz = 70 เง่ือนไขif z > 70: คำสั่งต่ำง ๆ เมื่อ เง่ือนไขเป็ นจริงprint(\"Something is very wrong\")elif z < 70: คำสั่งต่ำง ๆ เม่ือ print(\"This is normal\") เง่ือนไขเป็ นเท็จ คำสั่งถดั ไป

Part II – Control Statement เงื่อนไข 1 เท็จ จริงคาส่งั ควบคมุ IF, IF ELIF เท็จ เง่ือนไข 2 จริง คำส่ังต่ำง ๆ เม่ือ เงื่อนไข 1 เป็ นจริง#EXAMPLE 3 IF ELIFimport math เท็จ เงื่อนไข 3 จริง คำสั่งต่ำง ๆ เม่ือx = 30 เงื่อนไข 2 เป็ นจริงif x <= 15:y = x + 15elif x <= 30: คำสั่งต่ำง ๆ เมื่อ y = x + 30 เง่ือนไข 3 เป็ นจริงelse :y=xprint(\"y = \",math.sin(y)) คำสั่งถัดไป

Part II – Control Statementคาสงั่ ควบคมุ While LOOP#EXAMPLE 1a=0while a < 10:a=a+1 เท็จ เง่ือนไข จริงprint(a) คำส่ังถัดไป คำสั่งต่ำง ๆ เม่ือเง่ือนไข เป็ นจริง

Part II – Control Statementคาสง่ั ควบคมุ While LOOP#EXAMPLE 2#Type this in, see what it doesx = 10while x != 0: print(\"wow, we've counted x down, and now itequals\", x) x=x-1print(\"And now the loop has ended.\")

Part II – Control Statementคาสงั่ ควบคุม FOR LOOP#EXAMPLE 1for letter in 'Python': print('Current Letter :', letter)fruits = ['banana', 'apple', 'mango']for fruit in fruits: print('Current fruit :', fruit)print(\"Good bye!\")

Part II – Control Statementคาสงั่ ควบคมุ FOR LOOP#EXAMPLE 3for num in rang(10,20): #Loop 10-19 print(num)print(\"Good bye!\")

Programming with Python การเขยี นโปรแกรมด้วยภาษา python Part I – Basic of Python Syntax (ไวยากรณ)์ , Variable (ตวั แปร), Data type (ชนิดขอ้ มูล), Assignment (การกาหนดคา่ ), Operator (เครอื่ งหมายดาเนินการ) Part II – Control Statement (คาสง่ั ควบคมุ ) IF, ELIF, FOR , WHILE Part III – Function (การเขยี นฟั งกช์ นั ) Part IV – I/O File (การอา่ นและเขยี นไฟล)์

Part III – Functionฟงั ก์ชนั Function คอื โปรแกรมยอ่ ย (Sub Program) ทม่ี กี ารทางานบางอยา่ ง บางครงั้ อาจมกี ารสง่ คา่ (Passing) บางอยา่ งไปหรอื ไมม่ กี ็ได ้ Function อาจมกี ารทางานเสรจ็ ในตวั เอง หรอื อาจจะมกี ารสง่ คา่ กลบั มาก็ได ้ Function เป็ นการกระโดดจากบรรทดั ทที่ างาน อยไู่ ปหาโปรแกรมยอ่ ยทเี่ รา เรยี กใชง้ าน จากนั้นจะกระโดดกลบั มาทบี่ รรทดั ถดั ไป เมอื่ มกี ารทางานทโี่ ปรแกรม ยอ่ ยเสรจ็ แบง่ ไดเ้ ป็ น 2 ประเภทคอื Function เป็ น Function ทม่ี มี ากบั Compiler จะถกู บรรจไุ วใ้ น library file math.pi, math.sqrt() Function ทเี่ ขยี นขนึ้ เอง

Part III – Functionฟงั กช์ นั คำส่ัง ช่ือฟังก์ชั่น อำร์กวิ เมนท์ def cal_area (width , height) :คำสั่งในฟังก์ช่ัน area = width * height return areaคำส่ังท่ัวไป x = int(input(‘width : ‘) y = int(input(‘height : ‘) area = cal_area(x,y) print area

Part III – Functionฟังกช์ ัน  Function #EXAMPLE 1 def print_data(): b = input(\"Enter data: \") print(\"Data is \",b) print_data()

Part III – Functionฟังกช์ นั  Function #EXAMPLE 2 def print_data(data): print(\"Data is \",data) b = input(\"Enter data: \") print_data(b)

Part III – Functionฟังกช์ นั  Function and Return value #EXAMPLE 3 def maxval(num1,num2): if num1 > num2: return num1 else: return num2 x = input(\"Enter number 1: \") y = input(\"Enter number 2: \") print(maxval(x,y))

Part III – Functionฟังกช์ นั Recursive Functions คอื การเรยี กฟั งกช์ นั ตวั มนั เองซา้ ๆ#EXAMPLE 4#n! = n * (n-1)!, if n > 1 and f(1) = 1def factorial(n): if n == 1: return 1 else: return n * factorial(n-1)n = int(input(\"Input n:\"))print(\"Factorial \",n,\" is \", factorial(n))

Part III – Functionฟงั ก์ชนั Recursive Functions#EXAMPLE 5#Fn = Fn-1 + Fn-2 , F0 = 0 and F1 = 1def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2)n = int(input(\"Input n:\"))print(“Fibonacci \",n,\" is \", fib(n))

Part III – Functionฟังกช์ นัแบบฝึ กหดั จงเขยี นเป็ น function1) เขยี นโปรแกรมคานวณหาพนื้ ทสี่ ามเหลยี่ มb = read_base() #รบั คา่ จาก keyboardh = read_height() #รบั คา่ จาก keyboarda = compute_area(b,h) #คานวณprint(a)

Part III – Functionฟังก์ชนัแบบฝึ กหดั จงเขยี นเป็ น function2) เขยี นโปรแกรมเครอื่ งคดิ เลขโดยรบั คา่ จาก Keyboardadd(x, y) #+subtract(x, y) #-multiply(x, y) #*divide(x, y) #/# take input from the userprint(\"Select operation.\")print(\"1.Add\")print(\"2.Subtract\")print(\"3.Multiply\")print(\"4.Divide\")

Programming with Pythonการเขียนโปรแกรมดว้ ยภาษา python  Part I – Basic of Python Syntax (ไวยากรณ)์ , Variable (ตวั แปร), Data type (ชนิดขอ้ มูล), Assignment (การกาหนดคา่ ), Operator (เครอื่ งหมายดาเนินการ)  Part II – Control Statement (คาสง่ั ควบคมุ ) IF, ELIF, FOR , WHILE  Part III – Function (การเขยี นฟั งกช์ นั )  Part IV – I/O File (การอา่ นและเขยี นไฟล)์

Part IV – Input/Output Filesการอ่านและเขียนไฟล์file = open(‘data’, ‘r’) Open the file ‘data’ for inputfile.read() Read whole file into onefile.read(N) Stringfile.readline() Reads N bytes (N >= 1) Returns a list of line strings

Part IV – Input/Output Filesการอ่านและเขียนไฟล์file = open(‘data’, ‘w’) Open the file ‘data’file.write(S) for writingfile.writelines(L)file.close() Writes the string S to file Writes each of the strings in list L to file Closes the file

Part IV – Input/Output Filesการอา่ นและเขยี นไฟล์#Example 1file = open(\"d:/newfile.txt\", \"w\") #file.write(\"hello world in the new file\")file.write(\"and another line\")file.close()file = open(\"d:/newfile.txt\", \"r\")print(file.read())file.close()file = open(\"d:/newfile.txt\", \"r\")print(file.read(5)) #using file.read(n), where \"n\"determines number of characters.file.close()

Part IV – Input/Output Filesการอา่ นและเขยี นไฟล์#Example 1file = open(\"d:/newfile.txt\", \"r\")print(file.readline())file.close()'''file = open(\"d:/newfile.txt\", \"r\")for line in file: print(line)


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook