Python Cheat Sheet เบื้องต้น
เป็น Cheat Sheet สำหรับไวยากรณ์ภาษา Python เบื้องต้นสารบัญ
- การดำเนินการทางตรรกะพื้นฐาน
- Python Loop
- Python Strings
- Python Dictionaries
- Python List
- Python Tuples
- Python File
- Python Functions
- Python Class
การดำเนินการทางตรรกะพื้นฐาน
if :
if test:
หาก test เป็นจริงจะทำงานตามคำสั่งในนี้
elif test2:
หาก test2 เป็นจริงจะทำงานตามคำสั่งในนี้
else:
หากเงื่อนไขทั้งหมดเป็นเท็จ จะทำงานตามนี้
กลับขึ้นสารบัญ ^
Python loop
for :
for x in aSequence:
#ดำเนินการสมาชิกแต่ละตัวใน aSequence
#ตัวอย่างเช่น สมาชิกที่อยู่ใน list
#ตัวอักษรที่อยู่ใน string เป็นต้น
for x in range(10):
#ดำเนินการ 10 ครั้ง (0 ถึง 9)
for x in range(5,10):
#ดำเนินการ 5 ครั้ง (5 ถึง 9)
while :
while test:
#ทำจนกว่าจะสิ้นสุด
#จนกว่า test จะเป็น false
กลับขึ้นสารบัญ ^
Python Strings
Strings เป็นลำดับของอักษระที่เรียงต่อกัน มักเป็นข้อความที่เก็บไว้
การสร้าง :
the_string = "Hello World!"การเข้าถึงตัวอักษรตามลำดับ
the_string = 'Hello World!'
the_string[1] คืนค่า eการแยก String
the_string.split(" ") คืนค่า ['Hello', 'World!']ในการแปลง List เป็นสตริง เราสามารถทำได้ด้วยคำสั่ง
join()words = ["this", "is", "a", "list", "of", "strings"]
' '.join(words) คืนค่า "This is a list of strings"'ZOOL'.join(words) คืนค่า "ThisZOOLisZOOLaZOOLlistZOOLofZOOLstrings""".join(words) คืนค่า "Thisisalistofstrings"String Formatting : เหมือนกับ
printf() ของภาษา C โดยใช้ % หลังสตริงตามด้วย tuple โดยมีสตริงอยู่ภายใน เช่นthe_string = "Hello World!"
print("text : %s" % the_string)
ผลลัพธ์
text : Hello World!แปลงสตริงจากตัวพิมพ์ใหญ่เป็นตัวพิมพ์เล็ก
"PYTHON".lower() คืนค่า "python"แปลงสตริงจากตัวพิมพ์เล็กเป็นตัวพิมพ์ใหญ่
"python".upper() คืนค่า PYTHONนับจำนวนสตริงที่กำหนด
"pythonpy".count("py") คืนค่า 2ค้นหาตำแหน่งสตริงที่ต้องการ
"python".find("n") คืนค่า 5 และสามารถค้นหาแบบกำหนดช่วงได้ "pythonpython".find("n",4,6) คืนค่า 5แทนที่สตริง
"pythonpython".replace("py","go") คืนค่า "gothongothon"กลับขึ้นสารบัญ ^
Python Dictionaries
การสร้าง :
emptyDict = {}การดึงข้อมูล :
thisdict = {"a":1, "b":23, "c":"eggs"}
thisdict["a"] คืนค่า 1Python List
การสร้าง List :
thelist = ['5', '3', 'p', '9', 'e']การเข้าถึงข้อมูล :
thelist = list('53p9e') # ['5', '3', 'p', '9', 'e']
thelist[0] คืนค่า '5'ดึง List เฉพาะส่วนที่ต้องการ :
thelist[1:3] คืนค่า ['3', 'p']thelist[2:] คืนค่า ['p', '9', 'e']thelist[:2] คืนค่า ['5', '3']thelist[2:-1] คืนค่า ['p', '9']วัดความยาว
len(thelist) คืนค่า 5เรียงลำดับ list ใหม่
thelist.sort() ตอนนี้ list กลายเป็น [3,5,9,'e','p']เพิ่ม
thelist.append(7) ตอนนี้ list กลายเป็น [3,5,9,'e','p',7]คืนค่า และ ลบค่าออก
thelist.pop() คืนค่า 7 ตอนนี้ list กลายเป็น [3,5,9,'e','p']แทรก List
thelist.insert(1, 't') ตอนนี้ list กลายเป็น ['5', 't', '3', 'p', '9', 'e']ลบค่า
thelist.remove("t") ตอนนี้ list กลายเป็น ['5', '3', 'p', '9', 'e']del thelist[0] ตอนนี้ list กลายเป็น ['3', 'p', '9', 'e']บวก list
thelist + [0] คืนค่า ['3', 'p', '9', 'e', 0]ค้นหาค่าใน List
'3' in thelist คืนค่า Trueกลับขึ้นสารบัญ ^
Python Tuples
การสร้าง Tuples :
emptyTuple = ()การดึงข้อมูล :
singleItemTuple = ("spam",) # note the comma!
thistuple = 12, 89, 'a'
thistuple = (12, 89, 'a')
thistuple[0] คืนค่า 12กลับขึ้นสารบัญ ^
Python File
การเปิดไฟล์ :
thisfile = open("datadirectory/file.txt",โหมดเปิดไฟล์)โหมดการเปิดไฟล์
- w เขียนไฟล์
- r อ่านไฟล์
- a เขียนไฟล์ต่อจากข้อมูลเดิม
หากไม่กำหนดโหมดการเปิดไฟล์ ค่าพื้นฐานเป็น r อ่านได้เท่านั้น
คำสั่งเพิ่มเติม
thisfile.read()อ่านสตริงตัวแรกจากไฟล์thisfile.readline()อ่านข้อมูล 1 บรรทัดจากไฟล์thisfile.readlines()อ่านข้อมูลจากบรรทัดทั้งหมดในไฟล์for eachline in thisfile:อ่านบรรทัดต่อบรรทัดในไฟล์thisfile.write()เขียนไฟล์thisfile.close()ปิดไฟล์
หากทำงานกับภาษาไทย แนะนำให้ใช้
codecs โดยใช้ utf-8import codecs
thisfile = codecs.open("datadirectory/file.txt",โหมดเปิดไฟล์, encoding="utf-8")
กลับขึ้นสารบัญ ^
Python Functions
def myFunc(param1, param2):
# param1 และ param2 ใช้รับข้อมูลเมื่อใช้งาน myFunc(ข้อมูล1,ข้อมูล2)
# หากไม่มีการรับข้อมูล ไม่ต้องใส่ param ก็ได้
return param1+param2
กลับขึ้นสารบัญ ^
Python Class
class FC:
def __init__(self,one,ten):
self.one = one
self.ten = ten
def show(self):
print (self.ten,"-",self.one,"=",self.ten - self.one)
a = FC(one=1,ten=10)
a.show()
กลับขึ้นสารบัญ ^
อ้างอิง
http://www.cogsci.rpi.edu/~destem/igd/python_cheat_sheet.pdf
http://docs.python.org/
https://python3.wannaphong.com/

0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)