by jirawoot » 24/06/2019 4:47 pm
ตัวแปร list ในภาษา
python สามารถเก็บค่าในตัวแปรได้มากกว่า 1 ค่า การทำงานจะเหมือนกับ array ในภาษา PHP ตัวแปร list นั้นจะมี index เป็นตัวระบุตำแหน่งของข้อมูลเริ่มตั้งแต่ 0 ไปเรื่อยๆ
ในการประกาศตัวแปร list
จะต้องมีชื่อตัวแปร list แล้วตามด้วยเครื่องหมาย [ ] นะครับ
Code: Select all
name_list=['prthon','mindphp','PHP','test']
num = [1,2,3,4,5,6,7,8,9]
a1=[1,2,'prthon','mindphp','test']
การแสดงผลของตัวแปร
ในการแสดงผลเราจะใช้ คำสั่ง print เป็นตัวดึงข้อมูลในตัวแปร list ออกมาแสดงนะครับ
Code: Select all
name_list=['prthon','mindphp','PHP','test']
num = [1,2,3,4,5,6,7,8,9]
a1=[1,2,'prthon','mindphp','test']
print(name_list)
print(num)
print(a1)
ผลลัพธ์

- Selection_021.png (9.71 KiB) Viewed 483 times
จะมีการแสดงค่าโดยการกำหนด index ให้กับตัวแปรนั้นๆ
Code: Select all
name_list=['prthon','mindphp','PHP','test']
print('name_list[0] = ',name_list[0])
print('name_list[1] = ',name_list[1])
print('name_list[2] = ',name_list[2])
ผลลัพธ์จะได้ตามนี้

- Selection_022.png (7.19 KiB) Viewed 483 times
มีการแสดงค่าโดยการใช้ for loop จะมีตัวอย่างโด้ดดังนี้
Code: Select all
name_list=['prthon','mindphp','PHP','test', 56]
i=0
for n in name_list:
print('name_list[%d]'%(i),n)
i +=1
ผลลัพธ์จะได้ตามนี้

- Selection_023.png (5.73 KiB) Viewed 483 times
การลบค่าข้อมูลออกจากตัวแปร list
จะสามารถทำได้โดยการกำหนด index ให้กับตัวแปรที่จะลบข้อมูลออกจากหน่วยความจำก็จะใช้คำสั่ง del ตามด้วยตัวแปรแล้วระบุ index
Code: Select all
name_list=['prthon','mindphp','PHP','test', 56]
del name_list[2]
del name_list[-1]
print(name_list)
ผลลัพธ์จะได้ตามนี้

- Selection_024.png (7.07 KiB) Viewed 483 times
[b]ตัวแปร list[/b] ในภาษา [url=https://www.mindphp.com/คู่มือ/73-คืออะไร/2417-python-คืออะไร.html]python[/url] สามารถเก็บค่าในตัวแปรได้มากกว่า 1 ค่า การทำงานจะเหมือนกับ array ในภาษา PHP ตัวแปร list นั้นจะมี index เป็นตัวระบุตำแหน่งของข้อมูลเริ่มตั้งแต่ 0 ไปเรื่อยๆ
[b]ในการประกาศตัวแปร list [/b]
จะต้องมีชื่อตัวแปร list แล้วตามด้วยเครื่องหมาย [ ] นะครับ
[code]
name_list=['prthon','mindphp','PHP','test']
num = [1,2,3,4,5,6,7,8,9]
a1=[1,2,'prthon','mindphp','test']
[/code]
[b]การแสดงผลของตัวแปร[/b]
ในการแสดงผลเราจะใช้ คำสั่ง print เป็นตัวดึงข้อมูลในตัวแปร list ออกมาแสดงนะครับ
[code]
name_list=['prthon','mindphp','PHP','test']
num = [1,2,3,4,5,6,7,8,9]
a1=[1,2,'prthon','mindphp','test']
print(name_list)
print(num)
print(a1)
[/code]
ผลลัพธ์
[attachment=3]Selection_021.png[/attachment]
จะมีการแสดงค่าโดยการกำหนด index ให้กับตัวแปรนั้นๆ
[code]
name_list=['prthon','mindphp','PHP','test']
print('name_list[0] = ',name_list[0])
print('name_list[1] = ',name_list[1])
print('name_list[2] = ',name_list[2])
[/code]
ผลลัพธ์จะได้ตามนี้
[attachment=2]Selection_022.png[/attachment]
มีการแสดงค่าโดยการใช้ for loop จะมีตัวอย่างโด้ดดังนี้
[code]
name_list=['prthon','mindphp','PHP','test', 56]
i=0
for n in name_list:
print('name_list[%d]'%(i),n)
i +=1
[/code]
ผลลัพธ์จะได้ตามนี้
[attachment=1]Selection_023.png[/attachment]
[b]การลบค่าข้อมูลออกจากตัวแปร list[/b]
จะสามารถทำได้โดยการกำหนด index ให้กับตัวแปรที่จะลบข้อมูลออกจากหน่วยความจำก็จะใช้คำสั่ง del ตามด้วยตัวแปรแล้วระบุ index
[code]
name_list=['prthon','mindphp','PHP','test', 56]
del name_list[2]
del name_list[-1]
print(name_list)
[/code]
ผลลัพธ์จะได้ตามนี้
[attachment=0]Selection_024.png[/attachment]