ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
Moderators: mindphp, ผู้ดูแลกระดาน
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
ตัวแปรชนิด List(ลิส)
ตัวแปรชนิดลิส สามารถเก็บค่าได้มากกว่า 1 ค่าใน 1 ตัวแปร เพื่อการใช้งานที่หลากหลาย
การกำนดค่าตัวแปร List
-การกำหนดค่าให้กับตัวแปร List (ลิส) ต้องอยู่ภายใต้เครื่องหมาย [...] การเพิ่มค่าให้กับตัวแปร list
จะเห็นได้ว่า Update (อัพเดท) เข้าไปแทนที่ค่าเก่าก็คือ abc การลบค่าในตัวแปร List
จะเห็นได้ว่าเมื่อ Delete (ดีลิท) ตัวแปร abc ออก ตัวแปร 2013 จะเข้ามาแทนที่ ตัวดำเนินการของตัวแปรลิส
led([a , b , c]) คือความยาวของลิส ผลที่ได้ 3
[1 , 2 , 3] + [1 , 2] คือการเรียงต่อกัน ผลที่ได้ [1 , 2 , 3 , 1 , 2]
[1 , 2 , 3] * 3 คือการทำซ้ำ ผลที่ได้ [1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3]
1 in [1 , 2 , 3] คือการตรวจสอบสมาชิก ผลที่ได้ True (ทรู)
for x in [1 , 2 , 3]: print (x) คือการทวน ผลที่ได้ 1 2 3
ตัวอย่างการลองรันโค้ด ศึกษาข้อมูลจาก https://www.youtube.com/watch?v=pZ788u6 ... lzdKrpxsMM
ตัวแปรชนิดลิส สามารถเก็บค่าได้มากกว่า 1 ค่าใน 1 ตัวแปร เพื่อการใช้งานที่หลากหลาย
การกำนดค่าตัวแปร List
-การกำหนดค่าให้กับตัวแปร List (ลิส) ต้องอยู่ภายใต้เครื่องหมาย [...] การเพิ่มค่าให้กับตัวแปร list
จะเห็นได้ว่า Update (อัพเดท) เข้าไปแทนที่ค่าเก่าก็คือ abc การลบค่าในตัวแปร List
จะเห็นได้ว่าเมื่อ Delete (ดีลิท) ตัวแปร abc ออก ตัวแปร 2013 จะเข้ามาแทนที่ ตัวดำเนินการของตัวแปรลิส
led([a , b , c]) คือความยาวของลิส ผลที่ได้ 3
[1 , 2 , 3] + [1 , 2] คือการเรียงต่อกัน ผลที่ได้ [1 , 2 , 3 , 1 , 2]
[1 , 2 , 3] * 3 คือการทำซ้ำ ผลที่ได้ [1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3]
1 in [1 , 2 , 3] คือการตรวจสอบสมาชิก ผลที่ได้ True (ทรู)
for x in [1 , 2 , 3]: print (x) คือการทวน ผลที่ได้ 1 2 3
ตัวอย่างการลองรันโค้ด ศึกษาข้อมูลจาก https://www.youtube.com/watch?v=pZ788u6 ... lzdKrpxsMM
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
Re: ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
สามารถศึกษาข้อมูลเพิ่มเติมได้ที่ https://www.mindphp.com/vdo-tutorial-pyt ... -type.html
-
- PHP Sr. Member
- Posts: 84
- Joined: 01/02/2017 11:10 am
Re: ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
อ่านแล้วเข้าใจครับ
- Jom07
- PHP Super Hero Member
- Posts: 514
- Joined: 08/01/2018 9:56 am
Re: ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
Code: Select all
list1 = ["test", "test1", 2017, 2018]
list2 = [1, 2, 3, 4, 5]
print (list1[0])
print (list1[1:3])
print (list2[2:])
print (list1[-2])
ให้ listที่1 index ที่ 0 จะแสดง test
ให้ listที่1 index ที่ 1:3 จะแสดง test1,2017
ให้ listที่2 index ที่ 2: จะแสดง 3,4,5
ให้ listที่1 index ที่ -2 จะแสดง 2017
ศึกษาข้อมูลจาก :
https://www.youtube.com/watch?v=pZ788u6 ... MM&index=6

