ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
Moderators: mindphp, ผู้ดูแลกระดาน
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
ตัวแปรชนิด Tuple(ทูเปิล)
ตัวแปรชนิด Tuple(ทูเปิล) สามารถเก็บค่าได้มากกว่า 1 ค่าใน 1 ตัวแปร เหมือนตัวแปรชนิด List (ลิส) แต่แตกต่างกันตรงที่ Tuple ไม่สามารถเพิ่มค่าได้
การกำหนดตัวแปร Tuple
- การกำหนดค่าให้กับตัวแปรชนิด Tuple ต้องอยู่ภายใต้เครื่องหมาย (...) การเพิ่มค่าให้กับตัวแปร Tuple
-ตัวแปร Tuple ไม่สามารถเพิ่มหรือเปลี่ยนค่าได้ ถ้าเพิ่มหรือเปลี่ยนค่าจะเกิด Error (แอร์เรอร์) ขึ้นว่า Error : " 'Tuple' object does not support item assignment"
การลบค่าตัวแปร Tuple
- ตัวแปร Tuple ไม่สามารถลบค่าได้ ถ้าลบค่าจะเกิด Error ขึ้นว่า Error : "'Tuple' object does not support item delection"
ตัวดำเนินการของตัวทูเปิล
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=kF0Ujfh ... MM&index=7
ตัวแปรชนิด Tuple(ทูเปิล) สามารถเก็บค่าได้มากกว่า 1 ค่าใน 1 ตัวแปร เหมือนตัวแปรชนิด List (ลิส) แต่แตกต่างกันตรงที่ Tuple ไม่สามารถเพิ่มค่าได้
การกำหนดตัวแปร Tuple
- การกำหนดค่าให้กับตัวแปรชนิด Tuple ต้องอยู่ภายใต้เครื่องหมาย (...) การเพิ่มค่าให้กับตัวแปร Tuple
-ตัวแปร Tuple ไม่สามารถเพิ่มหรือเปลี่ยนค่าได้ ถ้าเพิ่มหรือเปลี่ยนค่าจะเกิด Error (แอร์เรอร์) ขึ้นว่า Error : " 'Tuple' object does not support item assignment"
การลบค่าตัวแปร Tuple
- ตัวแปร Tuple ไม่สามารถลบค่าได้ ถ้าลบค่าจะเกิด Error ขึ้นว่า Error : "'Tuple' object does not support item delection"
ตัวดำเนินการของตัวทูเปิล
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=kF0Ujfh ... MM&index=7
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
-
- PHP Sr. Member
- Posts: 84
- Joined: 01/02/2017 11:10 am
Re: ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
เข้าใจเรื่อง Tuple แล้วครับ ก็คือมันจะคล้ายๆ กับList แต่ว่าTuple จะใช้สัญลักษณ์ "()" และข้อสำคัญของTuple คือ ไม่สามารถเพิ่มหรือลบข้อมูลได้
- Jom07
- PHP Super Hero Member
- Posts: 514
- Joined: 08/01/2018 9:56 am
Re: ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
ตัวอย่าง การกำหนดตัวแปร tuple
ผลรัน

ศึกษาจาก :
https://www.youtube.com/watch?v=kF0Ujfh ... lzdKrpxsMM
Code: Select all
tuple1 = ["test", "test1", 2017, 2018]
tuple2 = [1, 2, 3, 4, 5]
print (tuple1)
print (tuple2)
ศึกษาจาก :
https://www.youtube.com/watch?v=kF0Ujfh ... lzdKrpxsMM

-
- PHP Hero Member
- Posts: 114
- Joined: 09/04/2018 10:04 am
Re: ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
Code: Select all
tuple1 = ("Jame","Ann", "Game", "Tom")
tuple2 = (9, 8, 7, 6, 5)
print (tuple2[0])
print (tuple1[0:])
print (tuple2[-3])
แสดงผลของสมาชิกใน tuple1 ตั้งแต่ index ที่ 0 เป็นต้นไป
แสดงผลของสมาชิกใน tuple2 นับถอยหลังมาตำแหน่งที่ 3
ศึกษาจาก https://youtu.be/kF0UjfhdsdM
-
- PHP Hero Member
- Posts: 199
- Joined: 30/04/2018 9:44 am
Re: ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
ตัวแปรชนิด Tuple(ทูเปิล)
ผลการรันโค้ด
ศึกษาจาก : https://www.youtube.com/watch?v=kF0Ujfh ... MM&index=6
Code: Select all
ex_tuple1 = ('Mindphp','Forums',2012,2018)
ex_tuple2 = (11,22,33,44,55)
print (ex_tuple1[0:2])
print (ex_tuple1[1:])
print (ex_tuple2[1:3])
print (ex_tuple2[-2:])
ศึกษาจาก : https://www.youtube.com/watch?v=kF0Ujfh ... MM&index=6
It’s never too late to start again.
- chatee supasand
- PHP VIP Members
- Posts: 1666
- Joined: 04/06/2019 10:06 am
- jirawoot
- PHP VIP Members
- Posts: 3130
- Joined: 17/06/2019 10:30 am
Re: ทำความรู้จักกับภาษา python (7) : ตัวแปรชนิด Tuple(ทูเปิล)
Code: Select all
tuple1=("python", 'hello', 2013, 2014)
tuple2=(1, 2, 3, 4, 5)
print(tuple1)
print(tuple2)
Code: Select all
tuple1=("python", 'hello', 2013, 2014)
tuple2=(1, 2, 3, 4, 5)
print(tuple1)
print(tuple2)
tuple2[1]=5
print(tuple2[1])
-
- Similar Topics
- Replies
- Views
- Last post
-
- 0 Replies
- 497 Views
-
Last post by Patcharanan.0399
17/04/2018 10:50 am
-
- 1 Replies
- 328 Views
-
Last post by md040
30/12/2016 10:18 am
-
-
ทำความรู้จักกับภาษา python (6) : ตัวแปรชนิด List (ลิส)
by dawthana » 28/12/2016 2:25 pm » in Python Knowledge - 7 Replies
- 803 Views
-
Last post by jirawoot
19/06/2019 2:43 pm
-
-
-
ทำความรู้จักกับภาษา python (8) : ตัวแปรชนิด Dictionary (ดิกชันนารี)
by dawthana » 28/12/2016 5:24 pm » in Python Knowledge - 9 Replies
- 1240 Views
-
Last post by jirawoot
19/06/2019 3:18 pm
-
-
- 0 Replies
- 299 Views
-
Last post by Four
18/01/2018 5:18 pm
Who is online
Users browsing this forum: No registered users and 13 guests