Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore 3-หลักการเขียนโปรแกรม-13-คู่มือ ProgrammingExpertwithPython-ภาษาไทย

3-หลักการเขียนโปรแกรม-13-คู่มือ ProgrammingExpertwithPython-ภาษาไทย

Published by t.panida.noisri, 2022-10-27 12:10:34

Description: 3-หลักการเขียนโปรแกรม-13-คู่มือ ProgrammingExpertwithPython-ภาษาไทย

Search

Read the Text Version

atan(-1) : -0.7853981633974483 atan(1) : 0.7853981633974483  atan2() คานวณค่า atan(y / x) ค่าทไ่ี ดเ้ ป็น radians รปู แบบคาสงั่ : atan2(y, x) พารามเิ ตอร:์ x และ y เป็นเลขจานวนใดๆ ค่าทส่ี ่งกลบั : คา่ atan2(y / x) ทไ่ี ดเ้ ป็น radian ตวั อยา่ ง: import math print (\"atan2(-0.50,-0.50) : \", math.atan2(-0.50,-0.50)) print (\"atan2(0.50,0.50) : \", math.atan2(0.50,0.50)) print (\"atan2(5,5) : \", math.atan2(5,5)) print (\"atan2(-10,10) : \", math.atan2(-10,10)) print (\"atan2(10,20) : \", math.atan2(10,20)) atan2(-0.50,-0.50) : -2.356194490192345 atan2(0.50,0.50) : 0.7853981633974483 OUTPUT atan2(5,5) : 0.7853981633974483 atan2(-10,10) : -0.7853981633974483 atan2(10,20) : 0.4636476090008061  cos() คานวณคา่ cosine ของ x รปู แบบคาสงั่ : acos(x) พารามเิ ตอร:์ x คอื เลขจานวนใดๆ คา่ ทส่ี ่งกลบั : ค่า cosine ทไ่ี ดเ้ ป็น radians อยใู่ นช่วง -1 ถงึ 1 ตวั อยา่ ง: import math print (\"cos(3) : \", math.cos(3)) print (\"cos(-3) : \", math.cos(-3)) print (\"cos(0) : \", math.cos(0)) print (\"cos(math.pi) : \", math.cos(math.pi)) print (\"cos(2*math.pi) : \", math.cos(2*math.pi)) cos(3) : -0.9899924966004454 cos(-3) : -0.9899924966004454 OUTPUT cos(0) : 1.0 cos(math.pi) : -1.0 cos(2*math.pi) : 1.0  hypot() คานวณค่า Euclidean norm รปู แบบคาสงั่ : hypot(x, y) พารามเิ ตอร:์ โดยคา่ x และ y เป็นเลขจานวนเตม็ คา่ ทส่ี ง่ กลบั : ค่า Euclidean norm ทถ่ี กู คานวณดว้ ยสมการ sqrt(x*x + y*y) ตวั อยา่ ง: import math print (\"hypot(3, 2) : \", math.hypot(3, 2)) หน้า 580

print (\"hypot(-3, 3) : \", math.hypot(-3, 3)) print (\"hypot(0, 2) : \", math.hypot(0, 2)) hypot(3, 2) : 3.605551275463989 hypot(-3, 3) : 4.242640687119285 OUTPUT hypot(0, 2) : 2.0  sin() คานวณคา่ sine ของ x รปู แบบคาสงั่ : sin(x) พารามเิ ตอร:์ x คอื เลขจานวนใดๆ ค่าทส่ี ่งกลบั : ค่า sine ทไ่ี ดเ้ ป็น radians อยใู่ นชว่ ง -1 ถงึ 1 ตวั อยา่ ง: import math print (\"sin(3) : \", math.sin(3)) print (\"sin(-3) : \", math.sin(-3)) print (\"sin(0) : \", math.sin(0)) print (\"sin(math.pi) : \", math.sin(math.pi)) print (\"sin(math.pi/2) : \", math.sin(math.pi/2)) sin(3) : 0.1411200080598672 sin(-3) : -0.1411200080598672 OUTPUT sin(0) : 0.0 sin(math.pi) : 1.2246467991473532e-16 sin(math.pi/2) : 1.0cos(2*math.pi) : 1.0  tan() คานวณค่า tangent ของ x รปู แบบคาสงั่ : tan(x) พารามเิ ตอร:์ x คอื เลขจานวนใดๆ คา่ ทส่ี ง่ กลบั : ค่า arc cosine ค่าทไ่ี ดเ้ ป็น radians อยใู่ นช่วง -1 ถงึ 1 ตวั อยา่ ง: import math print (\"tan(3) : \", math.tan(3)) print (\"tan(-3) : \", math.tan(-3)) print (\"tan(0) : \", math.tan(0)) print (\"tan(math.pi) : \", math.tan(math.pi)) print (\"tan(math.pi/2) : \", math.tan(math.pi/2)) print (\"tan(math.pi/4) : \", math.tan(math.pi/4)) tan(3) : -0.1425465430742778 tan(-3) : 0.1425465430742778 OUTPUT tan(0) : 0.0 tan(math.pi) : -1.2246467991473532e-16 tan(math.pi/2) : 1.633123935319537e+16 tan(math.pi/4) : 0.9999999999999999  degrees() แปลงคา่ จาก radians เป็น degree รปู แบบคาสงั่ : degrees(x) พารามเิ ตอร:์ x เป็นเลขจานวนใดๆ หน้า 581

คา่ ทส่ี ง่ กลบั : ค่า degree ของ radians x ตวั อยา่ ง: import math print (\"degrees(3) : \", math.degrees(3)) print (\"degrees(-3) : \", math.degrees(-3)) print (\"degrees(0) : \", math.degrees(0)) print (\"degrees(math.pi) : \", math.degrees(math.pi)) print (\"degrees(math.pi/2) : \", math.degrees(math.pi/2)) print (\"degrees(math.pi/4) : \", math.degrees(math.pi/4)) degrees(3) : 171.88733853924697 degrees(-3) : -171.88733853924697 OUTPUT degrees(0) : 0.0 degrees(math.pi) : 180.0 degrees(math.pi/2) : 90.0 degrees(math.pi/4) : 45.0  radians() แปลงคา่ จาก degree เป็น radians รปู แบบคาสงั่ : radians(x) พารามเิ ตอร:์ x เป็นเลขจานวนใดๆ ค่าทส่ี ่งกลบั : ค่า radians ของ degree x ตวั อยา่ ง: import math print (\"radians(3) : \", math.radians(3)) print (\"radians(-3) : \", math.radians(-3)) print (\"radians(0) : \", math.radians(0)) print (\"radians(math.pi) : \", math.radians(math.pi)) print (\"radians(math.pi/2) : \", math.radians(math.pi/2)) print (\"radians(math.pi/4) : \", math.radians(math.pi/4)) OUTPUT radians(3) : 0.05235987755982989 radians(-3) : -0.05235987755982989 radians(0) : 0.0 radians(math.pi) : 0.05483113556160755 radians(math.pi/2) : 0.027415567780803774 radians(math.pi/4) : 0.013707783890401887 ค่าคงที่ทางคณิตศาสตร์ (Mathematical constants) ค่าคงทท่ี ใ่ี ชใ้ นทางคณติ ศาสตร์ ทน่ี ยิ มมี 2 ตวั คอื ค่า pi = 3.14159 และ e = 2.71828 math.pi, math.e เมธอดและฟังชนั ท่ีดาเนินการกบั สตริง (Built-in String Methods) หน้า 582

สมมตุ ใิ ห้ str เป็นสตรงิ ใดๆ เชน่ str = \"Python\" หรอื str = 'Programming'  str.capitalize() แปลงตวั อกั ษรตวั แรกในสตรงิ ใหเ้ ป็นตวั ใหญ่ รปู แบบคาสงั่ : str.capitalize() พารามเิ ตอร:์ ไมม่ ี ค่าทส่ี ่งกลบั : ขอ้ ความสตรงิ ทม่ี อี กั ษรตวั แรกเป็นตวั ใหญ่ ตวั อยา่ ง: str = \"this is string example....wow!!!\" print (\"str.capitalize() : \", str.capitalize()) str.capitalize() : This is string example....wow!!! OUTPUT  str.center() จดั เรยี งสตรงิ ใหอ้ ยตู่ รงกลาง โดยเตมิ อกั ษร (fillchar) ดา้ นซา้ ย และดา้ นขวา ของสตรงิ ใหค้ รบจานวนความกวา้ งทก่ี าหนด (width) รปู แบบคาสงั่ : str.center(width, fillchar) พารามเิ ตอร:์ width ความกวา้ งของสตรงิ , fillchar คอื ตวั อกั ษรทต่ี อ้ งการเตมิ ในสตรงิ ค่าทส่ี ง่ กลบั : ขอ้ ความสตรงิ ทอ่ี ยตู่ รงกลางระหวา่ งตวั อกั ษรทเ่ี ตมิ เขา้ ไป ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (\"str.center(40, 'x'):\", str.center(40, 'x')) str.center(40,'x'):xxxxthis is string example....wow!!!xxxx OUTPUT  str.count() คน้ หากลุม่ คาทต่ี อ้ งการ รปู แบบคาสงั่ : str.count(str, beg= 0,end=len(string)) พารามเิ ตอร:์ str คอื กลุ่มคาทต่ี อ้ งการคน้ หา, beg คอื ตาแหน่งเรมิ่ ตน้ ทต่ี อ้ งการคน้ หา , end คอื ตาแหน่งสุดทา้ ยทต่ี อ้ งการคน้ หา ถา้ ไม่รตู้ าแหน่งสุดทา้ ยของ สตรงิ สามารถใช้ len() เพอ่ื หาความยาวสตรงิ (อกั ษรตวั แรกของสตรงิ เรม่ิ ตน้ ท่ี 0) คา่ ทส่ี ง่ กลบั : จานวนนับของกลมุ่ คานนั้ ๆ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; sub = \"i\"; print(\"str.count(sub,4,40):\",str.count(sub, 4, 40)) sub = \"wow\"; print (\"str.count(sub):\",str.count(sub)) )) หน้า 583

str.count(sub, 4, 40) : 2 str.count(sub) : 1 OUTPUT  str.decode() ถอดรหสั สตรงิ ทถ่ี ูกเขา้ รหสั ไว้ รปู แบบคาสงั่ : str.decode(encoding='UTF-8',errors='strict') พารามเิ ตอร:์ encoding คอื แบบของภาษาทใ่ี ชถ้ อดรหสั เช่น UTF-8, ISO, Latin เป็นตน้ , error คอื ขอ้ กาหนดเมอ่ื เกดิ ขอ้ ผดิ พลาดขน้ึ ขณะถอดรหสั เช่น strict, ignore, replace สาหรบั strict เป็นค่า default หมายถงึ ใหส้ นใจ ขอ้ ผดิ พลาดในการถอดรหสั คา่ ทส่ี ่งกลบั : ขอ้ ความทถ่ี ูกถอดรหสั แลว้ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; # This is encoding str = str.encode('base64','strict'); print(\"Encoded String:\" + str) print(\"Decoded String:\" + tr.decode('base64','strict')) Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE= Decoded String: this is string example....wow!!! OUTPUT  str.encode() เขา้ รหสั สตรงิ รปู แบบคาสงั่ : str.encode(encoding='UTF-8',errors='strict') พารามเิ ตอร:์ encoding คอื แบบอกั ษรทใ่ี ชเ้ ขา้ รหสั เชน่ UTF-8, ISO, Latin เป็นตน้ , error คอื ขอ้ กาหนดเมอ่ื เกดิ ขอ้ ผดิ พลาดขน้ึ ขณะเขา้ รหสั เชน่ strict, ignore, replace สาหรบั strict เป็นคา่ default หมายถงึ ใหส้ นใจ ขอ้ ผดิ พลาดในการเขา้ รหสั คา่ ทส่ี ่งกลบั : ขอ้ ความทถ่ี ูกเขา้ รหสั แลว้ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print(\"Encoded String:\" + str.encode('base64','strict')) print(\"Decoded String:\" + tr.decode('base64','strict')) Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE= OUTPUT  str.endswith() คน้ หากล่มุ คา โดยเรมิ่ ตน้ จากทางดา้ นทา้ ยของสตรงิ รปู แบบคาสงั่ : str.endswith(suffix[, start[, end=len(string)]]) หน้า 584

พารามเิ ตอร:์ suffix คอื กล่มุ คาทต่ี อ้ งการคน้ หา, start คอื ตาแหน่งเรมิ่ ตน้ , end คอื ขนาดความยาวของสตรงิ ค่าทส่ี ง่ กลบั : ถา้ คน้ หากลุ่มคาพบจะสง่ ค่ากลบั เป็นจรงิ (True) ตวั อยา่ ง: str = \"this is string example....wow!!!\"; suffix = \"wow!!!\"; print (str.endswith(suffix)); print (str.endswith(suffix,20)); suffix = \"is\"; print (str.endswith(suffix, 2, 4)); print (str.endswith(suffix, 2, 6)); OUTPUT True True True False Note: ควรจาไวว้ า่ สญั ลกั ษณ์ [, start[, end]] หมายถงึ คา่ start และ end จะใสห่ รอื ไมใ่ สก่ ไ็ ด้  str.expandtabs() แทนทแ่ี ทบ็ (\\t) ในสตรงิ ดว้ ยชอ่ งวา่ ง (space) โดยคา่ default ของ ช่องว่างทแ่ี ทนทค่ี อื 1 tab ต่อ 1 space (เมอ่ื ไมก่ าหนด tabsize) แต่ถา้ ตอ้ งการใหช้ ่องว่าง กวา้ งขน้ึ ใหก้ าหนด tabsize ในอตั ราสว่ น 1 tab ต่อ 8 spaces โดยค่าตวั เลขทป่ี ้อนใหค้ อื 8, 16, 24, 32 เป็นตน้ รปู แบบคาสงั่ : str.expandtabs(tabsize) พารามเิ ตอร:์ ความกวา้ งของ tabsize ค่าทส่ี ง่ กลบั : ขอ้ ความทแ่ี ทนท่ี \\t ดว้ ยชอ่ งวา่ งตามขนาดของ tabsize ตวั อยา่ ง: str = \"this is\\tstring example....wow!!!\"; print(\"Original string:\" + str) print(\"Defualt exapanded tab:\" + str.expandtabs()) print(\"Double exapanded tab:\" + str.expandtabs(16)) Original string: this is string example....wow!!! Defualt exapanded tab: this is string example....wow!!! OUTPUT Double exapanded tab: this is string example....wow!!!  str.find() คน้ หากลมุ่ คาในสตรงิ รปู แบบคาสงั่ : str.find(str, beg=0 end=len(string)) พารามเิ ตอร:์ str คอื กล่มุ คาทต่ี อ้ งการคน้ หา, beg คอื ตาแหน่งเรมิ่ ตน้ (default = 0), end คอื ขนาดความยาวของสตรงิ (ถา้ ไมก่ าหนดถอื วา่ คน้ หาหมด) หน้า 585

ค่าทส่ี ง่ กลบั : ถา้ คน้ หากลุ่มคาพบจะส่งตาแหน่งเรมิ่ ตน้ ของกลุม่ คากลบั คนื มา ถา้ ไม่ พบผลลพั ธค์ อื -1 ตวั อยา่ ง: str1 = \"this is string example....wow!!!\"; str2 = \"exam\"; print (str1.find(str2)); print (str1.find(str2, 10)); print (str1.find(str2, 40)); print (str1.find(str2,0,len(str1))); )) 15 15 OUTPUT -1 15  str.rfind() การทางานเหมอื น find() แต่ผลลพั ธท์ ส่ี ่งกลบั มาคอื ตาแหน่งของกล่มุ คาท่ี ตอ้ งการคน้ หาลาดบั สดุ ทา้ ย รปู แบบคาสงั่ : str.rfind(str, beg=0,end=len(string)) พารามเิ ตอร:์ str คอื กลมุ่ คาทต่ี อ้ งการคน้ หา, beg คอื ตาแหน่งเรมิ่ ตน้ (default = 0), end คอื ขนาดความยาวของสตรงิ (ถา้ ไมก่ าหนดถอื ว่าคน้ หาหมด) ค่าทส่ี ง่ กลบั : ถา้ คน้ หากลุ่มคาพบจะสง่ ตาแหน่งเรมิ่ ตน้ ของกลุ่มคาในลาดบั สุดทา้ ย กลบั คนื มา ถา้ ไมพ่ บผลลพั ธค์ อื -1 ตวั อยา่ ง: str1 = \"this is string example....wow!!!\"; str2 = \"exam\"; print (str1.find(str2)); print (str1.find(str2, 10)); print (str1.find(str2, 40)); print (str1.find(str2,0,len(str1))); 24 5 OUTPUT 24 -1  str.index() ทางานเหมอื น find() ทกุ ประการ ต่างกนั คอื ถา้ คน้ หากลุม่ คาไมเ่ จอจะส่งค่า กลบั เป็น exception ออกมาแทน รปู แบบคาสงั่ : str.index(str, beg=0, end=len(string)) พารามเิ ตอร:์ str คอื กลมุ่ คาทต่ี อ้ งการคน้ หา, beg คอื ตาแหน่งเรมิ่ ตน้ (default = 0), end คอื ขนาดความยาวของสตรงิ (ถา้ ไมก่ าหนดถอื ว่าคน้ หาหมด) คา่ ทส่ี ่งกลบั : ถา้ คน้ หากลุ่มคาพบจะส่งตาแหน่งเรมิ่ ตน้ ของกลุม่ คากลบั คนื มา ถา้ คน้ หากลุ่มคาไมเ่ จอจะส่งค่ากลบั เป็น exception หน้า 586

ตวั อยา่ ง: str1 = \"this is string example....wow!!!\"; str2 = \"exam\"; print (str1.index(str2)); print (str1.index(str2, 10)); print (str1.index(str2, 40)); 15 15 OUTPUT Traceback (most recent call last): File \"C:/Python33/testx.py\", line 5, in <module> print (str1.index(str2, 40)); ValueError: substring not found  str.rindex() การทางานเหมอื น index() แต่เรมิ่ ทาการคน้ หามาจากดา้ นทา้ ยสตรงิ แทน รปู แบบคาสงั่ : str.rindex(str, beg=0, end=len(string)) พารามเิ ตอร:์ str คอื กลุม่ คาทต่ี อ้ งการคน้ หา, beg คอื ตาแหน่งเรม่ิ ตน้ (default = 0), end คอื ขนาดความยาวของสตรงิ (ถา้ ไมก่ าหนดถอื ว่าคน้ หาหมด) ค่าทส่ี ง่ กลบั : ถา้ คน้ หากลุ่มคาพบจะส่งตาแหน่งเรม่ิ ตน้ ของกลุม่ คาทค่ี น้ หาจากดา้ น ทา้ ยของสตรงิ กลบั คนื มา ถา้ คน้ หากล่มุ คาไมเ่ จอจะส่งค่ากลบั เป็น exception ตวั อยา่ ง: str1 = \"this is string example....wow!!!\"; str2 = \"is\"; print(str1.rindex(str2)); # This method of rindex() print(str1.index(str2)); # This method of index() 5 2 OUTPUT  str.isalnum(), str.isalpha() ตรวจสอบวา่ สตรงิ เป็น Alphanumeric (สญั ลกั ษณ์ต่างๆ ท่ี พมิ พจ์ ากแป้นพมิ พ์ ทงั้ ตวั อกั ษร และตวั เลข โดยไมร่ วมช่องว่าง และสญั ลกั ษณ์พเิ ศษ) หรอื ไม่ รปู แบบคาสงั่ : str.isalnum(), str.isalpha() พารามเิ ตอร:์ str คอื กลมุ่ คาทต่ี อ้ งการตรวจสอบ ค่าทส่ี ง่ กลบั : ถา้ เจออกั ษรทไ่ี มใ่ ช่ Alphanumeric เพยี งตวั เดยี ว จะส่งคา่ กลบั เป็น เทจ็ ตวั อยา่ ง: str = \"this2009\"; # No space in this string print (str.isalnum()); str = \"this is string example....wow!!!\"; print (str.isalnum()); หน้า 587

True False OUTPUT  str.isdigit() ตรวจสอบตวั เลขในสตรงิ รปู แบบคาสงั่ : str.isdigit() พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการตรวจสอบ คา่ ทส่ี ่งกลบั : เป็นจรงิ เมอ่ื ขอ้ มลู ในสตรงิ ทุกตวั เป็นตวั เลขทงั้ หมด ตวั อยา่ ง: str = \"123456\"; # Only digit in this string print (str.isdigit()); str = \"this is string example....555 !!!\"; print (str.isdigit()); True False OUTPUT  str.islower() ตรวจสอบวา่ ตวั อกั ษรในสตรงิ เป็นตวั อกั ษรตวั เลก็ หรอื ไม่ รปู แบบคาสงั่ : str.islower() พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการตรวจสอบ คา่ ทส่ี ่งกลบั : เป็นจรงิ เมอ่ื ขอ้ มลู ในสตรงิ ทุกตวั เป็นอกั ษรตวั เลก็ ทงั้ หมด ตวั อยา่ ง: str = \"THIS is string example....wow!!!\"; print (str.islower()); str = \"this is string example....wow!!!\"; print (str.islower()); False True OUTPUT  str.isnumeric() ตรวจสอบตวั เลขในสตรงิ ชนิด Unicode รปู แบบคาสงั่ : str.isnumeric() พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการตรวจสอบ คา่ ทส่ี ง่ กลบั : เป็นจรงิ เมอ่ื ขอ้ มลู ในสตรงิ ชนิด Unicode ทุกตวั เป็นตวั เลขทงั้ หมด ตวั อยา่ ง: str = u\"this2009\"; # u means Unicode print (str.isnumeric()); str = u\"23443434\"; print (str.isnumeric()); False True OUTPUT หน้า 588

 str.isspace() ตรวจสอบสตรงิ วา่ ง รปู แบบคาสงั่ : str.isspace() พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการตรวจสอบ ค่าทส่ี ่งกลบั : เป็นจรงิ เมอ่ื ขอ้ มลู ในสตรงิ เป็นคา่ วา่ ง ตวั อยา่ ง: str = \" \"; print (str.isspace()); str = \"This is string example....wow!!!\"; print (str.isspace()); True False OUTPUT  str.istitle() ตรวจสอบอกั ษรตวั แรกของขอ้ ความว่าเป็นตวั ใหญ่หรอื ไม่ รปู แบบคาสงั่ : str.istitle() พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการตรวจสอบ ค่าทส่ี ่งกลบั : เป็นจรงิ เมอ่ื คาขน้ึ ต้นในทกุ ๆ กลุ่มคาเป็นตวั ใหญ่ทงั้ หมด ตวั อยา่ ง: str = \"This Is String Example...Wow!!!\"; print (str.istitle()); str = \"This is string example....wow!!!\"; print (str.istitle()); True False OUTPUT  str.isupper() ตรวจสอบวา่ ตวั อกั ษรในสตรงิ เป็นตวั ใหญ่ทงั้ หมด รปู แบบคาสงั่ : str.isupper() พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการตรวจสอบ ค่าทส่ี ง่ กลบั : เป็นจรงิ เมอ่ื ขอ้ ความในสตรงิ เป็นอกั ษรตวั ใหญ่ทงั้ หมด ตวั อยา่ ง: str = \"THIS IS STRING EXAMPLE....WOW!!!\"; print (str.isupper()); str = \"THIS is string example....wow!!!\"; print (str.isupper()); True False OUTPUT  str.lstrip() ลบช่องวา่ งหน้าสตรงิ ทงั้ หมดออก ถา้ ตอ้ งการลบตวั อกั ษรอ่นื ๆ ทไ่ี มใ่ ช่ช่องวา่ ง หน้าสตรงิ ออก ใหก้ าหนดตวั อกั ษรทต่ี อ้ งการลบกบั ตวั แปร chars หน้า 589

รปู แบบคาสงั่ : str.lstrip([chars]) พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการลบตวั อกั ษร, chars คอื ตวั อกั ษรทต่ี อ้ งการลบทง้ิ ค่าทส่ี ง่ กลบั : สตรงิ ทล่ี บชอ่ งว่าง หรอื อกั ษรทต่ี อ้ งการออกแลว้ ตวั อยา่ ง: str = \" this is string example....wow!!! \"; print (str.lstrip()); str = \"55555this is string example....wow!!!55555\"; print (str.lstrip('5')); this is string example....wow!!! this is string example....wow!!!55555 OUTPUT  str.rstrip() ทางานเหมอื น lstrip() แตกต่างกนั ตรงท่ี rstrip() จะแทนทต่ี วั อกั ษรมาจากดา้ น ทา้ ยของสตรงิ แทน รปู แบบคาสงั่ : str.rstrip([chars]) พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการลบตวั อกั ษร, chars คอื ตวั อกั ษรทต่ี อ้ งการลบทง้ิ ค่าทส่ี ง่ กลบั : สตรงิ ทล่ี บชอ่ งวา่ ง หรอื อกั ษรทต่ี อ้ งการออกแลว้ ตวั อยา่ ง: str = \" this is string example....wow!!! \"; print (str.rstrip()); str = \"55555this is string example....wow!!!55555\"; print (str.rstrip('5')); this is string example....wow!!! 55555this is string example....wow!!! OUTPUT  str.join() เช่อื มกลุม่ ของสตรงิ เขา้ ดว้ ยกนั โดยคนั่ ดว้ ยเครอ่ื งหมายทผ่ี ใู้ ชก้ าหนดเอง รปู แบบคาสงั่ : str.join(seq) พารามเิ ตอร:์ str คอื สตรงิ ชดุ ท่ี 1 ใชส้ าหรบั คนั่ ระหว่างสตรงิ , seq สตรงิ ชุดท่ี 2 ค่าทส่ี ่งกลบั : สตรงิ ทเ่ี กดิ จากการเชอ่ื มต่อระหว่าง str และ seq ตวั อยา่ ง: str = \"/\"; # This is concatenation fo string seq = (\"12\", \"12\", \"2012\"); # Sequence of strings. print (str.join( seq )); 12/12/2012 OUTPUT  len() คานวณหาความยาวของสตรงิ รปู แบบคาสงั่ : len(string) พารามเิ ตอร:์ สตรงิ ทต่ี อ้ งการหาความยาว หน้า 590

คา่ ทส่ี ง่ กลบั : ความยาวของสตรงิ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (\"Length of the string: \", len(str)); Length of the string: 32 OUTPUT  str.ljust() เตมิ ตวั อกั ษรลงในดา้ นทา้ ยสตรงิ ใหค้ รบตามจานวนทก่ี าหนดไว้ รปู แบบคาสงั่ : str.ljust(width[, fillchar]) พารามเิ ตอร:์ width คอื ความยาวของสตรงิ ทต่ี อ้ งการ, fillchar คอื ตวั อกั ษรทต่ี อ้ งการ เตมิ ใหเ้ ตม็ ถา้ ไมก่ าหนด fillcahr ไว้ จะมคี ่าเป็นช่องวา่ งโดยอตั โนมตั ิ คา่ ทส่ี ่งกลบั : สตรงิ ทเ่ี ตมิ ตวั อกั ษรตามทต่ี อ้ งการแลว้ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (str.ljust(50, '@')); this is string example....wow!!!@@@@@@@@@@@@@@@@@@ OUTPUT  str.rjust() ทางานเหมอื น ljust() แต่ทาการเตมิ อกั ษรจากดา้ นหน้าของสตรงิ แทน รปู แบบคาสงั่ : str.rjust(width,[, fillchar]) พารามเิ ตอร:์ width คอื ความยาวของสตรงิ ทต่ี อ้ งการ, fillchar คอื ตวั อกั ษรทต่ี อ้ งกา เตมิ ใหเ้ ตม็ ถา้ ไมก่ าหนด fillcahr ไว้ จะมคี ่าเป็นชอ่ งว่างโดยอตั โนมตั ิ คา่ ทส่ี ่งกลบั : สตรงิ ทเ่ี ตมิ ตวั อกั ษรตามทต่ี อ้ งการแลว้ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (str.rjust(50, '@')); @@@@@@@@@@@@@@@@@@this is string example....wow!!! OUTPUT  str.lower() เปลย่ี นตวั อกั ษรในสตรงิ ใหเ้ ป็นอกั ษรตวั เลก็ ทงั้ หมด รปู แบบคาสงั่ : str.lower() พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการเปลย่ี นใหเ้ ป็นตวั เลก็ คา่ ทส่ี ง่ กลบั : สตรงิ ทเ่ี ป็นตวั อกั ษรตวั เลก็ ทงั้ หมดแลว้ ตวั อยา่ ง: str = \"THIS IS STRING EXAMPLE....WOW!!!\"; print (str.lower()); หน้า 591

this is string example....wow!!! OUTPUT  max() คน้ หาตวั อกั ษรทม่ี คี ่าสงู ทส่ี ุดจากการเรยี งลาดบั ตวั อกั ษรของแต่ละภาษา รปู แบบคาสงั่ : max(str) พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการคน้ หา ค่าทส่ี ่งกลบั : ตวั อกั ษรทม่ี คี า่ สงู ทส่ี ดุ ของแต่ละภาษา ตวั อยา่ ง: str = \"this is really a string example....wow!!!\"; print (\"Max character: \" + max(str)); str = \"this is a string example....wow!!!\"; print (\"Max character: \" + max(str)); str = \"กขคงจพศอฮ\" print (\"Max character of Thai is: \" + max(str)); Max character: y Max character: x OUTPUT Max character of Thai is: ฮ  min() คน้ หาตวั อกั ษรทม่ี คี า่ ต่าทส่ี ดุ จากการเรยี งลาดบั ตวั อกั ษรของแต่ละภาษา รปู แบบคาสงั่ : min(str) พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการคน้ หา คา่ ทส่ี ่งกลบั : ตวั อกั ษรทม่ี คี า่ ต่าทส่ี ดุ ของแต่ละภาษา ตวั อยา่ ง: str = \"this-is-real-string-example....wow!!!\"; print (\"Min character: \" + min(str)); str = \"this-is-a-string-example....wow\"; print (\"Min character: \" + min(str)); str = \"กขคงจพศอฮ\" print (\"Max character of Thai is: \" + min(str)) Min character: ! Min character: - OUTPUT Max character of Thai is: ก  str.replace() แทนทต่ี วั อกั ษร รปู แบบคาสงั่ : str.replace(old, new[, max]) พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการแทนท่,ี old ตวั อกั ษรเก่าทถ่ี กู แทนท,่ี new ตวั อกั ษรใหมท่ ต่ี อ้ งการแทนท่ี ถา้ ไมก่ าหนดค่า max จะทาการแทนท่ี ทงั้ สตรงิ คา่ ทส่ี ่งกลบั : สตรงิ ทถ่ี กู แทนทแ่ี ลว้ หน้า 592

ตวั อยา่ ง: str = \"this is string example....wow!!! this is really string\"; print (str.replace(\"is\", \"was\")); print (str.replace(\"is\", \"was\", 3)); thwas was string example....wow!!! thwas was really string thwas was string example....wow!!! thwas is really string OUTPUT  str.split() แยกกล่มุ คาออกจากสตรงิ โดย default จะทาการแยกโดยพจิ ารณาจากอกั ษร วา่ ง (space) ทค่ี นั่ ละหวา่ งกลุ่มคา (และตดั สญั ลกั ษณ์พเิ ศษออกดว้ ย เช่น \\n) และสามารถ แยกกล่มุ คาดว้ ยตวั อกั ษรอ่นื ๆ ทผ่ี ใู้ ชก้ าหนดได้ รปู แบบคาสงั่ : str.split(strs=\"\", num=string.count(str)) พารามเิ ตอร:์ str คอื สตรงิ ทต่ี อ้ งการแยกออก, strs ตวั อกั ษรทใ่ี ชแ้ ยกกลมุ่ คา, num ใชส้ าหรบั กาหนดจานวนกลมุ่ คายอ่ ยว่าตอ้ งการทงั้ หมดกช่ี ุด ถา้ ไมใ่ ส่ จะแยกทุกกลุ่มคาในสตรงิ คา่ ทส่ี ง่ กลบั : ขอ้ มลู ชนดิ ลสิ ตข์ องสตรงิ ทถ่ี ูกแยกแลว้ ตวั อยา่ ง: str = \"Line1-abcdef \\nLine2-abc \\nLine4-abcd\"; str1 = \"This + is + program + for + splitting + word\"; print (str.split()); print (str.split(' ', 1)); print (str.split('\\n')); print (str1.split(' + ')); ['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] ['Line1-abcdef', '\\nLine2-abc \\nLine4-abcd'] OUTPUT ['Line1-abcdef ', 'Line2-abc ', 'Line4-abcd'] ['This', 'is', 'program', 'for', 'splitting', 'word']  str.splitlines() ทาการแยกกลุม่ คาออกจากสตรงิ โดยพจิ ารณาจาก \\n ในกรณที ก่ี าหนดค่า num > 0 กล่มุ คาทถ่ี กู แยกออกมาจะมี \\n ต่อทา้ ย รปู แบบคาสงั่ : str.splitlines( num=string.count('\\n')) พารามเิ ตอร:์ num > 0 กลุ่มคาทถ่ี กู แยกออกมาจะมี \\n ต่อทา้ ย ค่าทส่ี ่งกลบั : ขอ้ มลู ชนดิ ลสิ ตข์ องสตรงิ ทถ่ี ูกแยกแลว้ ตวั อยา่ ง: str = \"Line1-a b c d e f\\nLine2- a b c\\n\\nLine4- a b c d\"; print (str.splitlines()); print (str.splitlines(0)); print (str.splitlines(3)); print (str.splitlines(4)); print (str.splitlines(5)); หน้า 593

['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] OUTPUT ['Line1-a b c d e f\\n', 'Line2- a b c\\n', '\\n', 'Line4- a b c d'] ['Line1-a b c d e f\\n', 'Line2- a b c\\n', '\\n', 'Line4- a b c d'] ['Line1-a b c d e f\\n', 'Line2- a b c\\n', '\\n', 'Line4- a b c d']  str.startswith() ตรวจสอบสตรงิ ว่ามขี อ้ ความทต่ี รงกบั ขอ้ ความทต่ี อ้ งการคน้ หาหรอื ไม่ รปู แบบคาสงั่ : str.startswith(str, beg=0,end=len(string)) พารามเิ ตอร:์ str เป็นขอ้ ความทต่ี อ้ งการคน้ หา, beg คอื ตาแหน่งเรมิ่ ตน้ ทต่ี อ้ งการ คน้ หา, end คอื ตาแหน่งสดุ ทา้ ยของสตรงิ ทต่ี อ้ งการคน้ หา คา่ ทส่ี ่งกลบั : เป็นจรงิ เมอ่ื คน้ หาพบ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (str.startswith( 'this' )); print (str.startswith( 'is', 2, 4 )); print (str.startswith( 'this', 2, 4 )); True True OUTPUT False  str.strip() ลบตวั ตวั อกั ษรทต่ี อ้ งการทง้ิ ทงั้ จากดา้ นหน้าและดา้ นหลงั สตรงิ โดยทางาน เหมอื น lstrip() และ rstrip() รว่ มกนั รปู แบบคาสงั่ : str.strip([chars]) พารามเิ ตอร:์ str เป็นขอ้ ความทต่ี อ้ งการลบตวั อกั ษร, chars ตวั อกั ษรทต่ี อ้ งการ ลบทง้ิ ถา้ ไมก่ าหนดตวั อกั ษรทจ่ี ะลบ คา่ default คอื ชอ่ งว่าง คา่ ทส่ี ่งกลบั : สตรงิ ทถ่ี กู ลบตวั อกั ษรทต่ี อ้ งการออกแลว้ ตวั อยา่ ง: str = \"0000000this is string example....wow!!!0000000\"; print (str.strip( '0' )); #Remove 0 both sides this is string example....wow!!! OUTPUT  str.swapcase() สลบั ตวั อกั ษรในสตรงิ เชน่ ถา้ เดมิ เป็นตวั เลก็ จะสลบั เป็นตวั ใหญ่ แต่ถา้ เดมิ เป็นตวั ใหญ่จะสลบั เป็นอกั ษรตวั เลก็ แทน รปู แบบคาสงั่ : str.swapcase() พารามเิ ตอร:์ str เป็นขอ้ ความทต่ี อ้ งการสลบั ท่ี คา่ ทส่ี ง่ กลบั : สตรงิ ทถ่ี ูกสลบั ทต่ี วั อกั ษรแลว้ ตวั อยา่ ง: str = \"This is string example....Wow!!!\"; print (str.swapcase()); หน้า 594

str = \"THIS Is STRING EXAMPLE....WOw!!!\"; print (str.swapcase()); tHIS IS STRING EXAMPLE....wOW!!! this iS string example....woW!!! OUTPUT  str.title() แทนทต่ี วั อกั ษรตวั แรกของกลุ่มคาในสตรงิ เป็นตวั อกั ษรตวั ใหญ่ทงั้ หมด รปู แบบคาสงั่ : str.title() พารามเิ ตอร:์ str เป็นขอ้ ความทต่ี อ้ งการเปลย่ี นตวั อกั ษรตวั แรกใหเ้ ป็นตวั ใหญ่ ค่าทส่ี ง่ กลบั : สตรงิ ทถ่ี กู เปลย่ี นใหอ้ กั ษรตวั แรกเป็นตวั ใหญ่แลว้ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (str.title()); This Is String Example....Wow!!! OUTPUT  str.upper() แปลงอกั ษรในสตรงิ ใหเ้ ป็นตวั ใหญ่ทงั้ หมด รปู แบบคาสงั่ : str.upper() พารามเิ ตอร:์ str เป็นขอ้ ความทต่ี อ้ งการเปลย่ี นเป็นอกั ษรตวั ใหญ่ คา่ ทส่ี ่งกลบั : สตรงิ ทถ่ี กู เปลย่ี นใหเ้ ป็นอกั ษรตวั ใหญ่แลว้ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (\"str.capitalize() : \", str.capitalize()) str.upper() : THIS IS STRING EXAMPLE....WOW!!! OUTPUT  str.zfill() เตมิ อกั ษรเลข 0 ตามจานวนทก่ี าหนด ตรงส่วนหน้าของสตรงิ รปู แบบคาสงั่ : str.zfill(width) พารามเิ ตอร:์ str เป็นขอ้ ความทต่ี อ้ งการเตมิ เลข 0, width คอื ความกวา้ งของสตรงิ ค่าทส่ี ่งกลบั : สตรงิ ทถ่ี กู เตมิ เลข 0 แลว้ ตวั อยา่ ง: str = \"this is string example....wow!!!\"; print (\"Original string length = \",len(str)); #len = 32 chars print (str.zfill(40)); # fill 0 from 32 to 40 chars print (str.zfill(50)); # fill 0 from 32 to 50 chars Original string length = 32 00000000this is string example....wow!!! OUTPUT 000000000000000000this is string example....wow!!! หน้า 595

 str.isdecimal() ตรวจสอบว่าตวั อกั ษรในสตรงิ เป็นเลขฐานสบิ รปู แบบคาสงั่ : str.isdecimal() พารามเิ ตอร:์ str เป็นขอ้ ความทต่ี อ้ งการตรวจสอบ ค่าทส่ี ง่ กลบั : เป็นจรงิ เมอ่ื ตวั อกั ษรในสตรงิ เป็นเลขฐานสบิ ทงั้ หมด ตวั อยา่ ง: str = u\"this2009\"; # u means Unicode print (str.isdecimal()); str = u\"23443434\"; print (str.isdecimal()); False True OUTPUT  chr() ฟังชนั แปลงจานวนเตม็ ใดๆ เป็นตวั อกั ษร 1 ตวั รปู แบบคาสงั่ : chr(i) พารามเิ ตอร:์ i คอื จานวนเตม็ ใดๆ มคี า่ อยรู่ ะหวา่ ง 0 - 256 ค่าทส่ี ง่ กลบั : สตรงิ ทเ่ี ป็นตวั อกั ษรเพยี ง 1 ตวั ตวั อยา่ ง: a = 97 # integer of ‘a’ in ASCII code b = 60 # integer of < in ASCII print (chr(a)); print (chr(b)); a < OUTPUT  len() ฟังชนั หาขนาดความยาวของอ๊อปเจก็ ตใ์ ดๆ รปู แบบคาสงั่ : len(object) พารามเิ ตอร:์ object คอื อ๊อปเจก็ ตใ์ ดๆ ทต่ี อ้ งการหาความยาว ค่าทส่ี ่งกลบั : ความยาวทเ่ี ป็นเลขจานวนเตม็ ตวั อยา่ ง: strs = 'Hello Python!' lst = [1, 2, 3, 4] tupl = (5, 6) dicts = {1:'name',2:'Surname'} print(\"len of string = \",len(strs)) print(\"len of list = \",len(lst)); print(\"len of tuple = \",len(tupl)); print(\"len of dictionary = \",len(dicts)); หน้า 596

len of string = 13 len of list = 4 OUTPUT len of tuple = 2 len of dictionary = 2  ord() ฟังชนั แปลงตวั อกั ษรใดๆ ใหเ้ ป็นตวั เลขจานวนเตม็ รปู แบบคาสงั่ : ord(c) พารามเิ ตอร:์ c คอื ตวั อกั ษรเพยี ง 1 ตวั เท่านัน้ ค่าทส่ี ่งกลบั : เลขจานวนเตม็ ในรหสั ASCII ตวั อยา่ ง: print(\"Integer (ASCII Table) of a is \",ord('a')) print(\"Integer (ASCII Table) of > is \",ord('>')) print(\"Integer (ASCII Table) of # is \",ord('#')) Integer (ASCII Table) of a is 97 Integer (ASCII Table) of > is 62 OUTPUT Integer (ASCII Table) of # is 35  repr() ฟังชนั แปลงอ๊อปเจก็ ตใ์ ดๆ เป็นสตรงิ รปู แบบคาสงั่ : repr(object) พารามเิ ตอร:์ object คอื อ๊อปเจก็ ตใ์ ดๆ ทต่ี อ้ งการแปลงเป็นสตรงิ ค่าทส่ี ่งกลบั : สตรงิ ตวั อยา่ ง: print(\"Convert Integer to String :\",repr(1234)) print(\"Convert Float to String :\",repr(12.34)) print(\"Convert List to String :\",repr([1, 2, 3])) print(\"Convert Tuple to String :\",repr((4, 5))) print(\"Convert Dictionary to String :\",repr({1:'Python'})) print(\"Convert Hex to String :\",repr(0x345)) Convert Integer to String : 1234 Convert Float to String : 12.34 OUTPUT Convert List to String : [1, 2, 3] Convert Tuple to String : (4, 5) Convert Dictionary to String : {1: 'Python'} Convert Hex to String : 837  str() ฟังชนั แปลงอ๊อปเจก็ ตใ์ ดๆ เป็นสตรงิ เหมอื น repr() รปู แบบคาสงั่ : str(object) พารามเิ ตอร:์ object คอื อ๊อปเจก็ ตใ์ ดๆ ทต่ี อ้ งการแปลงเป็นสตรงิ ค่าทส่ี ง่ กลบั : สตรงิ ตวั อยา่ ง: เหมอื นกบั ฟังชนั repr() ทกุ ประการ 2. ฟังชนั ภายในสาหรบั ข้อมูลเชิงประกอบ (Built-in functions for composite data types) หน้า 597

ฟังชนั และเมธอดสาหรบั ลิสต์ (Built-in list functions & methods) ฟังชนั สาหรบั ลิสต์  len() ฟังชนั หาจานวนของสมาชกิ หรอื ความยาวในลสิ ต์ รปู แบบคาสงั่ : len(list) พารามเิ ตอร:์ list คอื ตวั แปรลสิ ตท์ ต่ี อ้ งการหาจานวนสมาชกิ คา่ ทส่ี ง่ กลบั : จานวนของสมาชกิ ในลสิ ตเ์ ป็นเลขจานวนเตม็ ตวั อยา่ ง: list1, list2 = [123, 'xyz', 'zara'], [456, 'abc'] print (\"First list length : \", len(list1)); print (\"Second list length : \", len(list2)); First list length : 3 Second list length : 2 OUTPUT  max() ฟังชนั คน้ หาค่าขอ้ มลู ทม่ี คี ่าสงู ทส่ี ดุ ในลสิ ต์ ในกรณที เ่ี ป็นตวั อกั ษรจะพจิ ารณาคา่ ตวั เลขประจาตวั อกั ษรนนั้ ๆ เช่น a = 97, b = 98 ในรหสั ASCII เป็นตน้ รปู แบบคาสงั่ : max(list) พารามเิ ตอร:์ list คอื ตวั แปรชนิดลสิ ต์ ค่าทส่ี ่งกลบั : สมาชกิ ในลสิ ตท์ ม่ี คี ่ามากทส่ี ุด ตวั อยา่ ง: list1, list2 = ['123','xyz', 'zara', 'abc'], [456, 700, 200] print (\"Max value element : \", max(list1)); print (\"Max value element : \", max(list2)); Max value element : zara Max value element : 700 OUTPUT  min() ฟังชนั คน้ หาคา่ ขอ้ มลู ทม่ี คี ่าต่าทส่ี ุดในลสิ ต์ รปู แบบคาสงั่ : min(list) พารามเิ ตอร:์ list คอื ตวั แปรชนิดลสิ ต์ ค่าทส่ี ่งกลบั : สมาชกิ ในลสิ ตท์ ม่ี คี ่าน้อยทส่ี ุด ตวั อยา่ ง: list1, list2 = ['123','xyz', 'zara', 'abc'], [456, 700, 200] print (\"Min value element : \", min(list1)); print (\"Min value element : \", min(list2)); Min value element : 123 Min value element : 200 OUTPUT หน้า 598

 list() ฟังชนั แปลงขอ้ มลู ชนิดทพั เพลิ (tuple) เป็นลสิ ต์ (list) รปู แบบคาสงั่ : list(seq) พารามเิ ตอร:์ seq คอื ขอ้ มลู ของตวั แปรชนิดทพั เพลิ คา่ ทส่ี ง่ กลบั : ขอ้ มลู ชนดิ ลสิ ต์ ตวั อยา่ ง: aTuple = (123, 'xyz', 'zara', 'abc'); aList = list(aTuple) print (\"List elements : \", aList) List elements : [123, 'xyz', 'zara', 'abc'] OUTPUT  any() ฟังชนั ตรวจสอบหาวา่ ลสิ ตว์ ่างหรอื ไม่ รปู แบบคาสงั่ : any(seq) พารามเิ ตอร:์ seq คอื ขอ้ มลู ของตวั แปรชนิดลสิ ต์ คา่ ทส่ี ่งกลบั : เป็นจรงิ เมอ่ื มขี อ้ มลู สมาชกิ อยา่ งน้อย 1 คา่ หรอื ไมใ่ ชล่ สิ ตว์ า่ ง ตวั อยา่ ง: aList = [] print(\"Testing empty list :\",any(aList)) aList = [1, 2, 3] print(\"Testing empty list :\",any(aList)) Testing empty list : False Testing empty list : True OUTPUT  all() ฟังชนั ตรวจสอบหาคา่ ทเ่ี ป็นเทจ็ ของสมาชกิ รปู แบบคาสงั่ : all(seq) พารามเิ ตอร:์ seq คอื ขอ้ มลู ของตวั แปรชนิดลสิ ต์ ค่าทส่ี ง่ กลบั : เป็นจรงิ เมอ่ื ขอ้ มลู สมาชกิ ในลสิ ตท์ งั้ หมดไมม่ คี ่าใดเป็นค่าเทจ็ ตวั อยา่ ง: aList = [4, 2, 5] print(\"Testing false member:\",all(aList)) aList = [4, 2, 0] print(\"Testing false member:\",all(aList)) aList = [4, 2, False] print(\"Testing false member:\",all(aList)) Testing false member: True Testing false member: False OUTPUT Testing false member: False  range() ฟังชนั สรา้ งลาดบั ขอ้ มลู สมาชกิ ของลสิ ต์ หน้า 599

รปู แบบคาสงั่ : range([start], stop, [step]) พารามเิ ตอร:์ start คอื ค่าเรม่ิ ตน้ ของขอ้ มลู , stop คอื ค่าสน้ิ สดุ ขอ้ มลู , step คอื คา่ ทใ่ี ช้ ระบถุ งึ การสรา้ งขอ้ มลู ในลกั ษณะเป็นชว่ งๆ ถา้ ไมไ่ ดร้ ะบุค่าใน step ขอ้ มลู จะเรยี งในลกั ษณะจานวนนบั คอื 1, 2, 3,…n และถา้ ไม่ กาหนดค่าใน start โปรแกรมจะถอื ว่าเรม่ิ จาก 0 เสมอ คา่ ทส่ี ่งกลบั : ขอ้ มลู สมาชกิ ของตวั แปรลสิ ต์ ตวั อยา่ ง: lst1 = list(range(10)) lst2 = list(range(10, 20)) lst3 = list(range(0, 20, 2)) print(\"lst1 :\",lst1) print(\"lst2 :\",lst2) print(\"lst3 :\",lst3) lst1 : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] lst2 : [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] OUTPUT lst3 : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] เมธอดสาหรบั ลิสต์  list.append() เพมิ่ ขอ้ มลู สมาชกิ โดยการต่อทา้ ยลสิ ต์ รปู แบบคาสงั่ : list.append(obj) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , obj คอื อ๊อปเจก็ ตท์ ต่ี อ้ งการเพมิ่ ต่อทา้ ยลสิ ต์ คา่ ทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: aList = [123, 'xyz', 'zara', 'abc']; print (\"Before update list : \", aList); aList.append( 2009 ); print (\"After update list : \", aList); Before update list : [123, 'xyz', 'zara', 'abc'] After update list : [123, 'xyz', 'zara', 'abc', 2009] OUTPUT  list.count() นบั จานวนสมาชกิ ทต่ี อ้ งการในลสิ ต์ รปู แบบคาสงั่ : list.count(obj) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , obj คอื อ๊อปเจก็ ตท์ ต่ี อ้ งการนบั ค่าทส่ี ่งกลบั : จานวนอ๊อปเจก็ ตท์ น่ี บั ได้ มคี ่าเป็นจานวนเตม็ บวก ตวั อยา่ ง: aList = [123, 'xyz', 'zara', 'abc', 123]; print (\"Count for 123 : \", aList.count(123)); หน้า 600

print (\"Count for zara : \", aList.count('zara')); Count for 123 : 2 Count for zara : 1 OUTPUT  list.extend() เชอ่ื มสมาชกิ ของ 2 ลสิ ตใ์ ดๆ เขา้ ดว้ ยกนั โดยเช่อื มต่อจากดา้ นหลงั รปู แบบคาสงั่ : list.extend(seq) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , seq คอื ลสิ ตใ์ หมท่ ต่ี อ้ งการเชอ่ื มต่อ ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: aList = [123, 'xyz', 'zara', 'abc', 123]; bList = [2009, 'manni']; aList.extend(bList) print (\"Extended List:\", aList); Extended List:[123, 'xyz', 'zara', 'abc', 123, 2009, 'manni'] OUTPUT  list.index() คน้ หาขอ้ มลู ในลสิ ต์ รปู แบบคาสงั่ : list.index(obj) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , obj คอื ขอ้ มลู ทต่ี อ้ งการคน้ หา ค่าทส่ี ่งกลบั : ตาแหน่งของขอ้ มลู สมาชกิ ทค่ี น้ หาเจอในอนั ดบั ตน้ ของลสิ ต์ ถา้ คน้ หา ไมพ่ บจะเกดิ exception error ตวั อยา่ ง: aList = [125, 'xyz', 'zara', 'abc', 123]; print (\"Index for 123 : \", aList.index( 123 )); print (\"Index for zara : \", aList.index( 'zara' )); Index for 123 : 4 Index for zara : 2 OUTPUT  list.insert() เพม่ิ ขอ้ มลู สมาชกิ ใหมเ่ ขา้ ไปในลสิ ต์ รปู แบบคาสงั่ : list.insert(index, obj) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , index คอื ตาแหน่งทต่ี อ้ งการเพม่ิ สมาชกิ , obj คอื ขอ้ มลู สมาชกิ ใหมท่ ต่ี อ้ งการเพมิ่ ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: aList = [123, 'xyz', 'zara', 'abc'] aList.insert(3, 2009) print (\"Insert 2009 to list : \", aList) aList.insert(-1,123) หน้า 601

print (\"Insert 123 before end list : \", aList) Insert 2009 to list : [123, 'xyz', 'zara', 2009, 'abc'] Insert 123 before end list:[123,'xyz','zara',2009,123,'abc'] OUTPUT  list.pop() ลบขอ้ มลู สมาชกิ ออกจากลสิ ต์ โดย default จะลบสมาชกิ ทอ่ี ยใู่ นตาแหน่งทา้ ยสดุ ของลสิ ตอ์ อกก่อน แต่ถา้ ตอ้ งการลบขอ้ มลู สมาชกิ ตาแหน่งใดๆ ในลสิ ตอ์ อก ใหก้ าหนดชอ่ื สมาชกิ ทต่ี อ้ งการลบใน obj รปู แบบคาสงั่ : list.pop(obj=list[-1]) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , obj คอื ขอ้ มลู สมาชกิ ทต่ี อ้ งการลบออก ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: aList = [123, 'xyz', 'zara', 'abc']; print (\"Before pop list : \", aList); print (\"1'st pop (last list): \", aList.pop()); print (\"After 1'st pop list : \", aList); print (\"2'nd pop [index 1]: \", aList.pop(1)); print (\"After 2'nd pop list : \", aList); Before pop list : [123, 'xyz', 'zara', 'abc'] 1'st pop (last list): abc OUTPUT After 1'st pop list : [123, 'xyz', 'zara'] 2'nd pop [index 1]: xyz After 2'nd pop list : [123, 'zara']  list.remove() ลบขอ้ มลู สมาชกิ ออกจากลสิ ต์ โดยระบุช่อื ของสมาชกิ ทต่ี อ้ งการลบ รปู แบบคาสงั่ : list.remove(obj) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , obj คอื ชอ่ื สมาชกิ ทต่ี อ้ งการลบออก ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: List = [123, 'xyz', 'zara', 'abc', 'xyz']; print (\"Before remove list : \", aList); aList.remove('xyz'); print (\"Remove xyz member : \", aList); aList.remove('abc'); print (\"Remove abc member : \", aList); Before remove list : [123, 'xyz', 'zara', 'abc', 'xyz'] Remove xyz member : [123, 'zara', 'abc', 'xyz'] OUTPUT Remove abc member : [123, 'zara', 'xyz']  list.reverse() จดั เรยี งขอ้ มลู ของสมาชกิ ภายในลสิ ต์ แบบยอ้ นกลบั หรอื สลบั ทศิ ทางกนั รปู แบบคาสงั่ : list.reverse() พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั หน้า 602

ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: aList = [1, 2, 3, 4, 5, 6, 7]; print (\"Before reverse list : \", aList) aList.reverse(); print (\"After reverse list : \", aList) Before reverse list : [1, 2, 3, 4, 5, 6, 7] After reverse list : [7, 6, 5, 4, 3, 2, 1] OUTPUT  list.sort() จดั เรยี งขอ้ มลู ของสมาชกิ ภายในลสิ ต์ โดยพจิ ารณาจากคา่ ประจาตาแหน่งของ ตวั อกั ษรแต่ละตวั รปู แบบคาสงั่ : list.sort([func]) พารามเิ ตอร:์ list คอื ลสิ ตต์ น้ ฉบบั , func คอื วธิ ที ใ่ี ชจ้ ดั เรยี งขอ้ มลู จะมหี รอื ไมม่ กี ไ็ ด้ ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: aList = ['123', 'xyz', 'zara', 'abc', 'xyz']; bList = [1, 5, 7, 4, 5, 6, 3] aList.sort(); bList.sort(); print (\"Sorting aList : \", aList); print (\"Sorting bList : \", bList); Sorting aList : ['123', 'abc', 'xyz', 'xyz', 'zara'] Sorting bList : [1, 3, 4, 5, 5, 6, 7] OUTPUT ฟังชนั ภายในสาหรบั ทพั เพิล (Built-in tuple functions)  len() ฟังชนั นบั จานวนสมาชกิ ในทพั เพลิ รปู แบบคาสงั่ : len(tuple) พารามเิ ตอร:์ tuple คอื ทพั เพลิ ตน้ ฉบบั ค่าทส่ี ง่ กลบั : จานวนของสมาชกิ เป็นจานวนเตม็ บวก ตวั อยา่ ง: tuple1, tuple2 = (123, 'xyz', 'zara'), (456, 'abc') print (\"First tuple length : \", len(tuple1)); print (\"Second tuple length : \", len(tuple2)); First tuple length : 3 Second tuple length : 2 OUTPUT  max() ฟังชนั คน้ หาสมาชกิ ทม่ี คี ่ามากทส่ี ดุ ในทพั เพลิ หน้า 603

รปู แบบคาสงั่ : max(tuple) พารามเิ ตอร:์ tuple คอื ทพั เพลิ ตน้ ฉบบั คา่ ทส่ี ง่ กลบั : สมาชกิ ทม่ี คี ่ามากทส่ี ุดในทพั เพลิ ตวั อยา่ ง: tuple1, tuple2 = ('123','xyz','zara','abc'),(456, 700, 200) print (\"Max value element : \", max(tuple1)); print (\"Max value element : \", max(tuple2)); Max value element : zara Max value element : 700 OUTPUT  min() คน้ หาสมาชกิ ทม่ี คี า่ น้อยทส่ี ดุ ในทพั เพลิ รปู แบบคาสงั่ : min(tuple) พารามเิ ตอร:์ tuple คอื ทพั เพลิ ตน้ ฉบบั ค่าทส่ี ่งกลบั : สมาชกิ ทม่ี คี ่าน้อยทส่ี ุดในทพั เพลิ ตวั อยา่ ง: tuple1, tuple2 = ('123','xyz','zara','abc'),(456, 700, 200) print (\"Min value element : \", min(tuple1)); print (\"Min value element : \", min(tuple2)); Min value element : 123 Min value element : 200 OUTPUT  tuple() แปลงขอ้ มลู ชนดิ ลสิ ตไ์ ปเป็นทพั เพลิ รปู แบบคาสงั่ : tuple(seq) พารามเิ ตอร:์ seq คอื ขอ้ มลู ของตวั แปรชนิดลสิ ต์ ค่าทส่ี ่งกลบั : ขอ้ มลู ชนิดทพั เพลิ ตวั อยา่ ง: aList = (123, 'xyz', 'zara', 'abc'); aTuple = tuple(aList) print (\"Tuple elements : \", aTuple) Tuple elements : (123, 'xyz', 'zara', 'abc') OUTPUT ฟังชนั และเมธอดสาหรบั ดิกชนั นารี (Built-in Dictionary Functions & Methods) ฟังชนั สาหรบั ดิกชนั นารี (Dictionary Functions)  len() ฟังชนั หาจานวนของสมาชกิ หรอื ความยาวในดกิ ชนั นารี หน้า 604

รปู แบบคาสงั่ : len(dict) พารามเิ ตอร:์ dict คอื ตวั แปรชนิดดกิ ชนั นารี คา่ ทส่ี ่งกลบั : จานวนของสมาชกิ ในดกิ ชนั นารี ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7}; print (\"Length : %d\" % len (dict)) Length : 2 OUTPUT  str() แสดงโครงสรา้ งของดกิ ชนั นารที งั้ หมดในรปู ของสตรงิ รปู แบบคาสงั่ : str(dict) พารามเิ ตอร:์ dict คอื ตวั แปรชนิดดกิ ชนั นารี คา่ ทส่ี ง่ กลบั : สตรงิ ทแ่ี สดงโครงสรา้ งของดกิ ชนั นารี ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7}; print (\"Equivalent String : %s\" % str (dict)) Equivalent String : {'Age': 7, 'Name': 'Zara'} OUTPUT  type() แสดงชนิดของตวั แปร รปู แบบคาสงั่ : type(variable) พารามเิ ตอร:์ variable คอื ออ๊ ปเจก็ ตใ์ ดๆ คา่ ทส่ี ่งกลบั : สตรงิ แสดงชนดิ ของตวั แปร ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7}; print (\"Variable Type : %s\" % type (dict)) Variable Type : <class 'dict'> OUTPUT เมธอดสาหรบั ดิกชนั นารี (Dictionary methods)  dict.clear() เคลยี รข์ อ้ มลู ทงั้ หมดออกจากดกิ ชนั นารี รปู แบบคาสงั่ : dict.clear() พารามเิ ตอร:์ dict คอื ตวั แปรดกิ ชนั นารี ค่าทส่ี ่งกลบั : ไมม่ ี หน้า 605

ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7}; print (\"Start Len : %d\" % len(dict)) dict.clear() print (\"End Len : %d\" % len(dict)) Start Len : 2 End Len : 0 OUTPUT  dict.copy() สาเนาขอ้ มลู ของดกิ ชนั นารี รปู แบบคาสงั่ : dict.copy() พารามเิ ตอร:์ dict คอื ตวั แปรดกิ ชนั นารี ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: dict1 = {'Name': 'Zara', 'Age': 7}; dict2 = dict1.copy() print (\"New Dictinary : %s\" % str(dict2)) New Dictinary : {'Name': 'Zara', 'Age': 7} OUTPUT  dict.fromkeys() สรา้ งดกิ ชนั นารใี หม่ โดยการสาเนาคยี ม์ าจากดกิ ชนั นารเี ดมิ หรอื กาหนดค่าเรมิ่ ตน้ ใหก้ บั ขอ้ มลู รปู แบบคาสงั่ : dict.fromkeys(seq[, value])) พารามเิ ตอร:์ seq คอื ดกิ ชนั นารเี ดมิ ทต่ี อ้ งการสาเนาคยี ์, value คอื คา่ เรม่ิ ตน้ ท่ี ตอ้ งการกาหนดใหก้ บั ตวั แปรดกิ ชนั นารที ส่ี รา้ งขน้ึ ใหม่ คา่ ทส่ี ง่ กลบั : ดกิ ชนั นารใี หมท่ ส่ี รา้ งขน้ึ ตวั อยา่ ง: seq = ('name', 'age', 'sex') dict = dict.fromkeys(seq) #new dict & copy keys print (\"New Dictionary : %s\" % str(dict)) dict = dict.fromkeys(seq, 10) #new dict by copy keys & set value print (\"New Dictionary : %s\" % str(dict)) )) New Dictionary : {'sex': None, 'name': None, 'age': None} New Dictionary : {'sex': 10, 'name': 10, 'age': 10} OUTPUT  dict.get() คน้ หาขอ้ มลู ในดกิ ชนั นารี โดยใชค้ ยี ใ์ นการคน้ หา รปู แบบคาสงั่ : dict.get(key, default=None) พารามเิ ตอร:์ key คอื คยี ท์ ใ่ี ชค้ น้ หา, default ระบุค่าทต่ี อ้ งการใหส้ ง่ กลบั หน้า 606

คา่ ทส่ี ่งกลบั : แสดงขอ้ มลู ทค่ี น้ พบ แต่ถา้ ไมพ่ บจะคนื ค่าเป็น None ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 27} print (\"Value : %s\" % dict.get('Age')) print (\"Value : %s\" % dict.get('Sex', \"Never\")) Value : 27 Value : Never OUTPUT  dict.items() แสดงค่ขู องคยี แ์ ละขอ้ มลู รปู แบบคาสงั่ : dict.items() พารามเิ ตอร:์ dict คอื ขอ้ มลู ชนิดดกิ ชนั นารี คา่ ทส่ี ่งกลบั : ลสิ ตท์ บ่ี รรจคุ ขู่ องคยี แ์ ละขอ้ มลู ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7} print (\"Value : %s\" % dict.items()) Value : dict_items([('Age', 7), ('Name', 'Zara')]) OUTPUT  dict.keys() แสดงรายการของคยี ท์ งั้ หมดในดกิ ชนั นารี รปู แบบคาสงั่ : dict.keys() พารามเิ ตอร:์ dict คอื ขอ้ มลู ชนดิ ดกิ ชนั นารี ค่าทส่ี ง่ กลบั : รายการของคยี ท์ งั้ หมด ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7} print (\"Value : %s\" % dict.keys()) Value : dict_keys(['Name', 'Age']) OUTPUT  dict.setdefault() ทางานคลา้ ย dict.get แต่แตกต่างทเ่ี มอ่ื คน้ หาคยี ใ์ ดๆ ในดกิ ชนั นารไี มเ่ จอ จะแทนทค่ี ยี แ์ ละขอ้ มลู ใหมใ่ ส่เขา้ ไปเป็นสมาชกิ ใหมท่ นั ที รปู แบบคาสงั่ : dict.setdefault(key, default=None) พารามเิ ตอร:์ dict คอื ขอ้ มลู ชนิดดกิ ชนั นาร,ี key คอื คยี ท์ ต่ี อ้ งการคน้ หา, default คอื ค่าทต่ี อ้ งการกาหนดใหเ้ มอ่ื คน้ หาขอ้ มลู ใดๆ ไมพ่ บ ถา้ ไมก่ าหนดจะ เป็น None คา่ ทส่ี ่งกลบั : รายการของขอ้ มลู ทงั้ หมด ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7} หน้า 607

print (\"Value : %s\" % dict.setdefault('Age', None)) print (\"Value : %s\" % dict.setdefault('Sex', None)) print (\"Value : %s\" % dict.setdefault('ID', '001')) print (dict) Value : 7 Value : None OUTPUT Value : 001 {'Age': 7, 'Sex': None, 'Name': 'Zara', 'ID': '001'}  dict.update() ปรบั ปรงุ หรอื เชอ่ื มขอ้ มลู ในดกิ ชนั นารี 2 ดกิ ชนั นารใี ดๆ เขา้ รปู แบบคาสงั่ : dict.update(dict2) พารามเิ ตอร:์ dict คอื ขอ้ มลู ชนดิ ดกิ ชนั นาร,ี dict2 คอื ขอ้ มลู ดกิ ชนั นารที ต่ี อ้ งการ เช่อื มต่อเขา้ กบั dict ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7} dict2 = {'Sex': 'female' } dict.update(dict2) print (\"Value : %s\" % dict) Value : {'Sex': 'female', 'Age': 7, 'Name': 'Zara'} OUTPUT  dict.values() แสดงรายการขอ้ มลู ในดกิ ชนั นารที งั้ หมด รปู แบบคาสงั่ : dict.values() พารามเิ ตอร:์ dict คอื ขอ้ มลู ชนิดดกิ ชนั นารี คา่ ทส่ี ่งกลบั : ลสิ ตข์ องรายการขอ้ มลู (values) ในดกิ ชนั นารที งั้ หมด ตวั อยา่ ง: dict = {'Name': 'Zara', 'Age': 7} print (\"Value : %s\" % dict.values()) Value : dict_values(['Zara', 7]) OUTPUT  dict.has_key(key) คน้ หาวา่ คยี อ์ ยใู่ นดกิ ชนั นารหี รอื ไม่ จะไม่มใี นไพธอน 3.0 แลว้ แต่ ผเู้ ขยี นโปรแกรมสามารถใช้ in แทนไดด้ งั น้ี >>> d = {'a': 1, 'b': 2} >>> check = 'a' in d >>> print(check) True หน้า 608

ฟังชนั และเมธอดสาหรบั เซต (Set built-in functions and methods) ฟังชนั สาหรบั เซต  len() คานวณจานวนสมาชกิ ทงั้ หมดทอ่ี ยใู่ นเซต รปู แบบคาสงั่ : len(object) พารามเิ ตอร:์ object คอื ขอ้ มลู ชนิดเซต ค่าทส่ี ง่ กลบั : จานวนสมาชกิ ทงั้ หมดทอ่ี ยใู่ นเซต ตวั อยา่ ง: setx = {1, 2, 3, 4, 5} print(\"Number of set :\",len(setx)) Number of set : 5 OUTPUT  max() คน้ หาสมาชกิ ทม่ี คี ่ามากทส่ี ุดในเซต รปู แบบคาสงั่ : max(set) พารามเิ ตอร:์ set คอื ขอ้ มลู ชนิดเซต คา่ ทส่ี ่งกลบั : สมาชกิ ทม่ี คี ่ามากทส่ี ุดในเซต ตวั อยา่ ง: setx = {1, 2, 3, 4, 5, 6, 7} print(\"The largest item of set :\",max(setx))) The largest item of set : 7 OUTPUT  min() คน้ หาสมาชกิ ทม่ี คี ่าน้อยทส่ี ดุ ในเซต รปู แบบคาสงั่ : min(set) พารามเิ ตอร:์ set คอื ขอ้ มลู ชนดิ เซต ค่าทส่ี ่งกลบั : สมาชกิ ทม่ี คี ่าน้อยทส่ี ดุ ในเซต ตวั อยา่ ง: setx = {1, 2, 3, 4, 5, 6, 7} print(\"The smallest item of set :\",min(setx)) The smallest item of set : 1 OUTPUT เมธอดสาหรบั เซต หน้า 609

 s.clear() เคลยี รข์ อ้ มลู ทงั้ หมดในเซต รปู แบบคาสงั่ : s.clear() พารามเิ ตอร:์ s คอื ขอ้ มลู ชนดิ เซต ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} print(\"Before set was cleared :\",s) s.clear() print(\"After set was cleared :\",s) Before set was cleared : {1, 2, 3, 4, 5, 6, 7} After set was cleared : set() OUTPUT  s.copy() การทาสาเนาเซต รปู แบบคาสงั่ : s.copy() พารามเิ ตอร:์ s คอื ขอ้ มลู ชนิดเซต คา่ ทส่ี ง่ กลบั : เซตใหมท่ ส่ี าเนามาจากเซต s ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} newset = s.copy() print(\"New set after coopy :\",newset) New set after coopy : {1, 2, 3, 4, 5, 6, 7} OUTPUT  s.pop() การลบสมาชกิ ตวั แรกออกจากเซต รปู แบบคาสงั่ : s.pop() พารามเิ ตอร:์ s คอื ขอ้ มลู ชนิดเซต ค่าทส่ี ง่ กลบั : ไมม่ ี แต่ถา้ เซตวา่ งจะเกดิ error ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} print(\"After pop set :\",s.pop()) print(\"remained item in set :\",s) After pop set : 1 remained item in set : {2, 3, 4, 5, 6, 7} OUTPUT  s.add() เพมิ่ สมาชกิ เขา้ ไปในเซต รปู แบบคาสงั่ : s.add(new) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนดิ เซต, new คอื ขอ้ มลู ใหมท่ ต่ี อ้ งการเพม่ิ เขา้ ไปในเซต หน้า 610

คา่ ทส่ี ่งกลบั : ไมม่ ี แต่ถา้ เซตวา่ งจะเกดิ error ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} s.add(8) print(\"After insert item into set :\",s) After insert item into set : {1, 2, 3, 4, 5, 6, 7, 8} OUTPUT  s.remove() ลบสมาชกิ ตวั ทต่ี อ้ งการออกจากเซต รปู แบบคาสงั่ : s.remove(old) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนิดเซต, old คอื ขอ้ มลู ทต่ี อ้ งการลบออกจากเซต คา่ ทส่ี ่งกลบั : ไมม่ ี แต่ถา้ เซตวา่ งจะเกดิ error ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} s.remove(5) print(\"After delete item from set :\",s) After delete item from set : {1, 2, 3, 4, 6, 7} OUTPUT  s.discard() ลบสมาชกิ ทต่ี อ้ งการออกจากเซต (ไมแ่ สดง error ถา้ ไมพ่ บสมาชกิ ทจ่ี ะลบ) รปู แบบคาสงั่ : s.discard(old) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนดิ เซต, old คอื ขอ้ มลู ทต่ี อ้ งการลบออกจากเซต คา่ ทส่ี ง่ กลบั : ไมม่ ี แต่ถา้ เซตว่าง จะไมเ่ กดิ error เหมอื น remove ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} s.discard(5) print(\"After delete item from set :\",s) s.discard(5) After delete item from set : {1, 2, 3, 4, 6, 7} OUTPUT  s.update() ทาการรวม 2 เซตใดๆ เขา้ ดว้ ยกนั รปู แบบคาสงั่ : s.update(new) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนิดเซต, new คอื เซตใหมท่ ต่ี อ้ งการรวมกบั s ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} s1 = {4, 5, 8, 9} หน้า 611

s.update(s1) print(\"After update set :\",s) After update set : {1, 2, 3, 4, 5, 6, 7, 8, 9} OUTPUT  s.intersection_update() intersection ระหว่าง 2 เซตใดๆ รปู แบบคาสงั่ : s.intersection_update(new) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนดิ เซต, new คอื เซตใหมท่ ต่ี อ้ งการทา intersection ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} s1 = {4, 5, 8, 9} s.intersection_update(s1) print(\"After intersect set :\",s) After intersect set : {4, 5} OUTPUT  s.difference_update() ลบสมาชกิ ทเ่ี หมอื นกนั ออกจาก s รปู แบบคาสงั่ : s.difference_update(new) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนดิ เซต, new คอื เซตทใ่ี ชใ้ นการเปรยี บเทยี บ ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} new = {4, 5, 8, 9} s.difference_update(new) print(\"After difference update set :\", s) After difference update set : {1, 2, 3, 6, 7} OUTPUT  s.issubset() ตรวจสอบความเป็น subset รปู แบบคาสงั่ : s.issubset(set) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนิดเซต, set คอื เซตทใ่ี ชใ้ นการเปรยี บเทยี บ คา่ ทส่ี ่งกลบั : เป็นจรงิ ถา้ s เป็น subset ของ set ถา้ ไมใ่ ชจ่ ะเป็นเทจ็ ตวั อยา่ ง: set = {1, 2, 3, 4, 5, 6, 7} s = {3,4} print(\"After check subset :\",s.issubset(set)) หน้า 612

After check subset : True OUTPUT  s.union() union ระหว่าง s และ new รปู แบบคาสงั่ : s.union(new) พารามเิ ตอร:์ s คอื ขอ้ มลู ชนิดเซต, new คอื เซตทใ่ี ชด้ าเนินการ union ค่าทส่ี ่งกลบั : เซตทผ่ี ่านการดาเนนิ การ union มาแลว้ ตวั อยา่ ง: s = {1, 2, 3, 4, 5, 6, 7} new = {4, 5, 8, 9} s1 = s.union(new) print(\"After union set :\", s1) After union set : {1, 2, 3, 4, 5, 6, 7, 8, 9} OUTPUT 3. เมธอดเกี่ยวกบั การบริหารจดั การแฟ้มข้อมลู และไดเรคทรอร่ี (File & Directory Methods) การใชง้ านเมธอดเกย่ี วกบั แฟ้มขอ้ มลู จาเป็นตอ้ งเปิดแฟ้มดว้ ยเมธอด open() และใชอ้ ๊อปเจก็ (File object) ทไ่ี ดจ้ ากเมธอด open เพ่อื อา้ งองิ ถงึ แฟ้มจรงิ ในระบบ ทงั้ น้กี ารใชเ้ มธอดใดๆ ตอ้ งขน้ึ อยกู่ บั โหมดทก่ี าหนดในเมธอด open ดว้ ย เช่น ถา้ ตอ้ งการเขยี นแฟ้มแบบต่อเน่อื งจะตอ้ งเปิดโดยใชโ้ หมด a+ เป็นตน้  file.close(): ปิดแฟ้มทเ่ี ปิดใชง้ านอยู่ เมอ่ื แฟ้มถูกปิดลงแลว้ จะไมส่ ามารถดาเนินการใดๆ กบั แฟ้มไดอ้ กี ถา้ มกี ารอ่านแฟ้มจะเกดิ ValueError, ขณะเปิดแฟ้ม A ขา้ งไว้ และเปิดแฟ้ม B อกี โดยใชต้ วั แปรทอ่ี า้ งองิ ตวั เดยี วกนั แฟ้ม A จะถูกปิดโดยอตั โนมตั ิ รปู แบบคาสงั่ : file.close() พารามเิ ตอร:์ ไมม่ ี ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: # Open a file file = open(\"test.txt\", \"wb\") print (\"Name of the file: \", file.name) # Close opend file file.close() เอาตพ์ ตุ : หน้า 613

Name of the file: test.txt OUTPUT  file.flush(): เขยี นขอ้ มลู ในหน่วยความจาลงแฟ้ม เมอ่ื ทาการปิดแฟ้ม ขอ้ มลู ทอ่ี ยใู่ น หน่วยความจาจะถูกเขยี นลงแฟ้มก่อนแฟ้มถกู ปิด แต่ผเู้ ขยี นโปรแกรมสามารถสงั่ ใหเ้ ขยี น ขอ้ มลู ลงแฟ้มก่อนได้ ดว้ ยเมธอด flush รปู แบบคาสงั่ : file.flush() พารามเิ ตอร:์ ไมม่ ี ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: # Open a file file = open(\"test.txt\", \"wb\") print (\"Name of the file: \", file.name) # flush operation file.flush() # Close opend file file.close() เอาตพ์ ุต: Name of the file: test.txt OUTPUT  file.fileno(): แสดงค่าหมายเลขอา้ งองิ แฟ้มจรงิ โดยปกตริ ะบบปฏบิ ตั กิ ารจะอา้ งองิ แฟ้มใดๆ จะใชค้ ่าจานวนเตม็ ในการอ้างองิ เรยี กว่า integer file descriptor เมธอด fileno() จะส่งค่า ตวั เลขดงั กล่าวใหก้ บั ผเู้ รยี กใชง้ าน รปู แบบคาสงั่ : file.fileno() พารามเิ ตอร:์ ไมม่ ี ค่าทส่ี ง่ กลบั : ตวั เลขจานวนเตม็ บวก ตวั อยา่ ง: # Open a file file = open(\"test.txt\", \"wb\") print (\"Name of the file: \", file.name) fid = file.fileno() print (\"File Descriptor: \", fid) # Close opend file file.close() เอาตพ์ ุต: หน้า 614

Name of the file: test.txt File Descriptor: 3 OUTPUT  file.isatty(): เช่อื มต่อแฟ้มกบั เทอรม์ นิ อล (tty) ในระบบปฏบิ ตั กิ ารยนู ิกสห์ รอื ลนิ ุกซ์ จะมอง ว่าโครงสรา้ งแฟ้มเป็นอุปกรณ์หน่งึ ๆ ทเ่ี ชอ่ื มต่อกบั ระบบปฏบิ ตั กิ าร เมอ่ื ตอ้ งการใชง้ านแฟ้ม จาเป็นตอ้ งสงั่ เช่อื มโครงสรา้ งเขา้ กบั โครงสรา้ งแฟ้มของระบบ (mount) การเขา้ ถงึ แฟ้มในยู นิกสจ์ ะผ่านโปรแกรมตวั หน่ึง บางครงั้ เรยี กเทอรม์ นิ อล (Terminal) เมอ่ื แฟ้มดงั กล่าวถกู เปิด ดว้ ยเทอรม์ นิ อล เรยี กว่าการเช่อื ม (connect) รปู แบบคาสงั่ : file.fileno() พารามเิ ตอร:์ ไมม่ ี ค่าทส่ี ง่ กลบั : เป็นจรงิ เมอ่ื แฟ้มดงั กลา่ วเช่อื มกบั เทอรม์ นิ อล กรณอี ่นื ๆ จะเป็นเทจ็ ตวั อยา่ ง: status = file.isatty() print (\"Return value : \", status) เอาตพ์ ุต: Return value : False OUTPUT  next(): ทาหน้าทอ่ี ่านขอ้ มลู จากแฟ้มเขา้ มาทางานครงั้ ละ 1 บรรทดั จนกวา่ จะหมดแฟ้ม นยิ มใชง้ านควบคกู่ บั คาสงั่ วบซ้า คอื for หมายเหตุ: file.next() ถูกยกเลกิ การใชง้ านในไพธอน 3 รปู แบบคาสงั่ : next(file) พารามเิ ตอร:์ ไมม่ ี ค่าทส่ี ่งกลบั : ขอ้ ความบรรทดั ถดั ไป ตวั อยา่ ง: file = open(\"INPUT.txt\", 'r+') for index in range(5): line = next(file) print (\"Line No %d - %s\" % (index, line)) file.close() อนิ พตุ : first line second line third line forth line fifth line เอาตพ์ ุต: หน้า 615

Line No 0 - first line Line No 1 - second line OUTPUT Line No 2 - third line Line No 3 - forth line Line No 4 - fifth line  file.read(): ทาหน้าทอ่ี ่านขอ้ มลู จากแฟ้มทงั้ หมด รปู แบบคาสงั่ : file.read(size) พารามเิ ตอร:์ size ขนาดของขอ้ มลู ทต่ี อ้ งการอ่านจากแฟ้ม มหี น่วยเป็นไบต์ คา่ ทส่ี ่งกลบั : ขอ้ มลู ทอ่ี ่านจากแฟ้ม มหี น่วยเป็นไบต์ ตวั อยา่ ง: file = open(\"INPUT.txt\", 'r+') line = file.read(10) print (\"Read Line: %s\" % (line)) file.close() เอาตพ์ ุต: Read Line: first line OUTPUT  file.readline(): ทาหน้าทอ่ี ่านขอ้ มลู จากแฟ้มทลี ะบรรทดั รปู แบบคาสงั่ : file.readline([size]) พารามเิ ตอร:์ size ขนาดของขอ้ มลู ทต่ี อ้ งการอ่านจากแฟ้ม มหี น่วยเป็นไบต์ เมอ่ื ไม่ กาหนดจะอ่านทลี ะบรรทดั จนหมดแฟ้ม ค่าทส่ี ง่ กลบั : ขอ้ มลู ทอ่ี ่านจากแฟ้ม ครงั้ ละ 1 บรรทดั ตวั อยา่ ง: file = open(\"INPUT.txt\", 'r+') line = file.readline() print (\"Read Line: %s\" % (line)) line = file.readline(5) print (\"Read Line: %s\" % (line)) file.close() เอาตพ์ ตุ : Read Line: first line Read Line: secon OUTPUT  file.readlines(): ทาหน้าทอ่ี ่านขอ้ มลู จากแฟ้มครงั้ ละหลายบรรทดั รปู แบบคาสงั่ : file.readline([sizehint]) หน้า 616

พารามเิ ตอร:์ sizehint ขนาดของขอ้ มลู ทต่ี อ้ งการอ่านจากแฟ้ม มหี น่วยเป็นไบต์ เมอ่ื กาหนดค่า sizehint จานวนบรรทดั จะถกู คานวณจากค่า sizehint แทน การอ่านแฟ้มทงั้ แฟ้ม ค่าทส่ี ง่ กลบั : ขอ้ มลู ทอ่ี ่านจากแฟ้มครงั้ ละหลายบรรทดั แต่ละบรรทดั จะปิดดว้ ย \\n ตวั อยา่ ง: file = open(\"INPUT.txt\", 'r+') line = file.readlines() print (\"Read Line: %s\" % (line)) line = file.readlines(2) print (\"Read Line: %s\" % (line)) file.close() เอาตพ์ ตุ : Read Line: ['first line\\n', 'second line\\n', 'third line\\n', 'forth line\\n', 'fifth line'] OUTPUT Read Line: []  file.seek(): เลอ่ื นตาแหน่งตวั ชใ้ี นแฟ้มตามค่าทร่ี ะบุใน offset รปู แบบคาสงั่ : file.seek(offset[, whence]) พารามเิ ตอร:์ offset เป็นตาแหน่งทเ่ี รม่ิ อ่าน-เขยี นแฟ้มขอ้ มลู , whence (option) เป็น การกาหนดจดุ อา้ งองิ ในการอ่าน-เขยี นแฟ้ม โดยปกตจิ ะเป็น 0 หมายถงึ อา้ งองิ จากตน้ แฟ้ม ถา้ กาหนดเป็น 1 คอื อา้ งองิ จากตาแหน่ง ปัจจบุ นั และถา้ เป็น 2 หมายถงึ อา้ งกบั ตาแหน่งจดุ จบของแฟ้ม คา่ ทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: file = open(\"INPUT.txt\", 'r+') line = file.readline() print (\"Read Line: %s\" % (line)) # Again set the pointer to the beginning file.seek(0, 0) line = file.readline() print (\"Read Line: %s\" % (line)) # Again set the pointer to the current position file.seek(0, 1) line = file.readline() print (\"Read Line: %s\" % (line)) file.close() อนิ พตุ : หน้า 617

Input File: TEST.txt first line second line third line forth line fifth line เอาตพ์ ตุ : Read Line: first line Read Line: first line OUTPUT Read Line: second line  file.tell(): ตรวจสอบตาแหน่งตวั ชแ้ี ฟ้มปัจจบุ นั รปู แบบคาสงั่ : file.tell() พารามเิ ตอร:์ ไมม่ ี ค่าทส่ี ่งกลบั : ตาแหน่งของตวั ชแ้ี ฟ้มทก่ี าลงั อ่าน-เขยี นในปัจจบุ นั ตวั อยา่ ง: file = open(\"INPUT.txt\", 'r+') line = file.readline() print (\"Read Line: %s\" % (line)) # Get the current position of the file. pos = file.tell() print (\"Current Position: %d\" % (pos)) file.close() เอาตพ์ ตุ : Read Line: first line Current Position: 12 OUTPUT  file.write(): เขยี นขอ้ มลู ลงแฟ้ม รปู แบบคาสงั่ : file.write(str) พารามเิ ตอร:์ ขอ้ ความหรอื สตรงิ ทต่ี อ้ งการเขยี น ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: file = open(\"INPUT.txt\", 'a+') str = \"\\nsixth line\" # Write a line at the end of the file. file.seek(0, 2) line = fo.write( str ) file = open(\"INPUT.txt\", \"r+\") # Now read complete file from beginning. file.seek(0,0) for index in range(6): line = next(file) หน้า 618

print (\"Line No %d - %s\" % (index, line)) file.close() เอาตพ์ ตุ : Line No 0 - first line Line No 1 - second line OUTPUT Line No 2 - third line Line No 3 - forth line Line No 4 - fifth line Line No 5 - sixth line  file.writelines(): เขยี นขอ้ มลู ลงแฟ้มครงั้ ละหลายบรรทดั รปู แบบคาสงั่ : file.writelines(sequence) พารามเิ ตอร:์ ชุดของขอ้ ความหรอื สตรงิ ทต่ี อ้ งการเขยี น คา่ ทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: file = open(\"INPUT.txt\", 'a+') seq = [\"This is 6th line\\n\", \"This is 7th line\"] # Write sequence of lines at the end of the file. file.seek(0, 2) line = file.writelines( seq ) # Now read complete file from beginning. file.seek(0,0) for index in range(7): line = next(file) print (\"Line No %d - %s\" % (index, line)) file.close() เอาตพ์ ุต: Line No 0 - first line Line No 1 - second line OUTPUT Line No 2 - third line Line No 3 - forth line Line No 4 - fifth line Line No 5 - This is 6th line Line No 6 - This is 7th line  os.access(): ทดสอบสทิ ธใิ ์ นการเขา้ ถงึ แฟ้ม โดยใช้ GID/UID รปู แบบคาสงั่ : os.access(path, mode) พารามเิ ตอร:์ path คอื ทอ่ี ยขู่ องแฟ้มขอ้ มลู ทต่ี อ้ งการทดสอบ Mode คอื คณุ สมบตั ทิ ใ่ี ชส้ าหรบั เขา้ ถงึ แฟ้ม ประกอบดว้ ย os.F_OK คอื ทดสอบวา่ แฟ้มดงั กล่าวมอี ยจู่ รงิ os.R_OK คอื ทดสอบว่าแฟ้มดงั กล่าวงสามารถอ่านได้ os.W_OK คอื ทดสอบว่าแฟ้มดงั กล่าวงสามารถเขยี นได้ หน้า 619

os.X_OK คอื ทดสอบว่าแฟ้มดงั กล่าวงสามารถประมวลผลได้ ค่าทส่ี ง่ กลบั : เป็นจรงิ เมอ่ื เป็นไปตามเงอ่ื นไขทก่ี าหนดไวใ้ นโหมด ตวั อยา่ ง: import os, sys, stat # Assuming C:\\Python34\\INPUT.txt exists, Set a file execute by the group. os.chmod(\"C:\\Python34\\INPUT.txt\", stat.S_IXGRP) # Set a file write by others. os.chmod(\"C:\\Python34\\INPUT.txt\", stat.S_IWOTH) print (\"Changed mode successfully!!\") เอาตพ์ ุต: Changed mode successfully!! OUTPUT  os.chdir(): เปลย่ี นตาแหน่งทอ่ี ย่ไู ปยงั ไดเรคทรอรใ่ี หม่ รปู แบบคาสงั่ : os.chdir(path) พารามเิ ตอร:์ path คอื ทอ่ี ยใู่ หมข่ องไดเรคทรอรท่ี ต่ี อ้ งการยา้ ยไป คา่ ทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: import os path = \"c:\\windows\" # Check current working directory. cur_dir = os.getcwd() print (\"Current working directory %s\" % cur_dir) # Now change the directory os.chdir( path ) # Check current working directory. cur_dir = os.getcwd() print (\"Directory changed successfully %s\" % cur_dir) เอาตพ์ ุต: Current working directory C:\\Python34 Directory changed successfully c:\\windows OUTPUT  os.chmod(): เปลย่ี นสทิ ธขิ ์ องแฟ้มหรอื ไดเรคทรอร่ี (ใชบ้ นยนู กิ ส-์ ลนิ ุกซ)์ รปู แบบคาสงั่ : os.chmod(path, mod) พารามเิ ตอร:์ path คอื ทอ่ี ยขู่ องแฟ้ม หรอื ไดเรคทรอรท่ี ต่ี อ้ งการเปลย่ี นสทิ ธิ ์ mode คอื สทิ ธทิ ์ ต่ี อ้ งการกาหนด ดงั น้ี stat.S_ISUID กาหนดคา่ UID (User ID) ใหแ้ ฟ้มขอ้ มลู หน้า 620

stat.S_ISGID กาหนดค่า GID (Group ID) ใหแ้ ฟ้มขอ้ มลู stat.S_ENFMT สงั่ ใหบ้ นั ทกึ การใชง้ านแฟ้ม stat.S_ISVTX บนั ทกึ ภาพขอ้ ความหลงั จากประมวลผล stat.S_IREAD เจา้ ของแฟ้มสามารถอ่านได้ stat.S_IWRITE เจา้ ของแฟ้มสามารถเขยี นได้ stat.S_IEXEC เจา้ ของแฟ้มสามารถประมวลผลได้ stat.S_IRWXU เจา้ ของแฟ้ม อ่าน เขยี น และประมวลผลแฟ้มได้ stat.S_IRUSR เจา้ ของแฟ้มสามารถอ่านได้ stat.S_IWUSR เจา้ ของแฟ้มสามารถเขยี นได้ stat.S_IXUSR เจา้ ของแฟ้มสามารถประมวลผลได้ stat.S_IRWXG สมาชกิ กลุ่มเดยี วกบั เจา้ ของแฟ้ม อ่าน เขยี น และ ประมวลผลแฟ้มได้ stat.S_IRGRP สมาชกิ ในกลุ่มสามารถอ่านได้ stat.S_IWGRP สมาชกิ ในกลมุ่ สามารถเขยี นได้ stat.S_IXGRP สมาชกิ ในกลมุ่ สามารถประมวลผลได้ stat.S_IRWXO สมาชกิ กลุ่มอ่นื ๆ สามารถอ่าน เขยี น และประมวลผล stat.S_IROTH สมาชกิ กลุ่มอ่นื ๆ สามารถอ่านได้ stat.S_IWOTH สมาชกิ กลุ่มอ่นื ๆ สามารถเขยี นได้ stat.S_IXOTH สมาชกิ กลุม่ อ่นื ๆ สามารถประมวลผลได้ คา่ ทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: #!/usr/bin/python import os, sys, stat # Assuming /tmp/test.txt exists, Set a file execute by the group. os.chmod(\"/tmp/test.txt\", stat.S_IXGRP) # Set a file write by others. os.chmod(\"/tmp/test.txt\", stat.S_IWOTH) print \"Changed mode successfully!!\" เอาตพ์ ตุ : Changed mode successfully!! OUTPUT  os.chown(): เปลย่ี นสทิ ธผิ ์ ูค้ รอบครองแฟ้มหรอื ไดเรคทรอร่ี (ใชบ้ นยนู ิกส-์ ลนิ ุกซ)์ รปู แบบคาสงั่ : os.chown(path, uid, gid) หน้า 621

พารามเิ ตอร:์ path คอื แฟ้ม หรอื ไดเรคทรอรท่ี ต่ี อ้ งการเปลย่ี นสทิ ธผิ ์ คู้ รอบครอง uid คอื หมายเลขทใ่ี ชร้ ะบุตวั ตน ของเจา้ ของแฟ้มหรอื ไดเรคทรอร่ี gid คอื หมายเลขทใ่ี ชร้ ะบกุ ลมุ่ ของเจา้ ของแฟ้มหรอื ไดเรคทรอร่ี ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # Assuming /tmp/test.txt exists. # To set owner uid = 99, gid = -1 (for group of super user). os.chown(\"/tmp/test.txt\", 99, -1) print \"Changed ownership successfully!!\" เอาตพ์ ุต: Changed ownership successfully!! OUTPUT  os.chroot(): เปลย่ี นดฟี อลท์ (default) ไดเรคทรอรข่ี อง root (ใชบ้ นยนู กิ ส-์ ลนิ ุกซ)์ รปู แบบคาสงั่ : os.chroot(path) พารามเิ ตอร:์ path คอื ไดเรคทรอรใ่ี หมท่ ต่ี อ้ งการเปลย่ี นเป็นดฟี อลทไ์ ดเรคทรอร่ี ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: #!/usr/bin/python import os, sys # To set the current root path to /tmp/user os.chroot(\"/tmp/usr\") print \"Changed root path successfully!!\" เอาตพ์ ตุ : Changed root path successfully!! OUTPUT  os.getcwd(): แสดงไดเรคทรอรท่ี ท่ี างานอยใู่ นปัจจุบนั รปู แบบคาสงั่ : os.getcwd() พารามเิ ตอร:์ ไมม่ ี คา่ ทส่ี ง่ กลบั : ไดเรคทรอรป่ี ัจุบนั ตวั อยา่ ง: import os, sys # Print current working directory print (\"Current working dir : %s\" % os.getcwd()) หน้า 622

เอาตพ์ ุต: Current working dir : C:\\Python34 OUTPUT  os.link(): สรา้ ง shortcut ของแฟ้มขอ้ มลู รปู แบบคาสงั่ : os.link(src, dst) พารามเิ ตอร:์ src คอื แฟ้มตน้ ฉบบั ทต่ี อ้ งการสรา้ ง shortcut dst คอื shortcut ใหมท่ เ่ี ช่อื มโยงไปยงั แฟ้ม src ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # Open a file src = \"C:\\Python34\\INPUT.txt\" fd = os.open(src, os.O_RDWR|os.O_CREAT) # Close opened file os.close(fd) # Now create another copy of the above file. dst = \"C:\\Python34\\LINK_INPUT.txt\" os.link( src, dst) print (\"Created hard link successfully!!\") เอาตพ์ ุต: Created hard link successfully!! OUTPUT  os.listdir(): แสดงรายชอ่ื แฟ้มและไดเรคทรอรย่ี อ่ ยภายในไดเรคทรอรท่ี ก่ี าลงั ทางานอยู่ โดยไม่รวมไดเรคทรอรช่ี นดิ . และ .. รปู แบบคาสงั่ : os.listdir(path) พารามเิ ตอร:์ path คอื ช่อื ไดเรคทรอรท่ี ต่ี อ้ งการแสดงรายการของแฟ้ม และไดเรคท รอรย่ี อ่ ย คา่ ทส่ี ่งกลบั : รายการแฟ้ม และไดเรคทรอรย่ี อ่ ยทงั้ หมด ตวั อยา่ ง: import os, sys path = \"C:\\Python34\" dirs = os.listdir( path ) # This would print all the files and directories for file in dirs: print (file) เอาตพ์ ตุ : หน้า 623

CalArea CalAreaRectangle.py OUTPUT DLLs … testfile testx.py Tools __pycache__  os.makedir(), os.makedirs() : สรา้ งไดเรคทรอรใ่ี หม่ รปู แบบคาสงั่ : os.makedir(path [,mode]), os.makedirs(path [,mode]) พารามเิ ตอร:์ path คอื ชอ่ื ไดเรคทรอรท่ี ต่ี อ้ งการสรา้ งใหม่ mode (option) เป็นการกาหนดสทิ ธใิ ์ หก้ บั ไดเรคทรอรท่ี ส่ี รา้ งขน้ึ ใหม่ ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # Path to be created path = \"C:\\Python34\\TEST\" os.makedirs(path, 755); print (\"Path is created\") เอาตพ์ ตุ : Path is created OUTPUT  os.open(): เปิดแฟ้มขอ้ มลู เพอ่ื อ่าน-เขยี น (บนยนู ิกส-์ ลนิ ุกซ)์ รปู แบบคาสงั่ : os.open(file, flags[, mode]) พารามเิ ตอร:์ file คอื ชอ่ื แฟ้มทต่ี อ้ งการอ่าน-เขยี น flags คอื โหมดในการเปิดแฟ้ม (เป็นคา่ คงท)่ี ดงั น้ี os.O_RDONLY เปิดเพอ่ื อ่านอยา่ งเดยี ว os.O_WRONLY เปิดเพอ่ื เขยี นเท่านนั้ os.O_RDWR เปิดเพอ่ื อ่าน และเขยี น os.O_NONBLOCK ไมป่ ิดกนั แฟ้มทต่ี อ้ งการเปิดใชง้ าน os.O_APPEND เปิดเพ่อื เขยี นขอ้ มลู เพม่ิ os.O_CREAT เปิดแฟ้ม ถา้ ไมม่ แี ฟ้มใหส้ รา้ งใหม่ os.O_TRUNC ตดั หรอื ลดขนาดแฟ้มใหม้ ขี นาดเท่ากบั 0 os.O_EXCL สรา้ งแฟ้มใหม่ ถา้ มแี ฟ้มอยแู่ ลว้ จะแสดงขอ้ ผดิ พลาด os.O_SHLOCK ไมอ่ นุญาตใหแ้ ชรแ์ ฟ้มขอ้ มลู os.O_EXLOCK เปิดแฟ้มชนดิ exclusive lock หน้า 624

os.O_DIRECT ไมส่ นใจผลกระทบทจ่ี าก cache ขณะเปิดแฟ้ม os.O_FSYNC เขยี นแฟ้มชนิดเขา้ จงั หวะ (synchronous) os.O_NOFOLLOW ไมส่ นับสนุนการทา symlinks mode (option) ทางานเหมอื นกบั mode ในคาสงั่ chmod ค่าทส่ี ่งกลบั : อ๊อปเจก็ ทอ่ี า้ งองิ ไปยงั แฟ้มขอ้ มลู จรงิ (File descriptor : แสดง รายละเอยี ดโครงสรา้ งแฟ้ม) ตวั อยา่ ง: import os, sys # Open a file fd = os.open( \"INPUT.txt\", os.O_RDWR|os.O_CREAT ) # Write one string os.write(fd, \"This is test\") # Close opened file os.close( fd ) print (\"Closed the file successfully!!\") เอาตพ์ ุต: Closed the file successfully!! OUTPUT  os.read() : อ่านขอ้ มลู จากแฟ้มทางานรว่ มกบั File descriptor (บนยนู กิ ส-์ ลนิ ุกซ)์ รปู แบบคาสงั่ : os.read(fd, n) พารามเิ ตอร:์ fd คอื File descriptor (แสดงรายละเอยี ดโครงสรา้ งแฟ้ม) n จานวนไบตข์ อ้ ทต่ี อ้ งการอ่าน คา่ ทส่ี ง่ กลบั : สายอกั ขระทเ่ี รยี งต่อกนั (ขอ้ มลู เป็นไบตเ์ รยี งต่อกนั ) ตวั อยา่ ง: import os, sys # Open a file fd = os.open(\"INPUT.txt\",os.O_RDWR) # Reading text 12 bytes ret = os.read(fd, 12) print (ret) # Close opened file os.close(fd) print (\"Closed the file successfully!!\") เอาตพ์ ตุ : b'first line\\n' Closed the file successfully!! OUTPUT หน้า 625

 os.remove() : ลบแฟ้มขอ้ มลู รปู แบบคาสงั่ : os.remove(path) พารามเิ ตอร:์ path คอื ช่อื แฟ้มทต่ี อ้ งการลบจากระบบ ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # listing directories print (\"The dir is: %s\" %os.listdir(os.getcwd())) # removing os.remove(\"t.txt\") # listing directories after removing path print (\"The dir after removal of path : %s\" %os.listdir(os.getcwd())) เอาตพ์ ุต: OUTPUT The dir is: ['CalArea', 'CalAreaRectangle.py', 'DLLs', 'Doc', 'exam10_1.py', 'exam10_2.py', 'exam10_9.py', t.txt,…] The dir is: ['CalArea', 'CalAreaRectangle.py', 'DLLs', 'Doc', 'exam10_1.py', 'exam10_2.py', 'exam10_9.py',…]  os.removedirs() : ลบไดเรคทรอร่ี รปู แบบคาสงั่ : os.removedirs(path) พารามเิ ตอร:์ path คอื ชอ่ื ไดเรคทรอรท่ี ต่ี ้องการลบจากระบบ ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # listing directories print (\"The dir is: %s\" %os.listdir(os.getcwd())) # removing os.removedirs(\"C:\\Python34\\TEST\") # listing directories after removing directory print (\"The dir after removal is:\" %os.listdir(os.getcwd())) เอาตพ์ ตุ : OUTPUT The dir is: ['CalArea', 'CalAreaRectangle.py', 'DLLs', 'Doc', 'exam10_1.py',TEST,…] … The dir after removal is: ['CalArea', 'CalAreaRectangle.py', 'DLLs', 'Doc', 'exam10_1.py',…] หน้า 626

 os.rename() : เปลย่ี นช่อื แฟ้มหรอื ไดเรคทรอร่ี รปู แบบคาสงั่ : os.rename(src, dst) พารามเิ ตอร:์ src คอื ช่อื แฟ้ม หรอื ไดเรคทรอรเ่ี ดมิ ทต่ี อ้ งการเปลย่ี นช่อื dst คอื ชอ่ื แฟ้ม หรอื ไดเรคทรอรใ่ี หม่ ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # listing directories print (\"The dir is: %s\"%os.listdir(os.getcwd())) # renaming directory os.rename(\"TEST\",\"TEST1\") print (\"Successfully renamed.\") # listing directories after renaming print (\"The dir after rename is: %s\" %os.listdir(os.getcwd())) เอาตพ์ ุต: OUTPUT The dir is: ['CalArea', 'CalAreaRectangle.py', 'DLLs', 'Doc', 'exam10_1.py',TEST,…] … The dir after rename is: ['CalArea', 'CalAreaRectangle.py', 'DLLs', 'Doc', 'exam10_1.py',TEST1,…]  os.renames() : เปลย่ี นชอ่ื แฟ้มหรอื ไดเรคทรอรเ่ี หมอื น rename แต่แตกต่างท่ี renames สามารถเปลย่ี นชอ่ื พรอ้ มกบั ยา้ ยไดเรคทรอรใ่ี หมด่ ว้ ย รปู แบบคาสงั่ : os.rename(old, new) พารามเิ ตอร:์ old คอื ชอ่ื แฟ้ม หรอื ไดเรคทรอรเ่ี ดมิ ทต่ี อ้ งการเปลย่ี นช่อื new คอื ช่อื แฟ้ม หรอื ไดเรคทรอรใ่ี หม่ ค่าทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: import os, sys print (\"Current directory is: %s\" %os.getcwd()) # listing directories print (\"The dir is: %s\"%os.listdir(os.getcwd())) # renaming file \"t.txt\" os.renames(\"t.txt\",\"newdir/tt.txt\") print (\"Successfully renamed.\") # listing directories after renaming and moving \"t.txt\" print (\"The dir is: %s\" %os.listdir(os.getcwd())) เอาตพ์ ุต: หน้า 627

Current directory is: C:\\Python34 The dir is: ['CalArea', OUTPUT 'CalAreaRectangle.py',…,t.txt,…] Successfully renamed. The dir is: ['CalArea', 'CalAreaRectangle.py',…,t.txt,…,newdir,…]  os.rmdir() : ลบไดเรคทรอร่ี (ตอ้ งเป็นไดเรคทรอรว่ี า่ ง) รปู แบบคาสงั่ : os.rmdir(path) พารามเิ ตอร:์ path คอื ชอ่ื แฟ้ม หรอื ไดเรคทรอรเ่ี ดมิ ทต่ี อ้ งการลบ ค่าทส่ี ่งกลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # listing directories print \"the dir is: %s\" %os.listdir(os.getcwd()) # removing path os.rmdir(\"mydir\") # listing directories after removing directory path print \"the dir is:\" %os.listdir(os.getcwd()) เอาตพ์ ตุ : The dir is: ['CalArea', 'CalAreaRectangle.py',…,t.txt,…,newdir,…] OUTPUT The dir is: ['CalArea', 'CalAreaRectangle.py',…,t.txt,…]  os.stat() : แสดงสถติ แิ ฟ้มขอ้ มลู จากการเรยี กใชง้ านของระบบปฏบิ ตั ิ รปู แบบคาสงั่ : os.stat(path) พารามเิ ตอร:์ path คอื ชอ่ื แฟ้มทต่ี อ้ งการแสดงสถติ ิ คา่ ทส่ี ่งกลบั : รายการขอ้ มลู สถติ ิ st_mode บติ ขอ้ มลู ทใ่ี ชส้ าหรบั กาหนดความปลอดภยั st_ino หมายเลข inode st_dev ชอ่ื อุปกรณ์ st_nlink จานวนลงิ คท์ เ่ี ชอ่ื มต่อ st_uid UID (user ID) ของผเู้ ป็นเจา้ ของแฟ้ม st_gid GID (group ID) ของผเู้ ป็นเจา้ ของแฟ้ม st_size ขนาดของแฟ้ม (มหี น่วยเป็นไบต)์ st_atime วนั -เวลาทเ่ี ขา้ ใชง้ านลา่ สุด st_mtime วนั -เวลาทป่ี รบั ปรงุ แฟ้มล่าสุด st_ctime วนั -เวลาทป่ี รบั ปรงุ แฟ้ม metadata ลา่ สดุ ตวั อยา่ ง: หน้า 628

import os, sys # showing stat information of file \"testx.py\" statinfo = os.stat('testx.py') print (statinfo) เอาตพ์ ตุ : os.stat_result(st_mode=33206, st_ino=1407374883790118, st_dev=1222005554, st_nlink=1, st_uid=0, st_gid=0, OUTPUT st_size=113, st_atime=1388819475, st_mtime=1389107870, st_ctime=1388819475)  os.utime() : กาหนดวนั -เวลา ในการเขา้ ใชง้ านและปรบั ปรงุ แฟ้ม รปู แบบคาสงั่ : os.utime(path[, times]) พารามเิ ตอร:์ path คอื ช่อื แฟ้มทต่ี อ้ งการกาหนดค่าเวลา time คอื เวลาใหมท่ ต่ี อ้ งการเปลย่ี นแปลงใหก้ บั แฟ้ม เมอ่ื ไม่ กาหนดเวลาทป่ี รบั ปรงุ ใหม่ จะใชเ้ วลาปัจจบุ นั ประกอบดว้ ย (atime, mtime) เช่น (accesstime และตามดว้ ย modifiedtime) มรี ปู แบบคอื yymmddHHMM คา่ ทส่ี ง่ กลบั : ไมม่ ี ตวั อยา่ ง: import os, sys # Showing stat information of file stinfo = os.stat('testx.py') print (stinfo) # Using os.stat to recieve atime and mtime of file print (\"access time of testx.py: %s\" %stinfo.st_atime) print (\"modified time of testx.py: %s\" %stinfo.st_mtime) # Modifying atime and mtime os.utime(\"testx.py\",(1330712280, 1330712292)) print (\"done!!\") เอาตพ์ ุต: os.stat_result(st_mode=33206, st_ino=1407374883790118, st_dev=1222005554, st_nlink=1, st_uid=0, st_gid=0, OUTPUT st_size=358, st_atime=1388819475, st_mtime=1389109069, st_ctime=1388819475) access time of testx.py: 1388819475.676249 modified time of testx.py: 1389109069.4353268  os.walk() : แสดงโครงสรา้ งของไดเรคทรอรใ่ี นรปู แบบของทรี รปู แบบคาสงั่ : os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) พารามเิ ตอร:์ top กาหนดไดเรคทรอรร่ี าก (root) ทต่ี อ้ งการแสดงผล (รายการทไ่ี ดร้ บั กลบั มาจะเกบ็ ในทพั เพลิ มี 3 ชนิด คอื dirpath, dirnames, filenames) หน้า 629