tutorial
13. ปัจฉิมลิขิต
Submitted by wd on 7 January, 2008 - 15:55.ยังเรียบเรียงไม่หมด ยังขาดอีก 4 เรื่อง ที่ยังไง ๆ ก็ไม่แปลเด็ดขาด เพราะท่านที่อ่านจนมาถึงตรงนี้ได้ก็คงเก่งภาษาอังกฤษกว่าผมแล้ว :D
คือ
- A. Interactive Input Editing and History Substitution
- B. Floating Point Arithmetic: Issues and Limitations
- C. History and License
- D. Glossary
หวังว่าคงจะมีท่านผู้ใจบุญมาปรับปรุงให้บทความนี้สมบูรณ์ขึ้น และได้อำนวยประโยชน์สุขแก่ผู้อ่านทุกท่าน เทอญ.
12. เรียนต่อที่ไหน (What Now?)
Submitted by wd on 7 January, 2008 - 15:54.- เอกสารหลักของไพธอนเอง
-
- บรรณสารของไพธอน (Python Library Reference) เวลาดูรายละเอียดฟังก์ชั่น โมดูล และอื่น ๆ ต้องมาดูที่นี่สถานเดียว
- ติดตั้งโมดูล อธิบายวิธีติดตั้งโมดูลจากภายนอกด้วยตนเอง
- ภาษาสารของไพธอน (Language Reference) อธิบายโครงสร้างทางภาษา
- ข้อมูลจากแห่งอื่น
-
- http://www.python.org/ เว็บหลักของไพธอน
- http://docs.python.org/ ทางเข้าเอกสารหลักของไพธอน
- http://cheeseshop.python.org รายชื่อแพกเกจใหม่ ๆ รายวัน ถ้าเราอยากจะแจกจ่ายแพกเกจของเรา ก็ต้องมาที่นี่
11. เหลือบดูเครื่องมือเซียน (Brief Tour of the Standard Library - Part II)
Submitted by wd on 7 January, 2008 - 15:53.11.1 การแสดงผล (Output Formatting)
11.2 เทมเพลต (Templating)
11.3 ข้อมูลไบนารี (Working with Binary Data Record Layouts)
11.4 เธรด (Multi-threading)
11.5 ปูม (Logging)
11.6 กำจัดจุดอ่อน (Weak References)
11.7 ใช้ลิสต์ให้สะดวก (Tools for Working with Lists)
11.8 เลขทศนิยมลอย (Decimal Floating Point Arithmetic)
11.1 การแสดงผล (Output Formatting)
reprrepr() ให้เป็นแบบที่เราต้องการ
>>> import repr
>>> repr.repr(set('supercalifragilisticexpialidocious'))
"set(['a', 'c', 'd', 'e', 'f', 'g', ...])"pprint>>> import pprint >>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', ... 'yellow'], 'blue']]] ... >>> pprint.pprint(t, width=30) [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'],
10. มีอะไรให้ใช้บ้าง (Brief Tour of the Standard Library)
Submitted by wd on 7 January, 2008 - 15:52.10.1 งานของระบบปฏิบัติการ (Operating System Interface)
10.2 ไฟล์ไวลด์คาร์ด (File Wildcards)
10.3 อาร์กิวเมนต์ (Command Line Arguments)
10.4 นำเข้า ส่งออก และเปลี่ยนทิศ (Error Output Redirection and Program Termination)
10.5 จับคู่สตริงก์ (String Pattern Matching)
10.6 คณิตศาสตร์ (Mathematics)
10.7 อินเตอร์เน็ต (Internet Access)
10.8 วันที่และเวลา (Dates and Times)
10.9 การบีบอัดข้อมูล (Data Compression)
10.10 จับประสิทธิภาพ (Performance Measurement)
10.11 การควบคุมคุณภาพ (Quality Control)
10.12 พร้อมใช้ (Batteries Included)
เรามาดูว่าในไลบรารีมาตรฐานของไพธอน มีฟังก์ชั่นหรือเมธอดในโมดูล อะไรให้ใช้บ้าง
10.1 งานของระบบปฏิบัติการ (Operating System Interface)
os- โมดูล
osมีเมธอดให้ใช้เยอะมาก>>> import os >>> os.system('time 0:02')
9. คลาส (Classes)
Submitted by wd on 7 January, 2008 - 15:50.9.1 ศัพท์ (A Word About Terminology)
9.2 สโคปและเนมสเปซ (Python Scopes and Name Spaces)
9.3 แรกพบคลาส (A First Look at Classes)
9.4 หมายเหตุเรื่องคลาส (Random Remarks)
9.5 การสืบทอดคลาส (Inheritance)
9.6 ตัวแปรเฉพาะที่ (Private Variables)
9.7 ปกิณกะ (Odds and Ends)
9.8 ตัวยกข้อผิดพลาดก็เป็นคลาส (Exceptions Are Classes Too)
9.9 ตัวกระทำซ้ำ (Iterators)
9.10 เจนเนอเรเตอร์ (Generators)
9.11 เจนเนอเรเตอร์เอกซ์เพรสชั่น (Generator Expressions)
คลาสในไพธอนถูกออกแบบมาให้ใช้งานง่าย มันเลยไม่ได้ป้องกันแน่นหนาแบบภาษาอื่น อย่างไรก็ตามมันก็ยังมีคุณสมบัติของคลาสอย่างที่ควรเป็น
9.1 ศัพท์ (A Word About Terminology)
เพื่อให้เกิดความสุขสวัสดีทั้งผู้เขียนและผู้อ่าน บทนี้จะใช้ทับศัพท์ให้มากที่สุดเท่าที่เป็นไปได้ :)
8. ข้อผิดพลาดและสิ่งผิดปรกติ (Errors and Exceptions)
Submitted by wd on 7 January, 2008 - 15:49.8.1 ข้อผิดพลาดทางโครงสร้างประโยค (Syntax Errors)
8.2 สิ่งผิดปรกติตอนทำงาน (Exceptions)
8.3 การจัดการสิ่งผิดปรกติ (Handling Exceptions)
8.4 การป่าวสิ่งผิดปรกติ (Raising Exceptions)
8.5 สร้างชนิดสิ่งผิดปรกติเอง (User-defined Exceptions)
8.6 เก็บกวาดปิดท้าย (Defining Clean-up Actions)
8.7 การเก็บกวาดที่มีไว้ให้ (Predefined Clean-up Actions)
ข้อผิดพลาดมีสองแบบคือ ผิดทางโครงสร้างประโยค (syntax errors) หรือเกิดสิ่งผิดปรกติตอนโปรแกรมทำงาน (exceptions)
8.1 ข้อผิดพลาดทางโครงสร้างประโยค (Syntax Errors)
มือใหม่ต้องเจอทุกคน
>>> while True print 'Hello world'
File "<stdin>", line 1, in ?
while True print 'Hello world'
^
SyntaxError: invalid syntax
ในที่นี้คือตก : หลัง True
ข้อผิดพลาดแบบนี้ต้องตามไปแก้ในโค้ดอย่างเดียว
8.2 สิ่งผิดปรกติตอนทำงาน (Exceptions)
7. การอ่านและเขียนข้อมูล (Input and Output)
Submitted by wd on 7 January, 2008 - 15:46.7.1 การจัดรูปแบบเอาต์พุต (Fancier Output Formatting)
7.2 การอ่านเขียนไฟล์ (Reading and Writing Files)
7.1 การจัดรูปแบบเอาต์พุต (Fancier Output Formatting)
สามารถจัดรูปแบบได้สองแบบหลัก คือใช้ฟังก์ชัน และใช้ตัวกระทำ %
ตัวอย่างการใช้ฟังก์ชันแปลงเป็นตัวอักขระ str() ซึ่งให้คนอ่านง่าย หรือแปลงแบบดิบ repr() คือให้ระบบอ่าน (สามารถเขียนอีกแบบภายใต้เครื่องหมาย `...`)
>>> s = 'Hello, world.' >>> str(s) 'Hello, world.' >>> repr(s) "'Hello, world.'" >>> str(0.1) '0.1' >>> repr(0.1) '0.10000000000000001' >>> x = 10 * 3.25 >>> y = 200 * 200 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...' >>> print s The value of x is 32.5, and y is 40000... >>> # The repr() of a string adds string quotes and backslashes: ... hello = 'hello, world\n' >>> hellos = repr(hello) >>> print hellos 'hello, world\n'
6. มอดูล (Modules)
Submitted by wd on 7 January, 2008 - 15:45.6.1 เพิ่มเติม (More on Modules)
6.2 มอดูลมาตรฐาน (Standard Modules)
6.3 ฟังก์ชัน dir() (The dir() Function)
6.4 แพกเกจ (Packages)
ธรรมชาติของการเขียนโปรแกรมแบบมือใหม่ (มือเก่าจะวางโครงสร้างก่อน) ก็คือหัดเขียนในแบบโต้ตอบก่อน ตามมาด้วยลงไฟล์จริง พอไฟล์ใหญ่ขึ้นก็ต้องอาศัยมอดูลเพื่อเอาไว้เก็บพวกฟังก์ชันที่ต้องเรียกใช้ซ้ำ ๆ กัน พูดง่าย ๆ คือมอดูลคือที่เก็บฟังก์ชันเพื่อให้เรียกใช้สะดวก
ใช้ประโยค import module_name ในการเรียกใช้
ชื่อมอดูลจะถูกเก็บไว้ในตัวแปรส่วนรวม (เฉพาะในมอดูล) ชื่อ __name__
สมมุติเราสร้างมอดูลเป็นไฟล์ชื่อ fibo.py (ให้อยู่ในไดเรกทอรีปัจจุบัน) มีเนื้อไฟล์ว่า
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b5. โครงสร้างข้อมูล (Data Structure)
Submitted by wd on 7 January, 2008 - 15:44.5.1 ลิสต์อีกที (More on Lists)
5.2 ประโยค del (The del statement)
5.3 ทูเปิล (Tuples and Sequences)
5.4 เซ็ต (Sets)
5.5 ดิกชันนารี (Dictionaries)
5.6 เทคนิคการวนรอบ (Looping Techniques)
5.7 เงื่อนไข (More on Conditions)
5.8 น้ำหนักของข้อมูลแบบลำดับ (Comparing Sequences and Other Types)
บทนี้จะอธิบายเทคนิคการใช้งานข้อมูล
5.1 ลิสต์อีกที (More on Lists)
เวลาใช้งานจริง เราจะใช้ลิสต์มากหน่อย เพราะมันเปลี่ยนแปลงค่าได้ เลยทำให้มีเมธอดของลิสต์เยอะหน่อย
append(x)- เติมสมาชิกต่อท้ายลิสต์ มีค่าเทียบเท่า
a[len(a):] = [x]>>> a=[1,2,3] >>> a.append(4) >>> a [1, 2, 3, 4]
extend(L)- ยกลิสต์ L ทั้งยวง ไปขยายต่อท้าย a คือ
a[len(a):] = L>>> a=[1,2,3] >>> L=[4,5,6]
4. คำสั่งควบคุม (More Control Flow Tools)
Submitted by wd on 7 January, 2008 - 15:43.4.1 ประโยค if (if Statements)
4.2 ประโยค for (for Statements)
4.3 ฟังก์ชัน range() (The range() Function)
4.4 คำสั่ง break และ continue และวลี else สำหรับการวนรอบ (break and continue Statements, and else Clauses on Loops)
4.5 คำสั่ง pass (pass Statements)
4.6 นิยามฟังก์ชัน (Defining Functions)
4.7 เพิ่มเติมเรื่องฟังก์ชัน (More on Defining Functions)
เมื่อกี้ได้รู้จักคำสั่ง while แล้ว บทนี้เรามารู้จักคำสั่งควบคุมให้มากขึ้น
4.1 ประโยค if (if Statements)
>>> x = int(raw_input("Please enter an integer: "))
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...











