Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Class / Static Variables ใน Python

    ตัวแปรที่ประกาศภายในคลาสแต่นอกเมธอด เรียกว่า Class Variable (หรือ Static Variable) แชร์ระหว่างทุก Instance ของคลาสนั้น ตัวอย่างพื้นฐาน class Fruits: count = 0 # Class variable def __init__(self, name, count): self.name = name self.count = count # Instance variable (ชื

  2. สร้าง Permutation ทั้งหมดของเซตใน Python

    Permutation คือการจัดเรียงสมาชิกของเซตให้เป็นลำดับต่างๆ Python มีหลายวิธีสร้าง permutation วิธี 1: ใช้ itertools (มาตรฐาน แนะนำ) list(itertools.combinations(1234, 2)) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] วิธี 2: Recursive Generator (ไม่สร้างลิสต์กลาง) = len(xs): yield xs e

  3. String Formatting ด้วย % ใน Python

    Python มีหลายวิธีจัดรูปแบบสตริง: %, {} (.format()/f-string), และ Template บทความนี้เน้น % formatting ซึ่งมาจาก Cs printf Format Specifiers หลัก Specifierประเภทตัวอย่าง %sString%s % hello → hello %d / %iInteger%d % 42 → 42 %fFloat%f % 3.14 → 3.140000 %xHexadecimal%x % 255 → ff %oOctal%o % 8 → 10

  4. Logical Operators (and, or, not) กับ String ใน Python

    ใน Python ตัวดำเนินการตรรกะ and, or, not ใช้กับ String ได้ โดย String ว่าง ถือเป็น False ส่วน String ที่มีค่า ถือเป็น True พฤติกรรมของ and และ or นิพจน์ผลลัพธ์ x and yถ้า x เป็น False คืน y, ไม่เช่นนั้นคืน x x or yถ้า x เป็น True คืน x, ไม่เช่นนั้นคืน y not xTrue ถ้า x เป็น False, มิฉะนั้น Fal

  5. นับจำนวนตัวเลขลบใน Matrix ที่เรียงแล้ว แบบ Row-wise และ Column-wise

    โจทย์: นับจำนวนจำนวนเต็มลบใน Matrix ที่เรียงลำดับจากน้อยไปมากทั้งแถวและคอลัมน์ (Row-wise และ Column-wise sorted) เงื่อนไข Matrix ที่เรียงแล้ว ค่าทุกตำแหน่ง จะน้อยกว่าหรือเท่ากับค่าทางขวา (คอลัมน์ถัดไป) และค่าด้านล่าง (แถวถัดไป) M = [[-40, -12, 1, 5], [-20, -2, 5, 15], [-22, -1, 13, 18],

  6. การเปลี่ยนแปลง Class Members ใน Python: Class Variable vs Instance Variable

    ใน Python OOP ตัวแปรมี 2 ระดับ: Class Variable (ระดับคลาส) และ Instance Variable (ระดับอินสแตนซ์) การเข้าใจความแตกต่างสำคัญมาก ตัวอย่าง: พยายามเปลี่ยน Class Variable ผ่าน Instance class Shark: animal_type = fish # Class Variable def __init__(self, name, age): self.name = name

  7. Partial Functions ใน Python: สร้างฟังก์ชันใหม่จากฟังก์ชันเดิม

    Partial Function ช่วยให้เราสร้างฟังก์ชันใหม่จากฟังก์ชันเดิมโดย ล็อก ค่าพารามิเตอร์บางส่วนไว้ล่วงหน้า ทำให้โค้ด reusable และอ่านง่ายขึ้น Python ให้มาจากโมดูล functools ตัวอย่างพื้นฐาน add1(10) 14 partial(add, 4) คืนฟังก์ชันใหม่ที่รับอาร์กิวเมนต์เดียว (y) และคืน 4 + y ใช้กับ map() และฟังก์ชันระดับ

  8. List Slicing และ List Comprehension ใน Python

    บทความนี้จะอธิบายเรื่อง List Slicing (การตัดรายการ) และ List Comprehension (การสร้างลิสต์ใหม่จากลิสต์เดิม) ซึ่งเป็นเทคนิคพื้นฐานแต่ทรงพลังใน Python List Slicing คืออะไร? Slicing หมายถึงการ หั่น ลิสต์เพื่อดึงเฉพาะส่วนที่ต้องการ โดยระบุ เริ่มต้น (start), สิ้นสุด (stop) และ ขั้นตอน (step) List Compre

  9. ดาวน์โหลดไฟล์จากเว็บด้วย Python

    Python มีโมดูลหลายตัว เช่น urllib, requests สำหรับดาวน์โหลดไฟล์จากเว็บ บทความนี้จะใช้ไลบรารี requests ซึ่งมีประสิทธิภาพและใช้งานง่าย ขั้นตอนการดาวน์โหลดไฟล์ด้วย requests 1. นำเข้าโมดูล import requests 2. ระบุ URL และดึงข้อมูล url = https://www.facebook.com/favicon.ico r = requests.get(url, allow_

  10. WhatsApp Automation ใน Python ด้วย Selenium

    สร้าง WhatsApp Chatbot ด้วย Python เนื่องจาก WhatsApp ไม่มี Official Bot API ใช้ Selenium ควบคุม Browser ผ่าน WhatsApp Web ข้อกำหนดเบื้องต้น Python 3.6+ pip install selenium ChromeDriver (ตรงเวอร์ชัน Chrome Browser) ดาวน์โหลด: chromedriver.chromium.org WhatsApp Account และ Phone เชื่อมต่อ Internet

  11. Web Scraping ใน Python ด้วย BeautifulSoup (bs4)

    BeautifulSoup เป็นคลาสในโมดูล bs4 ใช้ Parse HTML/XML Documents ทำให้ดึงข้อมูลจากเว็บได้ง่าย ติดตั้ง pip install beautifulsoup4 lxml # lxml: Parser ที่เร็วกว่า built-in html.parser ตัวอย่างที่ 1: ดึง Link ทั้งหมดจาก HTML String from bs4 import BeautifulSoup html_doc = TutorialsPoint Google GitHub

  12. ค่าสูงสุดของ Integer ใน Python คือเท่าไหร่?

    ไม่เหมือน C/C++ ที่มี LONG_MAX Python 3 มี Integer แบบ Unlimited Precision (Arbitrary Precision) ไม่มีขีดจำกัดที่กำหนดไว้ชัดเจน ขีดจำกัดปฏิบัติคือ Available Memory (Address Space) Python 2 vs Python 3 Python 2 (Legacy) มี int (C Long, จำกัด) และ long (Arbitrary Precision) แยกกัน >>> impor

  13. Returning Multiple Values ใน Python (คืนค่าหลายค่า)

    Python Functions สามารถคืนค่าหลายค่าได้โดยตรง เก็บในตัวแปรหลายตัวพร้อมกัน นี่คือ Default Property ของ Python ที่หลายภาษา (C++, Java) ไม่มีโดยตรง วิธีที่ 1: Tuple (Implicit) - แนะนำสำหรับค่าน้อย def func(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** 3 return y0, y1, y2 # Implicit Tuple # เร

  14. เมื่อไหร่ควรใช้ yield แทน return ใน Python

    สรุปสั้นๆ: return จบการทำงานฟังก์ชันทันที ข้อความหลัง return ไม่รัน yield หยุดชั่วคราว (Pause) คืนค่า และResume ได้ภายหลัง Yield - Generator Function ใช้ yield ในฟังก์ชัน → ฟังก์ชันกลายเป็น Generator Function ใช้ใน Loop ได้ def gen_func(): yield 1 yield 2 yield 3 for num in gen_func()

  15. Swap ตัวแปรสองตัวในบรรทัดเดียวใน Python

    ใน Python การ Swap ตัวแปรสองตัวทำได้ง่ายมากในบรรทัดเดียว ไม่ต้องใช้ Temporary Variable วิธีมาตรฐาน (Pythonic Way) >>> a = 20 >>> b = 30 >>> a, b = b, a >>> a 30 >>> b 20 การทำงานเบื้องหลัง (Mechanism) Evaluate Right-hand Side ก่อน: b, a ถูกประเมิน สร้า

  16. Print Single และ Multiple Variables ใน Python (Python 2 vs 3)

    การ Print ใน Python 2 และ Python 3 แตกต่างกันอย่างมาก Python 2.7 (Legacy) Print Single Variable >>> print 27 27 >>> print Rahul Rahul >>> print(27) # กำลังทำงาน (ถือเป็น Tuple 1 Element) 27 >>> print(Rahul) Rahul Print Multiple Variables >>> print

  17. Increment และ Decrement Operators ใน Python (++, --)

    Python ไม่มี Unary Increment/Decrement Operators (++, --) เช่นใน C/C++/Java วิธีที่ถูกต้องใน Python # Increment a += 1 # Decrement a -= 1 ตัวอย่าง >>> a = 0 >>> a += 1 >>> a 1 >>> a -= 1 >>> a 0 ทำไม Python ไม่มี ++/-- ? Python Design Philosophy: Ther

  18. Python Program: นับจำนวนคำใน String (Count Words)

    นับจำนวนคำ (Words) ใน String ที่กำหนด มีหลายวิธีใน Python วิธีที่ 1: ใช้ str.split() (แนะนำ - เรียบง่ายที่สุด) int: นับคำโดยแยกด้วย Whitespace (Space, Tab, Newline) return len(text.split()) # ทดสอบ text = Python is a high level language. Python is interpreted language. Python is general-p

  19. Python Program: หาตัวอักษรแรกที่ไม่ซ้ำใน Stream (First Non-Repeating Character)

    หาตัวอักษรที่ไม่ซ้ำ (Unique/Non-repeating) ตัวแรกจาก String หรือ Stream ของ Characters มีหลายวิธีแก้ เราจะสาธิต 2 วิธี วิธีที่ 1: ใช้ Dictionary + List (O(n) Time, O(n) Space) นับ Frequency แล้ว Scan หาตัวแรกที่ Count = 1 def first_non_repeating_char(s: str): char_order = [] # เก็บลำดับตั

  20. Python Program: จัดเรียง String ให้ตัวอักษรซ้ำห่างกันอย่างน้อย d ระยะ

    โจทย์: กำหนด String str (Non-empty) และ Integer k ให้จัดเรียง String ใหม่ ทำให้ตัวอักษรที่เหมือนกันห่างกันอย่างน้อย k ตำแหน่ง หากไม่เป็นไปได้ คืน String ว่าง ตัวอย่าง InputkOutputคำอธิบาย tutorialspoint3tiotiotalnprsuตัวอักษรซ้ำห่าง ≥ 3 aabbcc3abcabcตัวอักษรซ้ำห่าง ≥ 3 aaabc3ไม่เป็นไปได้ aaadb

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