-
- PHP Hero Member
- Posts: 114
- Joined: 09/04/2018 10:04 am
Re: ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
Code: Select all
list1 = ["Jame", "Jan", "Game", "Tom"]
list2 = [9, 8, 7, 6, 5]
print (len(list1))
print (len(list2))
print (list1 + list2)
print (list2 * 2)
print (9 in list1)
print (7 in list2)
for x in list1
print x
ผลการรัน
[attachment=0]ผลลัพธ์ตัวดำเนินการ list.JPG[/attachment]
แสดงผลจำนวนสมาชิกใน list1 ทั้งหมด
แสดงผลจำนวนสมาชิกใน list2 ทั้งหมด
แสดงผลสมาชิกของ list1 และต่อด้วยสมาชิกของ list2
แสดงผลสมาชิกของ list2 ออกมาสองครั้ง เรียกว่าการทำซ้ำ
แสดงการตรวจสอบว่า 9 เป็นสมาชิกของ list1 หรือไม่
แสดงการตรวจสอบว่า 7 เป็นสมาชิกของ list2 หรือไม่
แสดงผลการตรวจทานสมาชิกทั้งหมดใน list1
ศึกษาจาก https://youtu.be/pZ788u6XsiM
- Attachments
-
- ผลลัพธ์ตัวดำเนินการ list.JPG (14.49 KiB) Viewed 679 times
-
- PHP Hero Member
- Posts: 199
- Joined: 30/04/2018 9:44 am
Re: ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
ตัวแปรชนิด List (ลิส)
ผลลัพธ์การรันโค้ด
ศึกษาจาก : https://www.youtube.com/watch?v=pZ788u6 ... lzdKrpxsMM
Code: Select all
Country_list = ['Thai','English','France','Geramany','America']
Year_list = [2000,2001,2002,2003,2004]
print (Country_list)
print (Country_list[0:4])
print (Country_list[-1:])
print (Year_list)
print (Year_list[1:3])
print (Year_list[2:])
It’s never too late to start again.
- chatee supasand
- PHP VIP Members
- Posts: 1666
- Joined: 04/06/2019 10:06 am
Re: ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
ลองเขียนความรู้ที่ได้จากวีดีโอ สอนไพทอน บทที่ 6 ทำความรู้จัก ตัวแปรชนิด List การใช้งานตัวแปลประเภทลิส
ศึกษาจาก https://www.youtube.com/watch?v=pZ788u6 ... MM&index=6
แสดง ชื่อทั้งหมด เพศ และ อายุ
แสดงชื่อคนที่ 2-3
แสดงชื่อคนที่1
แสดงเพศ หญิง
แสดงอยุ 23 เพราะนับมาจากได้หลัง -3
ศึกษาจาก https://www.youtube.com/watch?v=pZ788u6 ... MM&index=6
Code: Select all
name = ['bell','jame','boy','num','fon']
sex = ['male' , 'female']
age = [20,21,22,23,24,25]
print name + sex + age
print name[1:3]
print name[0]
print sex[1]
print age[-3]
แสดงชื่อคนที่ 2-3
แสดงชื่อคนที่1
แสดงเพศ หญิง
แสดงอยุ 23 เพราะนับมาจากได้หลัง -3

- jirawoot
- PHP VIP Members
- Posts: 3130
- Joined: 17/06/2019 10:30 am
Re: ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
Code: Select all
list1 =['python', 'helloworld', 2013,2024]
list2=[1,2,3,4,5]
print(list1)
print(list2)
Code: Select all
list1 =['python', 'helloworld', 2013,2024]
list2=[1,2,3,4,5]
print(list1[0])
print(list1[1:3])
print(list2[2:])
print(list1[-2])
Code: Select all
var_list = ['python', 'hello', 2013, 2014]
print(var_list[1])
var_list[1]='update'
print(var_list[1])
Code: Select all
var_list = ['python', 'hello', 2013, 2014]
print(var_list[1])
del var_list[1]
print(var_list[1])
Code: Select all
list1 =['python', 'helloworld', 2013,2024]
list2=[1,2,3,4,5]
print(len(list1))
print(len(list2))
print(list1+list2)
print(list1*2)
print("python" in list1)
print('php' in list1)
for x in list1:
print(x)
-
- Similar Topics
- Replies
- Views
- Last post
-
-
ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
by dawthana » 28/12/2016 3:38 pm » in Python Knowledge - 7 Replies
- 724 Views
-
Last post by jirawoot
19/06/2019 3:02 pm
-
-
-
ทำความรู้จักกับภาษา python (8) : ตัวแปรชนิด Dictionary (ดิกชันนารี)
by dawthana » 28/12/2016 5:24 pm » in Python Knowledge - 9 Replies
- 1261 Views
-
Last post by jirawoot
19/06/2019 3:18 pm
-
-
- 1 Replies
- 385 Views
-
Last post by md040
30/12/2016 10:17 am
-
- 1 Replies
- 2582 Views
-
Last post by mindphp
25/09/2019 9:09 pm
-
-
ทำความรู้จักกับภาษา python (23) : การใช้ from...import (ฟอร์ม...อิมพอร์ต) ในภาษา python (ไพทอน)
by dawthana » 12/01/2017 3:24 pm » in Python Knowledge - 9 Replies
- 2594 Views
-
Last post by jirawoot
21/06/2019 10:07 am
-
Who is online
Users browsing this forum: No registered users and 8 guests