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

    LinkedBlockingDeque เป็นคลาสที่ implement จาก Collection interface และ AbstractQueue ใช้ linked nodes รองรับ optional capacity ช่วยลดการสูญเสียหน่วยความจำใน Android ตัวอย่างโครงการ Android ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่ใน Android Studio File → New Project → เลือก Empty Activity → กรอกข้อมูลโปรเจก

  2. ตรวจจับสีเฉพาะ (สีน้ำเงิน) ด้วย OpenCV และ Python

    การประมวลผลภาพ (Image Processing) ดูยากแต่จริงๆ ไม่ยากเท่าที่คิด บทความนี้สาธิตการตรวจจับสีพื้นฐานด้วย OpenCV ใน Python Color Space: RGB vs HSV RGB (Red, Green, Blue) แต่ละสีมีค่า 0-255 (0,0,0) = ดำ, (255,255,255) = ขาว สีน้ำเงินบริสุทธิ์ = (0, 0, 255) สี RGB แดง255, 0, 0 ส้ม255, 128, 0 ช

  3. XML Parsing ใน Python ด้วย ElementTree API

    Python xml.etree.ElementTree เป็น API ที่ใช้งานง่ายที่สุดสำหรับ parse, แก้ไข และสร้างเอกสาร XML 1. สร้างไฟล์ XML ใหม่ import xml.etree.ElementTree as ET def create_xml(filename): root = ET.Element(users) child1 = ET.Element(user) root.append(child1) tree = ET.ElementTree(root)

  4. File Objects ใน Python: การเปิด อ่าน เขียน และปิดไฟล์ฉบับสมบูรณ์

    Python จัดการไฟล์ได้โดยตรง ไม่ต้อง import library เพิ่มเติม ใช้ built-in function open() เพื่อรับ file object ที่มีเมธอดและแอตทริบิวต์สำหรับจัดการไฟล์ ลำดับขั้นตอนการทำงานกับไฟล์ เปิดไฟล์ (open()) อ่านหรือเขียน ปิดไฟล์ (close()) ฟังก์ชัน open() file_obj = open(filename, mode) พารามิเตอร์ คำอ

  5. โมดูล timeit ใน Python: วัดประสิทธิภาพโค้ดอย่างแม่นยำ

    Python มีหลายวิธีวัดเวลาการรันโค้ด โมดูล time วัดเวลาได้แต่ไม่รวม background processes ส่วน timeit รันโค้ดประมาณ 1 ล้านรอบ (ค่าเริ่มต้น) และนำเวลาต่ำสุดมาแสดง ให้ผลลัพธ์แม่นยำกว่า การใช้งานผ่าน Command Line python -m timeit -.join(str(n) for n in range(200)) 1000 loops, best of 3: 290 usec per loo

  6. Python I/O สำหรับการเขียนโปรแกรมแข่งขัน (Competitive Programming)

    ในการเขียนโปรแกรมแข่งขัน (Competitive Programming) การอ่านอินพุตให้เร็วที่สุดเป็นสิ่งสำคัญยิ่ง บทความนี้สำรวจวิธีการ I/O ต่างๆ ใน Python โจทย์ตัวอย่าง: อ่านตัวเลข 4 จำนวน แล้วพิมพ์ผลคูณ วิธีที่ 1: List Comprehension + input() a, b, c, d = [int(x) for x in input().split()] print(a * b * c * d) วิธ

  7. 11 เคล็ดลับเพิ่มประสิทธิภาพโค้ด Python ให้รวดเร็วขึ้น

    แม้ Python จะไม่เร็วเท่าภาษา compiled อื่นๆ แต่บริษัทใหญ่หลายแห่งพิสูจน์ว่า Python จัดการ workload ใหญ่ได้ดี ในบทความนี้จะนำเสนอ 11 เทคนิคเพื่อให้โค้ด Python ทำงานเร็วขึ้นและมีประสิทธิภาพมากขึ้น เคล็ดลับที่ 1: ใช้ Built-in Functions Built-in functions เขียนด้วย C ทำให้เร็วกว่าการเขียนเองมาก ใช้ len

  8. ฟังก์ชัน any() และ all() ใน Python: คู่มือฉบับสมบูรณ์

    Python มอบฟังก์ชัน built-in สองตัวสำหรับการดำเนินการตรรกะ OR และ AND คือ any() และ all() ฟังก์ชัน any() any() คืนค่า True หากมี อย่างน้อยหนึ่งรายการ ใน iterable เป็น True มิฉะนั้นคืน False หาก iterable ว่างเปล่า จะคืน False ไวยากรณ์ any(iterable) iterable สามารถเป็น list, tuple, set, dictionary ได

  9. Python String Methods: คู่มือเมธอดสตริงครบถ้วน พร้อมตัวอย่าง

    Python มอบเมธอดสตริงที่เป็น built-in (สร้างตัว) ให้ใช้งานมากมาย นี่คือรายการเมธอดสตริงที่มีใน Python 3 พร้อมคำอธิบายและตัวอย่างโค้ด เมธอด คำอธิบาย ตัวอย่าง capitalize() คืนค่าสตริงที่ตัวอักษรแรกเป็นพิมพ์ใหญ่ และตัวอื่นเป็นพิมพ์เล็ก print(mystring.capitalize()) Hello python casefold() คืนค

  10. Function Decorators ใน Python: คู่มือฉบับสมบูรณ์

    นักพัฒนา Python สามารถขยายและปรับเปลี่ยนพฤติกรรมของฟังก์ชัน เมธอด หรือคลาสที่เรียกใช้งานได้ (callable) โดยไม่ต้องแก้ไขโค้ดต้นฉบับอย่างถาวร โดยใช้ Decorators (ตัวตกแต่ง) โดยสรุปได้ว่า Decorators คือออบเจกต์ที่เรียกใช้งานได้ ซึ่งนำมาใช้เพื่อปรับเปลี่ยนฟังก์ชันหรือคลาส Function Decorators คืออะไร? Fun

  11. Array Module ใน Python: Typed Arrays สำหรับ Numeric Data

    โมดูล array ให้ Typed Arrays แบบ C: เก็บข้อมูล numeric แบบ compact, memory-efficient, เหมาะสำหรับข้อมูลปริมาณมาก สร้าง Array type(arr) array.typecodes bBuhHiIlLqQfd Type Codes CodeC TypePython TypeBytes bsigned charint1 Bunsigned charint1 uPy_UNICODEUnicode char2 hsigned shortint2 Hunsigned

  12. Bisect Module ใน Python: Binary Search และ Insertion สำหรับ Sorted List

    โมดูล bisect ใช้ Bisection Algorithm (Binary Search) หาตำแหน่ง insert ใน sorted list ให้เรียงลำดับคงที่ O(log n) แทน O(n) ของ linear search Functions Functionคำอธิบาย bisect_left(a, x, lo=0, hi=len(a))หาตำแหน่งซ้ายสุดที่ insert x ได้ (ก่อน duplicate) bisect_right(a, x, lo=0, hi=len(a))หาตำแหน่ง

  13. Copy ใน Python: Shallow Copy vs Deep Copy

    ใน Python การกำหนดตัวแปร b = a เป็นแค่ reference ชี้ไป object เดียวกัน ไม่ได้ copy ค่า Reference Assignment L1, L2 ([1, 100, 3], [1, 100, 3]) # ทั้งคู่เปลี่ยน! Shallow Copy: copy.copy() สร้าง object ใหม่ แต่ nested objects ยัง reference เดิม L1, L2 ([[1, 2, 3], [100, 5, 6]], [[1, 2, 3], [100, 5

  14. Functools Module ใน Python: Higher-Order Functions และ Callable Operations

    โมดูล functools ให้ utilities สำหรับ Higher-Order Functions - ฟังก์ชันที่รับ/คืนฟังก์ชันอื่น partial() - Freeze Arguments สร้างฟังก์ชันใหม่โดยล็อกอาร์กิวเมนต์บางส่วน part(10) 100 partialmethod() - สำหรับ Method Definition c.alive True cmp_to_key() - แปลง Comparison Function เป็น Key Function Py

  15. html.parser: Simple HTML/XHTML Parser ใน Python

    โมดูล html.parser ให้ HTMLParser class สำหรับ parse HTML/XHTML แบบ event-driven (SAX-style) การใช้งานพื้นฐาน from html.parser import HTMLParser class MyParser(HTMLParser): def handle_starttag(self, tag, attrs): print(fStart tag: {tag}) for attr in attrs: print(f at

  16. urllib.parse: แยกและรวม URL Components ใน Python

    โมดูล urllib.parse จัดการ URL: แยก components, รวมกลับ, encode/decode, query string handling URL Parsing: urlparse() result = urlparse(url) ParseResult(scheme=https, netloc=mail.google.com, path=/mail/u/0/, params=, query=tab=rm, fragment=inbox) Result Attributes AttributeIndexคำอธิ

  17. urllib.robotparser: Parse robots.txt ใน Python

    urllib.robotparser ช่วยอ่านและตีความ robots.txt เพื่อตรวจสอบว่า bot/crawler ควรเข้าถึง URL ไหนได้บ้าง robots.txt Format # robots.txt Sitemap: https://example.com/sitemap.xml User-agent: * Disallow: /admin/ Disallow: /downloads/ Disallow: /media/ Disallow: /static/ RobotFileParser Class from urllib

  18. Enum ใน Python: สร้าง Enumeration ด้วย enum Module

    enum module (Python 3.4+) ให้การสร้าง Enumeration - ชุดค่าคงที่ที่มีชื่อและค่าเฉพาะ ใช้แทน magic numbers/strings สร้าง Enum พื้นฐาน from enum import Enum class Language(Enum): PYTHON = 1 JAVA = 2 CPP = 3 RUBY = 4 การเข้าถึง Members Language.PYTHON repr(Language.PYTHON) Language

  19. ipaddress Module ใน Python: จัดการ IPv4 และ IPv6

    โมดูล ipaddress (Python 3.3+) จัดการ IP Address และ Network แบบ IPv4/IPv6 อย่างถูกต้องและสะดวก Factory Functions (แนะนำให้ใช้) ip_address() - สร้าง Address Object ipaddress.ip_address(2001:ab7::) IPv6Address(2001:ab7::) ip_network() - สร้าง Network Object ipaddress.ip_network(2001:db8:abcd:100:

  20. Textwrap ใน Python: จัดรูปแบบข้อความ Wrap และ Fill

    โมดูล textwrap ช่วยจัดรูปแบบข้อความยาวให้อ่านง่าย: wrap (แยกบรรทัด) และ fill (รวมเป็น string เดียว) ฟังก์ชันหลัก wrap(text, width=70) คืน list of lines แต่ละบรรทัดไม่เกิน width for line in textwrap.wrap(sample, width=50): ... print(line) The textwrap module provides convenience functions, as

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