Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีใช้ Lambda Function ใน Python

    Lambda functions เป็น anonymous one-line functions สร้างขึ้นขณะรัน ไม่ผูกกับชื่อฟังก์ชัน ไม่มีคำสั่ง return คืนค่า expression เสมอ วางได้ทุกที่ที่คาดหวังฟังก์ชัน เหมาะสำหรับฟังก์ชันที่ใช้ครั้งเดียว เรียกจากที่เดียว Syntax lambda argument: manipulate(argument) ตัวอย่าง add = lambda x, y: x + y prin

  2. Anonymous Function (ฟังก์ชันไม่มีชื่อ) ใน Python คืออะไร

    Anonymous function คือฟังก์ชันที่นิยามโดยไม่มีชื่อ ใช้คีย์เวิร์ด lambda เรียกว่า lambda functions ต่างจากฟังก์ชันทั่วไปที่ใช้ def lambda functions ใช้สำหรับฟังก์ชันสั้นๆ ที่ใช้ครั้งเดียว ตัวอย่าง # ผลลัพธ์: [(13, -3), (4, 1), (1, 2), (9, 10)] รายละเอียดการใช้งาน lambda ดูในหัวข้อถัดไป

  3. วิธีใช้ *args และ **kwargs ใน Python

    *args รับ positional arguments จำนวนไม่คงที่ (tuple) **kwargs รับ keyword arguments จำนวนไม่คงที่ (dictionary) ตัวอย่าง *args def multiply(*args): y = 1 for i in args: y = y * i print(y) multiply(3, 4) # 12 multiply(10, 8) # 80 multiply(2, 5, 6) # 60 multiply(4,

  4. ขอบเขตตัวแปร (Variable Scope) ในฟังก์ชัน Python ทำงานอย่างไร

    ตัวแปรใน Python สร้างขึ้นเมื่อมีการ assign ค่า ไม่ต้อง declare ก่อน Global scope: ตัวแปรระดับโมดูล เข้าถึงได้ในฟังก์ชัน Local scope: ตัวแปรที่ assign ภายในฟังก์ชัน เป็น local ของฟังก์ชันนั้น ตัวอย่าง Local vs Global x = 141 def foo(): x = 424 # local variable print(x) foo() # 424 prin

  5. ทำไมต้องใช้คำสั่ง return ใน Python

    print() แสดงผลบนคอนโซล แต่ return ส่งค่ากลับให้ผู้เรียกโดยไม่พิมพ์ ทำให้ฟังก์ชันสิ้นสุดการทำงานทันทีแม้จะไม่ใช่บรรทัดสุดท้าย ฟังก์ชันที่คืนค่าเรียกว่า fruitful functions ฟังก์ชันที่ไม่คืนค่าเรียกว่า procedures ตัวอย่าง def foo(): print(Hello from within foo) return 2 def bar(): return 1

  6. วิธีนิยามฟังก์ชันใน Python

    ฟังก์ชันคือบล็อกโค้ดที่เรียกใช้ด้วยชื่อ สามารถรับพารามิเตอร์และคืนค่าได้ (อาจไม่คืนค่า) Syntax def function_name(): # do something ใช้คีย์เวิร์ด def ตามด้วยชื่อฟังก์ชัน วงเล็บเปิดปิด และ colon บล็อกโค้ดถัดต้องมีการ indent Method คือฟังก์ชันที่ผูกกับออบเจ็กต์ ใน Python ทุกอย่างเป็นออบเจ็กต์ รวม

  7. ฟังก์ชัน Python กับ Methods ของออบเจ็กต์ ตัวไหนพื้นฐานกว่า

    ใน Python ฟังก์ชันเป็นออบเจ็กต์ที่เรียกได้ (callable) เรียกด้วย operator () ออบเจ็กต์อื่นๆ ก็สามารถเลียนแบบฟังก์ชันได้โดย implement __call__ Method เป็นฟังก์ชันพิเศษที่ผูกกับ instance (bound method) หรือไม่ผูก (unbound method) ตัวอย่าง def a(): pass print(a) # print(type(a)) # class C:

  8. ฟังก์ชัน Python คืนค่า/ยืนยัน (Return/Yield) ออบเจ็กต์อย่างไร

    return ทำให้ฟังก์ชันสิ้นสุดการทำงานและคืนค่าให้ผู้เรียก ฟังก์ชันที่คืนค่าเรียกว่า fruitful functions ตัวอย่าง return def sum(a, b): return a + b print(sum(5, 16)) # 21 Generators และ yield Generators เป็น iterators ที่สร้างค่าแบบ lazy (on-the-fly) ไม่เก็บทั้งหมดในหน่วยความจำ ใช้ได้เพียงครั้งเด

  9. ทำไมและอย่างไรฟังก์ชัน Python จึง Hashable

    ออบเจ็กต์ที่ hashable ต้องมีค่า hash คงที่ตลอดชีวิต มีเมธอด __hash__() และเทียบเท่าได้ด้วย __eq__() หรือ __cmp__() ออบเจ็กต์ hashable ใช้เป็น dictionary key หรือสมาชิก set ได้ Built-in immutable objects (int, str, tuple) เป็น hashable Mutable containers (list, dict) ไม่ hashable User-defined class

  10. วิธีรันฟังก์ชันที่มีหลายอาร์กิวเมนต์จากเทอร์มินัล

    ใช้ sys.argv ดึงอาร์กิวเมนต์จากบรรทัดคำสั่ง ตัวอย่าง fubar.py import sys def print_funcargs(arg1, arg2, arg3): print(arg1 + + arg2 + + arg3) if __name__ == __main__: a = sys.argv[1] b = sys.argv[2] c = sys.argv[3] print_funcargs(a, b, c) print(sys.argv) รันในเทอร์มินัล

  11. วิธีส่ง Dictionary เป็นอาร์กิวเมนต์ให้ฟังก์ชัน Python

    ส่ง dictionary เข้าฟังก์ชัน แล้ววนลูปผ่าน key-value pairs ด้วย .items() (Python 3) หรือ .iteritems() (Python 2) ตัวอย่าง d = {a: 1, b: 2, c: 3} def f(dict_arg): for k, v in dict_arg.items(): print(k, 2 * v) f(d) ผลลัพธ์ a 2 c 6 b 4 ลำดับอาจแตกต่างไปได้ เพราะ dictionary ไม่มีลำดับ (Pyth

  12. วิธีขยายแท็บ (Tabs) ให้เป็นช่องว่างหลายช่องใน Python

    ใช้เมธอด str.replace() หรือโมดูล re เพื่อแทนที่แท็บ (\t) ด้วยช่องว่าง วิธีที่ 1: str.replace() print(replace \t\t tabs in \t\t\t this string.replace(\t, )) วิธีที่ 2: re.sub() import re print(re.sub(\t, , replace \t\ttabs in \t\t\t this string)) ผลลัพธ์ทั้งคู่ replace tabs in this string ทั้งสองวิ

  13. วิธีคืนค่า JSON object จากฟังก์ชัน Python

    ใช้ json.dumps() แปลง dictionary เป็น JSON string แล้วคืนค่า ตัวอย่าง import json a = {name: Sarah, age: 24, isEmployed: True} def retjson(): python2json = json.dumps(a) print(python2json) retjson() ผลลัพธ์ {age: 24, isEmployed: true, name: Sarah} ค่าจะเป็น JSON string ที่พร้อมส่งผ่าน net

  14. วิธีเก็บข้อมูลจากฟังก์ชัน Python ลงตาราง SQLite

    ตัวอย่างใช้ sqlite3 module สร้างตาราง เพิ่มข้อมูล และ query ข้อมูล (ไม่ได้เก็บฟังก์ชันโดยตรง แต่เก็บข้อมูลที่ฟังก์ชันสร้าง/คืนค่า) ตัวอย่าง import sqlite3 from employee import employee conn = sqlite3.connect(employee.db) c = conn.cursor() c.execute(CREATE TABLE employee(first text, last text, pay i

  15. วิธีเรียกฟังก์ชัน Python จาก MATLAB

    MATLAB รุ่น 2014b ขึ้นไปรองรับการเรียก Python libraries โดยตรง ใช้ prefix py. นำหน้าชื่อ library ตัวอย่าง py.calendar.isleap(2016) # ans = 1 py.calendar.isleap(2017) # ans = 0 สำหรับฟังก์ชันของตัวเอง สร้างไฟล์ hello.py ใน current working directory: def world(): return hello world ใน MATLAB เรี

  16. กฎขอบเขตตัวแปร (Scoping Rules) พื้นฐานใน Python: LEGB Rule

    ตามหนังสือ Learning Python ของ Mark Lutz ใช้ mnemonics LEGB จดจำขอบเขตจากแคบไปกว้าง: L - Local: ตัวแปรที่ประกาศในฟังก์ชัน (local scope) E - Enclosing: ตัวแปรในฟังก์ชันภายนอกที่ห่อฟังก์ชันปัจจุบัน (nested functions) G - Global: ตัวแปรที่ระดับโมดูล/ไฟล์ (module level) B - Built-in: ชื่อในตัวของ Pytho

  17. ทำไมโค้ด Python ในฟังก์ชันจึงรันเร็วกว่าระดับโมดูล

    โค้ดในฟังก์ชันรันเร็วกว่าระดับโมดูล (global scope) หลักสองเหตุผล: Local variables เร็วกว่า Globals: ตัวแปร local จัดเก็บใน fixed-size array (ไม่ใช่ dict) ตอนคอมไพล์ ชื่อตัวแปร map ไปยัง index การเข้าถึงคือ pointer lookup + refcount increase เร็วมาก Opcode prediction: CPython (ตัวแปลภาษามาตรฐาน) คอม

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:3/451  20-คอมพิวเตอร์/Page Goto:1 2 3 4 5 6 7 8 9