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

    List และ Tuple เรียกว่า Sequence Data Types ของ Python ทั้งคู่เป็น Comma-separated Collection ไม่จำเป็นต้องชนิดเดียวกัน ความแตกต่างหลัก: List Object เป็น Mutable ส่วน Tuple Object เป็น Immutable Immutable Object ไม่สามารถแก้ไขได้หลังสร้างใน Memory จึงไม่สามารถ Add, Modify, Remove Item จาก Tuple ได

  2. ความแตกต่างและความคล้ายคลึงระหว่าง Tuple และ List ใน Python

    List และ Tuple เรียกว่า Sequence Data Types ของ Python ทั้งคู่เป็น Comma-separated Collection ของ Items ไม่จำเป็นต้องชนิดเดียวกัน ความคล้ายคลึง (Similarities) Concatenation, Repetition, Indexing และ Slicing ทำได้ทั้งคู่ # List Operations L1=[1,2,3]; L2=[4,5,6] L3=L1+L2 # Concatenation: [1,2

  3. วิธีหา Index ของ Item ใน List ใน Python

    ตำแหน่งของ Element ใน List (Sequence Data Types ทุกประเภท) ได้จากเมธอด index() เมธอดนี้หา Instance แรกที่พบของ Element ที่กำหนด >>> L1=[45, 32, 100, 10, 24, 56] >>> L1.index(24) 4

  4. วิธีลบ Element จาก List ด้วย Index ใน Python

    มี 2 ทางเลือกในการลบ Element ด้วย Index ใน List: ใช้ del Statement และใช้ pop() Method 1. ใช้ del Statement ต้องการ Index ของ Element ที่จะลบ >>> L1=[11,22,33,44,55,66,77] >>> del L1[2] >>> L1 [11, 22, 44, 55, 66, 77] 2. ใช้ pop() Method ต้องการ Index เป็น Argument เมธอด

  5. วิธี Join List of Lists ใน Python (Flatten List)

    มีหลายวิธี Flatten List of Lists: 1. Nested Loops (Straightforward) L1=[[1,2],[3,4,5],[6,7,8,9]] flat=[] for i in L1: for j in i: flat.append(j) print(flat) 2. Generator Function def flatten(lst): for i in lst: for j in i: yield j L1=[[1,2,3],[4,5],[6,7,8,9]] f

  6. วิธีลบ Index List จาก List อื่นใน Python

    มี 2 ลิสต์: L1 ลิสต์ต้นทางที่จะลบ Element และ L2 ลิสต์ที่เก็บ Indices ที่จะลบ >>> L1=[1, 2, 3, 4, 5, 6, 7, 8, 9] >>> L2=[2, 4, 6] สร้าง Enumerate Object จาก L1 enumerate() คืน Collection ของ Tuple 2 ค่า (Index, Element) วน For Loop 2 ตัวแปรเปรียบเทียบ Index กับ Element ใน L2 หากเจ

  7. วิธีตรวจสอบว่าไฟล์มีอยู่จริงใน Python

    ตรวจสอบไฟล์ได้ 2 วิธี: วิธีที่ 1: ใช้ os.path.isfile() ฟังก์ชันคืน True หากไฟล์ที่ Path ระบุมีอยู่ False หากไม่มี >>> import os >>> os.path.isfile(d:\\Package1\\package1\\fibo.py) True >>> os.path.isfile(d:/Package1/package1/fibo.py) True >>> os.path.isfile(d:\\n

  8. ความแตกต่างระหว่าง __str__ และ __repr__ ใน Python

    Built-in Functions repr() และ str() เรียก object.__repr__(self) และ object.__str__(self) ตามลำดับ ฟังก์ชันแรกคำนวณ Official Representation ฟังก์ชันที่สองคืน Informal Representation ผลลัพธ์เหมือนกันสำหรับ Integer Object: >>> x = 1 >>> repr(x) 1 >>> str(x) 1 แต่ต่างกันสำหร

  9. คีย์เวิร์ด yield ใน Python ทำอะไร

    yield ใช้ใน Generator เพื่อทำความเข้าใจพฤติกรรม เริ่มจากดู Iterables ก่อน: Python Objects เช่น List, File, String เรียกว่า Iterables Object ใดๆ ที่วนลูปด้วย for ... in ได้ คือ Iterable Iterator Object ก็เป็น Iterable แต่ Iterate ได้เพียงครั้งเดียว ได้ Iterator จาก Iterable ด้วย iter() มีเมธอด next()

  10. วิธี Pop Element แรกออกจาก Tuple ใน Python

    โดยนิยาม Tuple Object เป็น Immutable จึงไม่สามารถลบ Element ได้ อย่างไรก็ตาม Workaround คือ แปลง Tuple เป็น List ลบ Element ที่ต้องการจาก List แล้วแปลงกลับเป็น Tuple >>> T1=(1,2,3,4) >>> L1=list(T1) >>> L1.pop(0) 1 >>> L1 [2, 3, 4] >>> T1=tuple(L1) >&

  11. วิธีทำ Tuple Slicing ใน Python

    Slicing Operator ใช้กับ Sequence Data Types ทุกประเภท รวมถึง Tuple Slicing หมายถึงการแยกส่วนของ Sequence ที่นี่คือ Tuple สัญลักษณ์คือ : Operator ต้องการ 2 Operands: Operand แรก = Index ของ Element เริ่มต้น Operand ที่สอง = Index ของ Element สุดท้ายใน Slice + 1 ผลลัพธ์ก็เป็น Tuple >>> T1=(

  12. วิธี Append Element ใน Python Tuple

    Python Tuple เป็น Immutable Object การดำเนินการที่พยายามแก้ไข (เช่น append) ไม่ได้รับอนุญาต แต่สามารถใช้ Workaround ได้ 1. แปลง Tuple เป็น List ด้วย list() 2. Append Item ใน List Object 3. แปลงกลับเป็น Tuple ด้วย tuple() >>> T1=(10,50,20,9,40,25,60,30,1,56) >>> L1=list(T1) >&g

  13. Python Tuple เป็น Immutable ทำไมจึงเพิ่มค่าได้

    Python Tuple เป็น Immutable Object การดำเนินการที่พยายามแก้ไข (เช่น append/insert) ไม่ได้รับอนุญาต อย่างไรก็ตาม สามารถใช้ Workaround ได้ 1. แปลง Tuple เป็น List ด้วย list() Built-in Function 2. Append หรือ Insert Item ใน List Object ได้ตามต้องการ 3. แปลงกลับเป็น Tuple ด้วย tuple() Built-in Function

  14. วิธีเข้าถึง Class Attributes ด้วย Dot Operator ใน Python

    Class Attribute คือ Attribute ของคลาส ไใช่ Attribute ของ Instance ในโค้ดด้านล่าง class_var เป็น Class Attribute ส่วน i_var เป็น Instance Attribute: ทุก Instance เข้าถึง class_var ได้ ซึ่งเข้าถึงเป็น Property ของคลาสโดยตรงก็ได้ ตัวอย่าง class MyClass(object): class_var = 2 def __init__(self

  15. Dot Operator (.) ใน Python คืออะไร

    เกือบทุกอย่างใน Python เป็น Object ทุก Object มี Attributes และ Methods การเชื่อมต่อระหว่าง Attributes/Methods กับ Object แสดงด้วย Dot (.) เขียนระหว่างกัน ตัวอย่าง: หาก dog เป็นคลาส แล้ว Fido เป็น Instance/Object ของมัน class Dog: pass Fido = Dog() ถ้าเมธอดของคลาสคือ eats(), runs(), sleeps() ส

  16. วิธีทำลายออบเจ็กต์ใน Python

    คลาส Implement เมธอดพิเศษ __del__() เรียกว่า Destructor เรียกตอน Instance จะถูกทำลาย ใช้ Cleanup Non-memory Resources ที่อินสแตนซ์ใช้ ตัวอย่าง class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __del__(self): class_name = self.__class__.__name__

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