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

การดำเนินการ Tuple พื้นฐาน (Basic Tuples Operations) ใน Python

Tuples ตอบสนองต่อตัวดำเนินการ + และ * คลายกับ strings พวกมันหมายถึง concatenation และ repetition ที่นี่ด้วย ยกเว้นว่าผลลัพธ์คือ tuple ใหม่ ไม่ใช่ string

ในความเป็นจริง tuples ตอบสนองต่อทุก general sequence operations ที่เราใช้กับ strings ในบทก่อนหน้า:

Python Expression Results Description
len((1, 2, 3)) 3 Length
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition
3 in (1, 2, 3) True Membership
for x in (1, 2, 3): print x, 1 2 3 Iteration