Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. ความแตกต่างระหว่าง datetime.now() และ datetime.today() ใน Python

    datetime.now() รับ tzinfo เป็น Keyword Argument แต่ datetime.today() ไม่รับ Keyword Arguments เลย จากเอกสาร: datetime.now() คืน Local Date และ Time ปัจจุบัน หาก Argument tz เป็น None หรือไม่ระบุ จะเหมือน today() แต่ให้ Precision สูงกว่า (อาจได้จาก C gettimeofday())

  2. Nested Functions ใน Python ทำงานอย่างไร

    เรียนรู้ Nested Function จากโค้ด: Inner Function เข้าถึง Variable จาก Enclosing Scope (Local Variable ของ Outer Function) ได้ def mulFunc(num1): def mul(num2): return num1 * num2 return mul res = mulFunc(15) print(res(20)) # 300 (20 * 15) Inner Function mul จดจำ num1 จาก Outer Sc

  3. วิธีเปรียบเทียบ String 2 ตัวด้วย Regex ใน Python

    เปรียบเทียบ String ที่กำหนดด้วยโค้ด: ตัวอย่าง import re s1 = Pink Forest s2 = Pink Forrest if bool(re.search(s1, s2)) == True: print(Strings match) else: print(Strings do not match) ผลลัพธ์ Strings do not match re.search(pattern, string) ค้นหา Pattern ใน String คืน Match Object หรือ None

  4. วิธีหา Length ของ List of Lists ใน Python

    ใช้ Nested Loop นับจำนวน Elements ในแต่ละ Sublist >>> a=[[1, 2, 3], [4, 5, 6]] >>> c=0 >>> for x in a: ... for y in x: ... c=c+1 >>> c 6 วิธีอื่น: ใช้ sum(len(sublist) for sublist in a) หรือ len(list(itertools.chain.from_iterable(a)))

  5. วิธีนิยาม Lists ใน Python

    List เป็น Sequence Data Type ใน Python เป็น Comma-separated List ของ Elements (ไม่จำเป็นชนิดเดียวกัน) ใน Square Brackets [ ] List เป็น Ordered Collection เข้าถึง Element แต่ละตัวด้วย Zero-based Index ตัวอย่าง list1 = [physics, chemistry, 1997, 2000] list2 = [1, 2, 3, 4, 5] list3 = [a, b, c, d] Emp

  6. วิธีที่ดีที่สุดจัดการ List Empty Exception ใน Python

    List เป็น Ordered Sequence เข้าถึง Element ด้วย Index 0 ถึง Length-1 หาก Index เกิน Range จะได้ IndexError Exception ตัวอย่าง: ใช้ Infinite Loop Pop Element ทีละตัว Loop พยายามไปหลัง Pop จนหมด จะได้ IndexError จับด้วย Try-Except a=[1,2,3] while True: try: b=a.pop() print(b)

  7. วิธี Iterate ผ่าน List ใน Python

    มีหลายวิธี Iterate ผ่าน List: 1. For Loop (Foreach Style) for var in list: stmt1 stmt2 ตัวอย่าง L=[10,20,30,40,50] for var in L: print(L.index(var), var) ผลลัพธ์ 0 10 1 20 2 30 3 40 4 50 2. Range + Index for var in range(len(L)): print(var, L[var]) 3. Enumerate for var in enumer

  8. วิธี Iterate ผ่าน Tuple ใน Python

    มีหลายวิธี Iterate ผ่าน Tuple: 1. For Loop (Foreach Style) for var in tuple: stmt1 stmt2 ตัวอย่าง T = (10,20,30,40,50) for var in T: print(T.index(var), var) ผลลัพธ์ 0 10 1 20 2 30 3 40 4 50 2. Range + Index for var in range(len(T)): print(var, T[var]) 3. Enumerate for var in en

  9. วิธี Iterate ผ่าน List of Tuples ใน Python

    วิธีง่ายที่สุด: ใช้ Nested For Loops 2 ชั้น Outer Loop ดึง Tuple แต่ละอัน Inner Loop วน Item ใน Tuple ใช้ print(y, end= ) พิมพ์ Item ใน Tuple เดียวกันบรรทัด print() ขึ้นบรรทัดใหม่หลัง Tuple แต่ละอัน ตัวอย่าง L=[(1,2,3), (4,5,6), (7,8,9,10)] for x in L: for y in x: print(y, end= ) pr

  10. วิธีเขียน Multiple Statements บนบรรทัดเดียวใน Python

    หลาย Statement ใน Block Indent เดียวกันเป็น Compound Statement ปกติเขียนบรรทัดละ Statement แต่เขียนบรรทัดเดียวได้ถ้าคั่นด้วย Semicolon a=10 b=20 c=a*b print(c) เขียนบรรทัดเดียว: a=10; b=20; c=a*b; print(c) Block ใหม่ Indent มากขึ้นมักเริ่มหลัง : (if, else, while, for, try) แต่ก็เขียนบรรทัดเดียวได

  11. Python สามารถ Assign Reference ให้ Variable ได้หรือไม่

    แนวคิด Variable ใน Python ต่างจาก C/C++ ใน C/C++ Variable คือ Named Memory Location แม้ Assign ค่าตัวหนึ่งให้อีกตัว ก็สร้าง Copy ใน Location ใหม่ int x=5; int y=x; ใน C++ Operator & คืน Address ของ Variable cout << x << &x << y << &y; จะพิมพ์ Address ต่างกัน แม้

  12. วิธี Find and Replace ใน Text File ด้วย Python

    โค้ดต่อไปนี้ทำ Replacement ใน Text File ที่กำหนด หลัง Replace เขียนไปยังไฟล์ใหม่ bar.txt ตัวอย่าง f1 = open(foo.txt, r) f2 = open(bar.txt, w) for line in f1: print(line) f2.write(line.replace(Poetry, Prose)) f2 = open(bar.txt, r) for line in f2: print(line, end=) f1.close() f2.close(

  13. วิธีอ่านและเขียน Unicode (UTF-8) Files ใน Python

    โมดูล io แนะนำให้ใช้ ตรงกับ Python 3s Open Syntax: ตัวอย่าง import io # อ่าน UTF-8 with io.open(filename, r, encoding=utf8) as f: text = f.read() # process Unicode text # เขียน UTF-8 with io.open(filename, w, encoding=utf8) as f: f.write(text) ใช้ with Statement ปิดไฟล์อัตโนมัติ รองร

  14. วิธี Validate URL ด้วย Regular Expression ใน Python

    ไม่มี Validate Method มาตรฐาน เพราะเกือบทุกอย่างเป็น Valid URL ได้ มี Punctuation Rules สำหรับแยกส่วน ถ้าไม่มี Punctuation ก็ยังเป็น Valid URL ขึ้นอยู่กับสถานการณ์ ใช้วิธีต่างๆ: Trust Data แค่เช็ค Protocol HTTP: ใช้ urlparse ต้องการ True URL จริง: ใช้ Regex ที่ซับซ้อน ต้องการเช็คว่าเป

  15. วิธี Validate Email ด้วย Regular Expression ใน Python

    โค้ดต่อไปนี้ Validate Email ID ใดๆ ด้วย Regex ใน Python ตัวอย่าง import re s = manogna.neelam@tutorialspoint.com match = re.search(r\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b, s, re.I) print(match.group()) ผลลัพธ์ manogna.neelam@tutorialspoint.com Pattern: \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\

  16. วิธีดึง Substring จาก String ใน Python

    ใน Python String เป็น Ordered Sequence ของ Characters Sequence Types ทั้งหมดรองรับ Slicing ใช้ Slice Operator : ดึง Substring จาก String หลัก ตัวอย่าง >>> string=Hello How are you? >>> string[6:9] How Slice Operator ต้องการ 2 Operands: Operand แรก = Index ตัวแรกของ Substring ที่ต

  17. วิธีดึง 4 ตัวอักษรสุดท้ายของ String ใน Python

    Slice Operator ใน Python รับ 2 Operands: Operand แรก = เริ่มต้น Slice (นับจากซ้าย Default) Operand ลบ = นับจากขวา Operand ที่สอง = Index ตัวสุดท้ายใน Slice + 1 หากละเว้น = ไปจนถึงท้าย ต้องการ 4 ตัวสุดท้าย ใช้ -4 เป็น Operand แรก และละ Operand ที่สอง >>> string = Thanks. I am fine >>

  18. วิธี Sort List of Dicts ตาม Value ใน Python

    Python มี Built-in Function sorted() เรียง Element ของ Iterable ตาม Order ที่กำหนด sorted(iterable[, key][, reverse]) Parameter ที่ 2: Function คืนค่าเป็น Key สำหรับ Sort Parameter ที่ 3: Default False หาก True เรียง Descending ใช้ Lambda Function กำหนด Dictionary Key Value เป็น Key สำหรับ Sorted

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:43/451  20-คอมพิวเตอร์/Page Goto:1 37 38 39 40 41 42 43 44 45 46 47 48 49