Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Ternary Operator ใน Python: If-Else แบบบรรทัดเดียว

    Ternary Operator (Conditional Expression) ใน Python เขียนได้ในบรรทัดเดียว คืนค่าตามเงื่อนไข Syntax value_if_true if condition else value_if_false ตัวอย่างเปรียบเทียบ y else y) # y ใน Function find_max(5, 6) 6 วิธีการ Implement อื่นๆ 1. Tuple Indexing b] b:0.8 2. Dictionary b] a:0.8089...

  2. Transpose Matrix ใน Python: 4 วิธี (Nested Loop, List Comprehension, Zip, NumPy)

    Transpose Matrix = สลับแถวเป็นคอลัมน์ คอลัมน์เป็นแถว ตัวอย่าง Original (3x2): [[1, 2], [3, 4], [5, 6]] Transposed (2x3): [[1, 3, 5], [2, 4, 6]] วิธี 1: Nested Loop (Classic) x = [[1, 2], [3, 4], [5, 6]] result = [[0, 0, 0], [0, 0, 0]] for i in range(len(x)): for j in range(len(x[0])):

  3. Time Functions ใน Python: โมดูล time ครบครั้ง

    โมดูล time จัดการเวลาในหลายรูปแบบ: timestamp, struct_time, formatted string ใช้ Epoch time (1 Jan 1970 UTC) Epoch และ Tick time.time() 1553262407.0398576 ฟังก์ชันหลัก 1. time.time() - Timestamp ปัจจุบัน import time print(time.time()) # 1553262407.0398576 # วัด elapsed time start = time.time()

  4. Generators ใน Python: yield, Memory Efficient และ Iterator Protocol

    Generator เป็นฟังก์ชันพิเศษที่ใช้ yield แทน return คืนค่าทีละตัว (Lazy Evaluation) ประหยัดหน่วยความจำมาก Generator พื้นฐาน def generator_thru_iter(): yield xyz yield 246 yield 40.50 for i in generator_thru_iter(): print(i) # xyz # 246 # 40.5 ใช้ next() แบบ Manual next(g) StopIter

  5. Counters ใน Python: นับความถี่ด้วย collections.Counter

    collections.Counter เป็น subclass ของ dict สำหรับนับความถี่ (Frequency Counting) ใช้งานง่ายและทรงพลัง การสร้าง Counter (3 วิธี) import collections # 1. จาก iterable collections.Counter([a, b, c, a, b, b]) # Counter({b: 3, a: 2, c: 1}) # 2. จาก dict collections.Counter({a: 2, b: 3, c: 1}) # Counte

  6. Global และ Local Variables ใน Python: Scope และการใช้งาน

    Python มี 2 ระดับ Variable Scope: Global (ทั่วโปรแกรม) และ Local (ภายในฟังก์ชัน) Local Variables เข้าถึงได้เฉพาะในฟังก์ชันที่ประกาศ def func(): x = Python # Local print(x) func() print(x) # NameError: name x is not defined Global Variables เข้าถึงได้ทุกที่ในโมดูล รวมถึงในฟังก์ชัน s = Tu

  7. Inplace vs Standard Operators ใน Python: +=, -=, *= และ operator module

    Inplace Operators (augmented assignment) ทำการดำเนินการและกำหนดค่ากลับตัวแปรเดิม โดยไม่สร้างออบเจกต์ใหม่ (สำหรับ mutable types) Inplace Operators พื้นฐาน a = 9 a += 2 print(a) # 11 # เทียบเท่า a = operator.iadd(a, 2) ใช้ได้กับ mutable types (list, dict, set) และ immutable (int, str, tuple) แต่พฤ

  8. Regular Expression (Regex) ใน Python: คู่มือครบครั้งพร้อมตัวอย่าง

    Regular Expression (Regex) คือภาษาสำหรับจับคู่ pattern ใน string Python มีโมดูล re built-in Import และฟังก์ชันหลัก import re re.findall(pattern, string) # list ของทุก match re.search(pattern, string) # Match object แรกที่เจอ re.split(pattern, string) # list แยกตาม pattern re.sub(pattern, rep

  9. Reloading Modules ใน Python: importlib.reload()

    เมื่อพัฒนาโค้ดแบบ Interactive (REPL, Jupyter) การแก้ไขโมดูลแล้ว import ใหม่ไม่ได้ เพราะ Python cache โมดูลไว้ ใช้ importlib.reload() เพื่อโหลดใหม่ Syntax import importlib importlib.reload(module_object) ตัวอย่าง importlib.reload(sys) Error ถ้ายังไม่ import importlib.reload(sys) NameError: name

  10. เขียน Empty Function ใน Python ด้วย pass Statement

    ใน Python ไม่สามารถเว้นบล็อคโค้ดว่างได้ (IndentationError) ต้องใช้คำสั่ง pass ซึ่งเป็น no-op (ไม่ทำอะไร) เพื่อเป็น placeholder Empty Function # Python def empty_func(): pass # C/C++/Java void func() { } Empty Loop condition = True while condition: pass # รอ condition เปลี่ยน (เช่น wait f

  11. Command Line Arguments ใน Python: sys.argv, getopt, argparse

    Command Line Arguments (CLA) ให้ผู้ใช้ควบคุมพฤติกรรมโปรแกรมจากเทอร์มินัล Python มี 3 วิธีหลัก 1. sys.argv (พื้นฐานที่สุด) import sys print(type(sys.argv)) # print(Arguments:) for arg in sys.argv: print(arg) # รัน: python script.py H E L L O # script.py # H # E # L # L # O sys.argv[0] = ชื่อ

  12. Complex Numbers ใน Python: สร้าง คำนวณ และ cmath Module

    Python รองรับ Complex Numbers แบบ Built-in ใช้ j แทนหน่วยจินตภาพ (√-1) การสร้าง Complex Number type(c) c1 (3+6j) Attributes c.conjugate() # 3-6j (conjugate) คำนวณพื้นฐาน c1 < c2 TypeError: <= not supported between instances of complex cmath Module: ฟังก์ชันขั้นสูง import cmath, math, numpy

  13. Mathematical Functions ใน Python: โมดูล math และ cmath

    โมดูล math ให้ฟังก์ชันคณิตศาสตร์มาตรฐานสำหรับจำนวนจริง ส่วน cmath สำหรับจำนวนเชิงซ้อน (Complex Numbers) Import และ Constants import math math.pi # 3.141592653589793 math.e # 2.718281828459045 math.tau # 6.283185307179586 (2π) math.inf # Infinity math.nan # Not a Number ฟังก

  14. Packing และ Unpacking Arguments ใน Python: *args และ **kwargs

    *args และ **kwargs เป็นไวยากรณ์สำคัญใน Python สำหรับรับ/ส่งอาร์กิวเมนต์จำนวนไม่กำหนด Packing: รวมอาร์กิวเมนต์เข้าด้วยกัน ใน Function Definition ใช้ * รวม positional args เป็น tuple, ** รวม keyword args เป็น dict def func(*args, **kwargs): print(args:, args) print(kwargs:, kwargs) func(1, 2

  15. List Methods ใน Python: คู่มือครบครั้งพร้อมตัวอย่าง

    Python List มี Built-in Methods มากมายที่ช่วยจัดการข้อมูลได้สะดวก บทความนี้รวบรวมทุกเมธอดพร้อมตัวอย่างโค้ดและผลลัพธ์ Methods ที่แก้ไขลิสต์ต้นฉบับ (In-place) append(x) เพิ่มสมาชิกท้ายลิสต์ lst [Hello, Python, Tutorialspoint] clear() ล้างลิสต์ให้ว่าง lst [] copy() คัดลอกลิสต์ (Shallow Copy) lst

  16. ความแตกต่างระหว่าง Python 2.x และ Python 3.x

    Python 2 จบการสนับสนุน (EOL) ตั้งแต่ 2020 แต่โค้ดเก่ายังคงมีอยู่ บทความนี้สรุปความแตกต่างหลักเพื่อช่วย Migration 1. print: Statement → Function # Python 2 print Hello, World! print text, ; print more # คั่นด้วย comma, semicolon # Python 3 print(Hello, World!) print(some text,, end=) print(some mo

  17. รับค่าหลายตัวจากผู้ใช้ในบรรทัดเดียวใน Python

    Python ไม่มี scanf() เหมือน C แต่มีวิธีอ่านหลายค่าในบรรทัดเดียวได้ง่ายด้วย input() ร่วมกับ split(), map(), หรือ List Comprehension วิธี 1: เรียก input() หลายครั้ง (ไม่แนะนำ) x, y, z (40, 30, 10) ต้องกด Enter 3 ครั้ง ไม่สะดวก วิธี 2: List Comprehension + split() (แนะนำ) x, y, z (40, 10, hello) ว

  18. str() vs repr() ใน Python: ความแตกต่างสำคัญ

    ทั้ง str() และ repr() คืน string representation ของออบเจกต์ แต่มีจุดประสงค์ต่างกัน คำอธิบายจาก Python Docs str(): informal / readable representation สำหรับ end-user repr(): official representation ที่สามารถนำไป eval() ได้กลับเป็นออบเจกต์เดิม ตัวอย่างเปรียบเทียบ eval(str(x)) # NameError: name P

  19. String Template Class ใน Python: $placeholder แทน %s

    string.Template เป็นวิธีจัดรูปแบบสตริงที่ใช้ $ แทน % ใช้งานง่ายและปลอดภัยกว่า (ไม่ eval code) เหมาะกับ user-generated templates 3 เมธอดหลัก Methodคำอธิบาย Template(template)Constructor รับ template string substitute(mapping, **kw)แทนค่า, โยน KeyError ถ้า placeholder ขาด safe_substitute(mapping,

  20. Division Operators ใน Python: / vs // และ Python 2 vs 3

    การหารใน Python มีรายละเอียดสำคัญที่แตกต่างระหว่าง Python 2 และ 3 และมีออเปอเรเตอร์ 2 ตัว: Python 2: การหารขึ้นกับชนิดข้อมูล 20.0 / 7 # 2.857... (float / int = float) สร้างบั๊กง่าย: 18*9/5 + 32 ให้ 64 (ผิด) แต่ 18.0*9/5 + 32 ให้ 64.4 (ถูก) Python 2.2+: ใส่ // สำหรับ Floor Division 9.0 // 2 # 4.

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:92/451  20-คอมพิวเตอร์/Page Goto:1 86 87 88 89 90 91 92 93 94 95 96 97 98