Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. โปรแกรม Python พิมพ์รูปแบบกระดานหมากรุก n×n ด้วย NumPy

    สร้างเมทริกซ์ n×n แบบ checkerboard pattern โดยใช้ NumPy อัลกอริทึม รับค่า n สร้างเมทริกซ์ n×n ด้วย zeros((n, n), dtype=int) เติม 1 แบบสลับแถวและคอลัมน์ด้วย slicing พิมพ์เมทริกซ์ โค้ดตัวอย่าง import numpy as np def checkboardpattern(n): print(Checkerboard pattern:) x = np.zeros((n, n),

  2. โปรแกรม Python ลบตัวอักษรลำดับที่ n จากสตริง

    สตริงคืออาร์เรย์ของตัวอักษร เริ่มดัชนีที่ 0 สามารถลบตัวอักษรที่ตำแหน่ง n ได้โดยแยกเป็นสองส่วนแล้วรวมกัน ตัวอย่าง Input: python, n=3 Output: pyton อัลกอริทึม รับสตริงเข้า รับดัชนี n ที่ต้องการลบ เก็บตัวอักษรก่อน n ไว้ใน X เก็บตัวอักษรหลัง n ไว้ใน Y คืนค่า X + Y โค้ดตัวอย่าง def removechar(str1,

  3. Truth Value Testing ใน Python

    ออบเจ็กต์ใดๆ ก็สามารถทดสอบค่าความจริงได้ ผ่านคำสั่ง if หรือ while ค่าความจริงเป็น True จนกว่า __bool__() จะคืน False หรือ __len__() คืน 0 ค่าที่ถือว่า False ค่าคงที่: False, None ตัวเลขศูนย์: 0, 0.0, Fraction(0,1), Decimal(0), 0j Sequence/Collection ว่าง: , [], (), {}, set(), range(0) 0 เทียบเท่

  4. Boolean Operations ใน Python: and, or, not

    Boolean operations พื้นฐาน 3 ตัว: and: x and y — คืน x ถ้า x เป็น False, มิฉะนั้นคืน y or: x or y — คืน y ถ้า x เป็น False, มิฉะนั้นคืน x not: not x — คืน True ถ้า x เป็น False, มิฉะนั้นคืน False ตัวอย่างโค้ด x = 25 y = 18 print(x or y) # 25 print(x and y) # 18 print(not x) # False x = 0

  5. Numeric Types ใน Python: int, float, complex

    Python มี numeric types 3 แบบ: integers, floating point, complex (มี real และ imag parts เขียนเป็น a + bj) ฟังก์ชันแปลงชนิด: int(), float(), complex() ยังมี Fraction และ Decimal สำหรับความแม่นยำ ออปอเรชันและฟังก์ชันตัวเลข ออปอเรชัน/ฟังก์ชันคำอธิบาย x + yบวก x - yลบ x * yคูณ x / yหาร x // yหารปัดเ

  6. Iterator Types ใน Python: __iter__ และ __next__

    Iterator ใน Python มีเมธอดสองตัวสำคัญ: __iter__() คืน iterator object ถ้าคลาสรองรับการวนซ้ำหลายแบบ อาจมีเมธอดอื่นเพิ่ม __next__() คืนอิลิเมนต์ถัดไป เมื่อหมดจะ raise StopIteration ตัวอย่าง: Custom Iterator คำนวณ x^x class PowerIter: def __init__(self, max=0): self.max = max def __it

  7. Sequence Types ใน Python: list, tuple, range

    Sequence types พื้นฐานใน Python ได้แก่ list, tuple, range รวมถึง binary data และ text string ออปอเรชันทั่วไปที่ใช้กับ sequence ทั้ง mutable และ immutable: ออปอเรชันคำอธิบาย x in seqTrue ถ้า x อยู่ใน seq x not in seqTrue ถ้า x ไม่อยู่ใน seq x + yรวม sequence สองตัว x * nทำซ้ำ sequence n ครั้ง seq[

  8. Text Sequence Types ใน Python: str

    ใน Python ออบเจ็กต์ str จัดการข้อมูลข้อความ สตริงเป็น immutable และเป็นลำดับของ Unicode characters สามารถกำหนดด้วย single quote, double quotes หรือ triple quotes เมธอดสตริงสำคัญ เมธอดคำอธิบาย capitalize()ตัวอักษรแรกเป็นพิมพ์ใหญ่ center(width[, fillchar])จัดกึ่งกลาง เติมอักขระ (default space) coun

  9. Binary Sequence Types ใน Python: bytes, bytearray, memoryview

    bytes และ bytearray ใช้จัดการข้อมูลไบนารี รองรับ buffer protocol ผ่าน memoryview ซึ่งเข้าถึงหน่วยความจำของออบเจ็กต์ไบนารีอื่นโดยไม่ต้องคัดลอก รูปแบบ Byte Literals bThis is byte with single quote bAnother set of bytes with double quotes bBytes using three single quotes หรือ bBytes using three doub

  10. Set Types ใน Python: set และ frozenset

    Set เป็นคอลเลกชันของออบเจ็กต์ที่ไม่ซ้ำกัน (distinct) และไม่มีลำดับ ใช้สำหรับการดำเนินการทางคณิตศาสตร์เช่น union, intersection, difference และลบค่าซ้ำ Set ไม่บันทึกตำแหน่ง ไม่รองรับ indexing หรือ slicing มี 2 ประเภท: set (mutable) และ frozenset (immutable) เมธอดและออปอเรชันหลัก เมธอด/ออปอเรชันคำอธ

  11. Mapping Types ใน Python: Dictionary

    ออบเจ็กต์ mapping ใช้จับคู่ค่าแฮช (hash table) กับออบเจ็กต์ตามต้องการ ใน Python มี mapping type คือ dictionary ซึ่งเป็น mutable (แก้ไขได้) Key ของ dictionary สามารถเป็นอะไรก็ได้ Value สามารถเป็น list, integer หรือออบเจ็กต์ mutable อื่นๆ เมธอดและออปอเรชันสำคัญ เมธอด/ออปอเรชันคำอธิบาย len(d)จำนวนอิ

  12. Context Manager ใน Python

    Python รองรับ runtime context ผ่านคำสั่ง with โดย context manager จะกำหนดบริบทรันไทม์ มีเมธอดหลัก 2 ตัว: __enter__() เข้าสู่บริบทรันไทม์ คืนค่าออบเจ็กต์ปัจจุบันหรือออบเจ็กต์ที่เกี่ยวข้อง ค่าที่คืนจะถูกผูกกับตัวแปรใน as clause __exit__(exc_type, exc_val, exc_tb) ออกจากบริบท คืนค่า Boolean บ่งบอกว่

  13. คลาสพื้นฐานของข้อยกเว้นใน Python

    ทุกข้อยกเว้นใน Python สืบทอดมาจากคลาส BaseException ข้อยกเว้นที่ผู้ใช้สร้าง (user-defined) ต้องสืบทอดจากคลาส Exception ไม่ใช่ BaseException โดยตรง ลำดับชั้นของข้อยกเว้น (Exception Hierarchy) BaseException +-- Exception +-- ArithmeticError | +-- FloatingPointError | +-- Ov

  14. ข้อยกเว้นพื้นฐานใน Python

    Python มีข้อยกเว้น (exceptions) มาตรฐานหลายประเภทที่มักเกิดขึ้นในโปรแกรม อาจถูกยกระaises โดยโปรแกรมเมอร์หรือโดยตัวอินเทอร์พรีเตอร์เอง ดังนี้: AssertionError เกิดเมื่อคำสั่ง assert ล้มเหลว ใช้สำหรับตรวจสอบเงื่อนไขที่ต้องเป็นจริงเสมอ 50 myObj = MyClass(5) # AssertionError AttributeError เกิดเมื่

  15. การดำเนินการกับสตริงใน Python

    โมดูล string และค่าคงที่ ใน Python มีไลบรารีมาตรฐานชื่อ string ซึ่งประกอบด้วยค่าคงที่ เมธอด และคลาสต่างๆ ที่เกี่ยวข้องกับสตริง เพื่อใช้งาน โมดูลนี้ต้องนำเข้า (import) ก่อน: import string ค่าคงที่สำคัญและค่าที่เก็บอยู่: ลำดับค่าคงที่และค่า 1string.ascii_lowercaseabcdefghijklmnopqrstuvwxyz 2strin

  16. Text Wrapping และ Filling ใน Python (โมดูล textwrap)

    โมดูล textwrap จัดรูปแบบข้อความ: Wrap, Fill, Shorten, Indent ให้พอดีกับ Width ที่กำหนด import textwrap TextWrapper Attributes (Constructor Parameters) AttributeDefaultคำอธิบาย width70Max Line Width expand_tabsTrueExpand Tabs เป็น Spaces tabsize8Tab Size replace_whitespaceTrueReplace Wh

  17. Python Unicode Database (โมดูล unicodedata)

    โมดูล unicodedata ให้เข้าถึง Unicode Character Database (UCD) คุณสมบัติของทุก Unicode Character import unicodedata ฟังก์ชันหลัก Functionคำอธิบาย lookup(name)หา Character จากชื่อ (เช่น LATIN CAPITAL LETTER A) name(chr[, default])ชื่อ Character (Raise ValueError ถ้าไม่มี) decimal(chr[, de

  18. Internet String Preparation ใน Python (โมดูล stringprep)

    โมดูล stringprep ใช้เตรียม Unicode Strings ตาม RFC 3454 ก่อนส่งผ่าน Network ใช้สำหรับ Internationalized Domain Names (IDNA), SASL, XMPP ฯลฯ import stringprep หลักการ Mapping: Map Characters ตามตาราง (เช่น Case Folding) Normalization: Unicode Normalization (NFKC) Prohibition: ตรวจสอบห้ามมี

  19. GNU Readline Interface ใน Python (โมดูล readline)

    โมดูล readline ให้ Interface ไปยัง GNU Readline Library สำหรับ Line Editing, History, Completion ใน Interactive Mode (UNIX Only) import readline **macOS:** ใช้ libedit แทน GNU Readline พฤติกรรมอาจต่างกัน History Management Functionคำอธิบาย read_history_file([filename])โหลด History (Default:

  20. Helpers for Computing Deltas ใน Python (โมดูล difflib)

    โมดูล difflib ให้ Classes/Functions สำหรับเปรียบเทียบ Sequences (Strings, Lines, Files) และสร้าง Diff/Delta import difflib Class: SequenceMatcher เปรียบเทียบ 2 Sequences ใดๆ (ไม่ใช่แค่ Text) เมธอดหลัก Methodคำอธิบาย set_seqs(a, b)ตั้งค่า Sequences ทั้งคู่ set_seq1(a) / set_seq2(b)ตั้งค่า

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:76/451  20-คอมพิวเตอร์/Page Goto:1 70 71 72 73 74 75 76 77 78 79 80 81 82