Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การอัปเดต Tuple (Updating Tuples) ใน Python

Tuples เป็น immutable หมายความว่าคุณไม่สามารถอัปเดตหรือเปลี่ยนค่าของสมาชิก tuple ได้ คุณสามารถนำส่วนของ tuples ที่มีอยู่มาสร้าง tuples ใหม่ได้ ตามตัวอย่างต่อไปนี้:

ตัวอย่าง

#!/usr/bin/python
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
# Following action is not valid for tuples
# tup1[0] = 100;
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;

ผลลัพธ์ (Output)

เมื่อรันโค้ดข้างต้น จะได้ผลลัพธ์ดังนี้:

(12, 34.56, 'abc', 'xyz')