ตัวอย่างตัวแปรตามชนิดข้อมูล

ว่ากันตามจริงแล้วตัวแปรในไพธอนใช้เหมือนกันหมดไม่มีข้อยกเว้นกรณีพิเศษใดๆ สิ่งที่แตกต่างไปของวิธีใช้ขึ้นกับชนิดข้อมูลของตัวแปรนั้นมากกว่า เพราะไพธอนเป็นภาษาที่อิงการเขียนโปรแกรมเชิงวัตถุ ด้วยเหตุนี้ตัวแปรที่ชี้ไปยังข้อมูลเหล่านี้จึงมีสภาพคล้ายกับอ๊อบเจ็กแต่ไม่ใช่อ๊อบเจ็ก อย่างไรก็ตามการคิดว่าชนิดข้อมูลเป็นอ๊อบเจ็กแบบพิเศษก็ไม่ผิดอะไรนัก แต่ชนิดข้อมูลพื้นฐานส่วนใหญ่ไม่มีเมธอดพิเศษเหมือนภาษาอื่น โดยเฉพาะอย่างยิ่งตรรกะและตัวเลข เมธอดที่ใช้บ่อยมักเกี่ยวกับลำดับและจับคู่ ตอนนี้เราก็จะมาว่ากันด้วยเรื่องของเมธอดที่ควรรู้กันทีละชุด

สตริง เป็นชนิดข้อมูลที่ใช้บ่อยมากๆ ส่วนใหญ่การทำงานกับสตริงจะเป็นเรื่องของค้นหาคำ find() เปลี่ยนคำ replace() ตัวเล็ก lower() ตัวใหญ่ upper() แยกคำ split() เป็นต้น นอกนั้นนี้ก็จะเป็นดึงตัวอักษรบางส่วนออกมาจากสตริง

s = "Ab,Cd,Ef"
print s.find("d")
print s.find("D")
print s.replace("Cd","Gh")
print s.lower()
print s.upper()
print s.split()
print s.split(",")

โค้ดด้านบนจะได้ผลลัพธ์ดังด้านล่าง

4
-1
Ab,Gh,Ef
ab,cd,ef,
AB,GH,EF
["A","b",",","C","d",",","E","f"]
["Ab","Cd","Ef"]

ลิสต์ เป็นชนิดข้อมูลสารพัดประโยชน์ ได้ใช้ทั้งแบบคิว และสแตก สามารถเรียงข้อมูลได้ด้วยตัวเอง แทรกข้อมูลได้

l = [1,2,3,4]
print l
l.append(5)
print l
l.append(1)
print l
print l.count(1),l.count(2)
l.extend([5,6,7])
print l
l.insert(5,8)
print l
print l.pop()
print l
l.remove(5)
print l
l.reverse()
print l
l.sort()
print l

ผลลัพธ์จะเป็นดังด้านล่าง

[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 1]
2 1
[1, 2, 3, 4, 5, 1, 5, 6, 7]
[1, 2, 3, 4, 5, 8, 1, 5, 6, 7]
7
[1, 2, 3, 4, 5, 8, 1, 5, 6]
[1, 2, 3, 4, 8, 1, 5, 6]
[6, 5, 1, 8, 4, 3, 2, 1]
[1, 1, 2, 3, 4, 5, 6, 8]

ดิกชั่นนารี เป็นชนิดข้อมูลสารพัดประโยชน์เช่นกัน เพราะใช้ง่าย สะดวก ไม่ต้องเขียนคลาสเพิ่ม สามารถเข้าถึงข้อมูลภายในได้ด้วยข้อมูลชนิดใดก็ได้

d = {"a": 1, "b": 2, "c": 3}
print d
print d.items()
print d.keys()
print d.values()

ตัวอย่างด้านบนจะให้ผลดังนี้

{"a": 1, "c": 3, "b": 2}
[("a", 1), ("c", 3), ("b", 2)]
["a", "c", "b"]
[1, 3, 2]

wow gold

lianyk
Welcome to wow gold our wow Gold and wow power leveling store. We wow gold are specilized, wow power leveling professional and reliable wow power leveling website for wow power leveling selling and wow gold service. By the World of Warcraft gold same token,we offer wow power leveling the best WoW service wow power leveling for our long-term and wow powerleveling loyal customers. wow powerleveling You will find wow powerleveling the benefits and value powerleveling we created powerleveling different from other sites. As to most people, power leveling they are unwilling to power leveling spend most of wow power leveling the time wow gold grinding money Rolex for mounts or rolex replica repair when replica rolex they can purchase Watches Rolex what they Rolex Watches are badly need. The Watch Rolex only way is to look Rolex Watch for the best place rs gold to buy cheap WOW gold. Yes! You find it here! Our WoW Gold supplying service has already accumulated a high reputation and credibility. We have plenty of Gold suppliers, which will guarantee our delivery instant. Actually, we have been getting Runescape Gold tons of postive feedbacks from our loyal RuneScape Money customers who really appreciate our service. ^_^..

Post new comment

The content of this field is kept private and will not be shown publicly.