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

    Star * operator unpacks sequence/collection เป็น positional arguments หากมี tuple ต้องการส่ง items เป็น arguments ตามตำแหน่ง แทนที่จะ index ทีละตัว ใช้ * operator ตัวอย่าง def multiply(a, b): return a * b values = (1, 2) print(multiply(*values)) Unpack tuple ให้ execute จริงเป็น: print(multipl

  2. การทำงานของ In Operator บน Tuple ใน Python

    Operator in ของ Python ให้ loop ผ่าน members ของ collection (list/tuple) ตรวจสอบว่ามี member เท่ากับ item ที่ให้มาหรือไม่ ตัวอย่าง my_tuple = (5, 1, 8, 3, 7) print(8 in my_tuple) print(0 in my_tuple) ผลลัพธ์ True False หมายเหตุ: in ต่อ dictionary ตรวจสอบ key ตัวอย่าง my_dict = {name: TutorialsPoin

  3. การทำงานของ Concatenation Operator บน Tuple ใน Python

    Concatenation operator สร้าง tuple ใหม่จาก tuple เริ่มต้นตามลำดับที่บวกเข้ามา ไม่ใช่ in-place operation ตัวอย่าง tuple1 = [1, 2, 3] tuple2 = [a, b] tuple3 = tuple1 + tuple2 print(tuple3) ผลลัพธ์ [1, 2, 3, a, b]

  4. การทำงานของ Repetition Operator บน Tuple ใน Python

    ปกติ * แทนคูณ แต่ถ้า operand ซ้ายเป็น tuple จะกลายเป็น repetition operator ทำหลาย copies ของ tuple แล้ว join เข้าด้วยกัน สร้าง tuple ด้วย repetition operator * ตัวอย่าง numbers = (0,) * 5 # comma บอกว่าเป็น single-valued tuple ไม่ใช่ expression print(numbers) ผลลัพธ์ (0, 0, 0, 0, 0) (0,) tuple มี

  5. การทำงานของ Del Operator บน Tuple ใน Python

    ไม่สามารถลบ tuple elements แยกต่างหากได้ สร้าง tuple ใหม่โดยไม่รวม elements ที่ไม่ต้องการด้วย list comprehensions หรือ slicing ได้ เพื่อลบ tuple ทั้งอ็อบเจกต์ ใช้ del statement ตัวอย่าง tup = (physics, chemistry, 1997, 2000) print(tup) del(tup) print(After deleting tup : ) print(tup) จะได้ exceptio

  6. วิธี Index และ Slice Tuple ใน Python

    Index หรือ slice tuple ใช้ operator [] บน tuple index บวกดึงจากซ้าย index ลบดึงจากขวา ตัวอย่าง my_tuple = (a, b, c, d) print(my_tuple[1]) print(my_tuple[-1]) ผลลัพธ์ b d ดึงส่วนของ tuple ใช้ slicing [start:stop:step] ตัวอย่าง my_tuple = (a, b, c, d) print(my_tuple[1:]) # index 1 ถึงสุด print(my_

  7. วิธีลบ Elements ทั้งหมดของ Dictionary ใน Python

    ลบ elements ทั้งหมดจาก dictionary วิธีง่ายสุดคือ reassign ให้เป็น empty dictionary ตัวอย่าง my_dict = {name: foo, age: 28} my_dict = {} print(my_dict) ผลลัพธ์ {} อีกวิธีใช้ del ลบ key เฉพาะตัว หรือ loop ลบทุก key: ตัวอย่าง my_dict = {name: foo, age: 28} keys = list(my_dict.keys()) for key in keys:

  8. วิธีได้ Value จาก Key ใน Python Dictionary

    ได้ value จาก key ใน Python dictionary ด้วย operator [] ส่ง key เป็น argument ตัวอย่าง my_dict = {name: TutorialsPoint, time: 15 years, location: India} print(my_dict[name]) print(my_dict[time]) ผลลัพธ์ TutorialsPoint 15 years อีกวิธีใช้เมธอด get บน dictionary: ตัวอย่าง my_dict = {name: TutorialsP

  9. วิธีได้รายการ Keys ทั้งหมดจาก Python Dictionary

    ได้ list ของ keys ทั้งหมดจาก dictionary ด้วย dict.keys() ตัวอย่าง my_dict = {name: TutorialsPoint, time: 15 years, location: India} key_list = list(my_dict.keys()) print(key_list) ผลลัพธ์ [name, time, location] อีกวิธีใช้ list comprehension: ตัวอย่าง my_dict = {name: TutorialsPoint, time: 15 years

  10. วิธีได้รายการค่าทั้งหมดจาก Python Dictionary

    ได้ list ของ values ทั้งหมดจาก dictionary ด้วย dict.values() ตัวอย่าง my_dict = {name: TutorialsPoint, time: 15 years, location: India} value_list = list(my_dict.values()) print(value_list) ผลลัพธ์ [TutorialsPoint, 15 years, India] อีกวิธีใช้ list comprehension: ตัวอย่าง my_dict = {name: Tutorials

  11. Tick ใน Python คืออะไร

    เลขทศนิยมในหน่วยวินาทีสำหรับ time interval เรียกว่า Tick ใน Python จุดเวลาเฉพาะแสดงเป็นวินาทีนับจาก 12:00am, January 1, 1970 (epoch) ใช้โมดูล time สำหรับฟังก์ชันทำงานกับเวลา และแปลงระหว่าง representations ตัวอย่าง พิมพ์เวลาปัจจุบันเป็น ticks: ตัวอย่าง import time ticks = time.time() print(Ticks sinc

  12. วิธีได้ Timer Ticks ณ เวลาที่กำหนดใน Python

    ได้ timer ticks ณ เวลาที่กำหนดด้วย clock เอกสารแนะนำใช้สำหรับ benchmarking ตัวอย่าง import time t0 = time.clock() print(Hello) t1 = time.clock() - t0 print(Time elapsed:, t1 - t0) # CPU seconds elapsed (floating point) ผลลัพธ์ Time elapsed: 0.0009403145040156798 ระบบต่างกันมีความแม่นยำต่างกันตาม

  13. TimeTuple ใน Python คืออะไร

    เมธอด timetuple() ของ datetime.date instances คืน object ประเภท time.struct_time ซึ่งเป็น named tuple object (เข้าถึงได้โดย index หรือชื่อ) struct_time object มี attributes สำหรับ date และ time fields พร้อม flag Daylight Saving Time named tuple ที่คืนจาก timetuple() จะมี year, month, day ตาม date ob

  14. วิธีพิมพ์ TimeTuple ทั้งหมดใน Python

    เมธอด timetuple() ของ datetime.date instances คืน object ประเภท time.struct_time ซึ่งเป็น named tuple object (เข้าถึงได้ทั้งโดย index และชื่อ) ตัวอย่าง import datetime todaysDate = datetime.date.today() timeTuple = todaysDate.timetuple() print(timeTuple) ผลลัพธ์ time.struct_time(tm_year=2017, tm_mo

  15. วิธีได้วันที่และเวลาปัจจุบันใน Python

    ได้วันที่และเวลาปัจจุบันหลายวิธี วิธีง่ายสุดใช้โมดูล datetime มีฟังก์ชัน now คืน current date และ time ตัวอย่าง import datetime now = datetime.datetime.now() print(Current date and time: ) print(str(now)) ผลลัพธ์ 2017-12-29 11:24:48.042720 จัดรูปแบบวันที่เวลาด้วย strftime รับ format string ดีเรกที

  16. วิธีได้วันที่และเวลาที่จัดรูปแบบแล้วใน Python

    ได้ formatted date และ time ด้วยฟังก์ชัน strftime รับ format string เพื่อผลลัพธ์ที่ต้องการ ดีเรกทีฟที่รองรับ: Directiveความหมาย %aชื่อวันสัปดาห์ย่อตาม locale %Aชื่อวันสัปดาห์เต็มตาม locale %bชื่อเดือนย่อตาม locale %Bชื่อเดือนเต็มตาม locale %cการแสดงวันที่และเวลาที่เหมาะสมตาม locale %dวันของเดือน

  17. วิธีพิมพ์ปฏิทินเดือนใน Python

    ใช้โมดูล calendar ได้ปฏิทินเดือนที่กำหนดของปีที่กำหนด ใส่ year และ month เป็น arguments ตัวอย่าง import calendar y = 2017 m = 11 print(calendar.month(y, m)) ผลลัพธ์ November 2017 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

  18. วิธีลบ 1 วันจาก Python Date

    ลบวันจาก Python date ด้วย timedelta สร้าง timedelta ด้วยจำนวนเวลาที่ต้องการลบ แล้วลบออกจาก date ตัวอย่าง from datetime import datetime, timedelta today = datetime.today() yesterday = today - timedelta(days=1) print(today) print() print(yesterday) ผลลัพธ์ 2017-12-29 12:28:06.531791 2017-12-28 12:2

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:36/451  20-คอมพิวเตอร์/Page Goto:1 30 31 32 33 34 35 36 37 38 39 40 41 42