Professional Documents
Culture Documents
เอกสารประกอบการสอน 4 tuple และ set
เอกสารประกอบการสอน 4 tuple และ set
Strings str sequence of charaters or sentences: 'A', 'hello world', "Bangkok KMUTT"
Lists list Order sequence of anything: [10, 'Non, str ,float, function]
Tuples tup immutable sequence of anything (10, 'Non, str ,float, function)
Sets set Unordered non-repeatable set {10, 'Non, str ,float, function}
ตัวแปรประเภท Tuple
คือตัวแปรแบบเรียงลำดับที่ไม่สามารถเปลี่ยนรูปหรือแก้ไขค่า
ของข้อมูลได้ (immutable and unchangeable)
In [1]: mytuple = ('A', "B", 1.0)
In [2]: type(mytuple)
tuple
Out[2]:
ตรวจสอบขนาดของข้อมูลหรือความยาวของ tuple
In [3]: len(mytuple)
3
Out[3]:
In [5]: print(len(Tuple1))
print(len(Tuple2))
In [ ]: type(Tuple1)
print(List)
---------------------------------------------------------------------------
<ipython-input-7-657a680fb7bf> in <module>()
In [9]: print(Tuple1[2])
print(Tuple1[2:])
print(Tuple2[:3])
print(Tuple2[4:])
Cat
('Cat', 'Dog')
ตัวแปรประเภท Set
ชุดของข้อมูลแบบไม่เรียงลำดับ ไม่สามารถแก้ไขค่าได้ และไม่มีดัชนีในการเรียกข้อมูล (unordered,
unchangeable, and unindexed).
print(Set1)
print(len(Set1))
{1, 2, 3, 4, 5, 6}
print(Set2)
print(len(Set2))
In [12]: Set1[0]
---------------------------------------------------------------------------
<ipython-input-12-25c87e89cfea> in <module>()
----> 1 Set1[0]
---------------------------------------------------------------------------
<ipython-input-14-10c0d018f856> in <module>()