Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Python Conditional Statements: if, else, elif คู่มือครบวงจร

    การตัดสินใจ (Decision Making) ใน Python ใช้ if, elif, else โครงสร้างควบคุมการทำงานของโปรแกรม 1. If Statement (พื้นฐาน) # Syntax if condition: statement # Example password = Hello if len(password) < 6: print(Password too weak - at least 6 characters) # Output: Password too weak - at least 6

  2. Python Variables: คู่มือการประกาศ ใช้งาน และ Best Practices

    ตัวแปร (Variables) ใน Python เป็นชื่อที่อ้างอิงไปยัง Object ใน Memory ไม่ต้องประกาศ Type ไปล่วงหน้า (Dynamic Typing) กฎการตั้งชื่อตัวแปร (Naming Rules) ✅ เริ่มต้นด้วย ตัวอักษร (a-z, A-Z) หรือ Underscore (_) ✅ ต่อด้วย ตัวอักษร, ตัวเลข (0-9), Underscore ✅ Case-sensitive: name, Name, NAME ต่างกัน ❌ ห้

  3. Python Data Types และ Type Conversion: คู่มือครบวงจร

    Python มี Built-in Data Types หลากหลาย การเข้าใจแต่ละประเภทและการแปลงชนิดข้อมูล (Type Conversion) จะช่วยเขียนโค้ดได้มีประสิทธิภาพขึ้น Python Built-in Data Types CategoryTypeคำอธิบายตัวอย่าง Textstrข้อความ (Unicode)hello, world Numericintจำนวนเต็ม (ไม่จำกัดขนาด)42, -10 floatทศนิยม3.14, -0.5 compl

  4. Python Function Attributes คืออะไร

    ฟังก์ชันใน Python เป็น objects จึงมี attributes เช่นเดียวกับออบเจ็กต์ทั่วไป Built-in attribute: __doc__ (docstring) กำหนด attribute ใหม่ได้ด้วย setattr() หรือ dot notation ดึงค่าได้ด้วย getattr() หรือ dot notation ตัวอย่าง def foo(): pass setattr(foo, age, 23) setattr(foo, name, John Doe) print(g

  5. วิธีสร้าง Decorator Chain ( שרשרת dekorators) ใน Python

    Decorators เป็น wrappers รันโค้ดก่อน/หลังฟังก์ชันที่ decorate โดยไม่แก้ฟังก์ชันต้นฉบับ สามารถ chain หลายตัวได้ ตัวอย่าง def makebold(fn): def wrapped(): return + fn() + return wrapped def makeitalic(fn): def wrapped(): return + fn() + return wrapped @makebold @m

  6. วิธีสร้าง Recursive Functions ใน Python

    Recursion คือฟังก์ชันเรียกตัวเอง ต้องมี base case เพื่อหยุด recursion ไม่งั้นจะ infinite loop ตัวอย่าง: ผลรวม n จำนวนแรก def sum_n(n): if n == 0: return 0 else: return n + sum_n(n - 1) print(sum_n(100)) # 5050 print(sum_n(500)) # 125250 Base case: n == 0 Recursive step: n

  7. วิธีทำ Function Overloading ใน Python

    Python ไม่รองรับ method overloading แบบดั้งเดิม (หลายฟังก์ชันชื่อเดียวต่าง signature) แต่ทำได้ด้วย default arguments หรือ type checking ภายในฟังก์ชัน ตัวอย่างด้วย default argument class Human: def sayHello(self, name=None): if name is not None: print(Hello + name) el

  8. วิธีใช้ Global Variable ในฟังก์ชัน Python

    Global variable: เข้าถึงได้ทุกที่ Local variable: เข้าถึงได้เฉพาะใน frame/function นั้น ถ้ามีตัวแปรชื่อเดียวกันในฟังก์ชัน จะเป็น local variable แยกต่างหาก ตัวอย่างเข้าถึง global k = I like green tea def foo(): print(k) # อ่าน global variable foo() print(k) ผลลัพธ์ I like green tea I like gree

  9. วิธีดึงซอร์สโค้ดจากออบเจ็กต์ Python

    ใช้ inspect.getsource(object) คืนซอร์สโค้ดเป็นสตริง ตัวอย่าง import inspect class foo: def bar(): print(Hello) print(inspect.getsource(foo)) ผลลัพธ์ class foo: def bar(): print Hello หากฟังก์ชันถูก compile จากสตริง ไฟล์คอมไพล์ หรือ built-in จะดึงซอร์สไม่ได้ (raise IOError)

  10. วิธีนิยามฟังก์ชัน Python ในขณะรัน (Runtime)

    ใช้โมดูล types และ types.FunctionType() สร้างฟังก์ชันจาก bytecode ที่ compile จากสตริง ตัวอย่างที่ Python prompt >>> import types >>> dynf = types.FunctionType(compile(print Really Works, dyn.py, exec), {}) >>> dynf() Really Works วิธีนี้ใช้สร้างฟังก์ชันแบบไดนามิกขณะรันโปร

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

    Python รองรับ first-class functions ส่งฟังก์ชันเป็นอาร์กิวเมนต์ได้ เช่น map(function, iterable) ตัวอย่าง s = [1, 3, 5, 7, 9] def sqr(x): return x ** 2 print(map(sqr, s)) # [1, 9, 25, 49, 81] # ใช้ lambda print(map(lambda x: x**2, s)) # [1, 9, 25, 49, 81] สามารถเขียนฟังก์ชันรับฟังก์ช

  12. วิธีเขียน Recursive Function หา Factorial ใน Python

    Recursive function เรียกตัวเองจนถึง base case ตัวอย่าง def factorial(n): if n == 1: return 1 else: res = n * factorial(n - 1) return res print(factorial(6) = %d % factorial(6)) # 720 print(factorial(15) = %d % factorial(15)) # 1307674368000 Base case: n == 1 คืน 1

  13. ฟังก์ชัน Python เป็น Objects หรือไม่

    ใช่ ฟังก์ชันใน Python เป็น first-class objects** Python สร้าง function objects เมื่อใช้ def หรือ lambda สามารถกำหนด attributes ได้เหมือนออบเจ็กต์ทั่วไป ตัวอย่าง def foo(): pass foo.score = 20 print(type(foo)) # print(foo.score) # 20 print(type(lambda x: x)) # ฟังก์ชันมี built-in attribu

  14. ทำไมฟังก์ชัน Python บางตัวจึงมี underscore __ นำหน้าและท้ายชื่อ

    ชื่อที่มี double underscores นำหน้าและท้าย (dunder names) เช่น __init__, __str__, __call__ เป็น reserved names สำหรับ built-in methods ของ Python** ควรหลีกเลี่ยงการตั้งชื่อฟังก์ชัน/ตัวแปรแบบนี้ เพื่อไม่ให้เกิดความสับสนหรือชนกับพฤติกรรมภาษา

  15. วิธีรันฟังก์ชัน Python จากบรรทัดคำสั่ง

    ใช้ flag -c (command) รันโค้ด Python โดยตรง ตัวอย่าง $ python -c import foobar; print(foobar.sayHello()) $ python -c from foobar import *; print(sayHello()) $ python -c from foobar import sayHello; print(sayHello()) ผลลัพธ์ Hello วิธีนี้สะดวกสำหรับทดสอบฟังก์ชันเดี่ยวโดยไม่ต้องสร้างไฟล์สคริปต์

  16. วิธีเรียกโมดูล Python จากบรรทัดคำสั่ง

    เขียนสคริปต์ที่มีการเรียกฟังก์ชันท้ายไฟล์ แล้วรันด้วยคำสั่ง python ตัวอย่าง def fubar(): # does something useful fubar() รันที่ command prompt ~$ python fubar.py วิธีนี้เหมาะสำหรับทดสอบโมดูลแบบเร็ว

  17. ฟังก์ชัน Python รันใน HTML ได้เหมือน JavaScript หรือไม่

    ไม่สามารถรัน Python ในเบราว์เซอร์ได้โดยตรง** เพราะไม่มีเบราว์เซอร์ตัวไหนมี Python interpreter built-in JavaScript เป็นภาษาเดียวที่รันในเบราว์เซอร์โดยไม่ต้องใช้ plugin ทางเลือก: ใช้ Transpiler แปลง Python code เป็น JavaScript (เช่น Transcrypt, Brython, Pyodide) เบราว์เซอร์ก็จะรัน JavaScript ที่แปลงมา

  18. ความแตกต่างระหว่าง Global และ Local Variables ใน Python

    Global variable: เข้าถึงได้ทุกที่ในโมดูล Local variable: เข้าถึงได้เฉพาะในขอบเขตที่สร้าง (เช่น ภายในฟังก์ชัน) ตัวอย่าง q = I love coffee # global variable def f(): p = Me Tarzan, You Jane. # local variable print(p) f() print(q) ผลลัพธ์ Me Tarzan, You Jane. I love coffee p เป็น local ของ

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