Engineering Python · Data & Control
Tuples and Sets
In Engineering Python because some sequences must not change and some collections only care about unique membership — tuple and set.
A tuple is an ordered sequence that you cannot assign into. A set holds unique items and answers `in` quickly, with no index. Concatenating two tuples is just `+` — useful in a lab, not the whole idea.
- Engineering Python
- Easy level
- 4 concepts
1Tuples are ordered and immutable
Parentheses build a tuple: `(10, 20, 30)`. Indexing works the same as a list. Assigning `t[0] = 1` raises TypeError — that is the point of a tuple.
Use a tuple when the group is a fixed record (name, marks) that later code should not rewrite in place.
No diagram — the idea is carried by the prose, code block or coding lab.
| Kind | Order | Change in place | Index |
|---|---|---|---|
| list | yes | yes | yes |
| tuple | yes | no | yes |
| set | no | add/remove | no |
Fixed record
row = ("Asha", 86)
print(row[0], row[1])What happens if you run `t[0] = 1` on `t = (2, 3)`?
- TypeError — tuples do not support item assignment
- t becomes (1, 3)
- t becomes [1, 3]
Immutability is the difference from a list.
2Concatenating tuples
`+` joins two tuples into a new tuple. The originals stay unchanged. A lab that says "concatenate member tuples" wants this, not a rewrite of either input.
Figure. + joins two tuples into a new tuple. left + right is ("Patel", "Riya", "Mehta", "Arun"). left afterwards is still ("Patel", "Riya") — + does not rewrite either input.
Join two records
left = ("Patel", "Riya")
right = ("Mehta", "Arun")
print(left + right)Join two member tuples
left is ("Patel", "Riya") and right is ("Mehta", "Arun"). What is left + right, and what is left afterwards?
- left + right("Patel", "Riya", "Mehta", "Arun")
- left after +("Patel", "Riya") — unchanged
Pro tip. + builds a new tuple. It does not rewrite either input.
3Sets store unique membership
Curly braces with values (not key:value pairs) build a set: `{1, 2, 2}` is `{1, 2}`. Duplicates disappear.
`x in s` asks membership. There is no `s[0]` — sets are not sequences. `|` is union; `&` is intersection.
Figure. {2, 4, 2, 6} is {2, 4, 6} — the second 2 disappears. 4 in seen is True. {2, 4} | {4, 8} is {2, 4, 8}; union keeps 4 once. There is no s[0] — sets are not sequences.
Unique then combine
seen = {2, 4, 2, 6}
print(seen)
print(4 in seen)
print({2, 4} | {4, 8})Drop the duplicate, then union
seen starts as {2, 4, 2, 6}. What is seen, is 4 in it, and what is {2, 4} | {4, 8}?
- {2, 4, 2, 6}{2, 4, 6}
- 4 in seenTrue
- {2, 4} | {4, 8}{2, 4, 8}
Pro tip. The second 2 disappears. Union keeps 4 once.
4Lab: join tuples, test a set
Concatenate two member tuples and print a set membership test.
Figure. Concatenate ("Patel", "Riya") with ("Mehta", "Arun") — the joined tuple holds Riya. Then 4 in {2, 4, 6} is True. Tuple + builds a new tuple; a set answers in and drops duplicates.
Takeaway
- Hold thisConcatenate two member tuples and print a set membership test.
Coding lab. Members and a unique set runs in the app, with checks on your output.
Notes
- In Engineering Python because some sequences must not change and some collections only care about unique membership — tuple and set.
- Parentheses build a tuple: `(10, 20, 30)`. Indexing works the same as a list. Assigning `t[0] = 1` raises TypeError — that is the point of a tuple.
- `+` joins two tuples into a new tuple. The originals stay unchanged. A lab that says "concatenate member tuples" wants this, not a rewrite of either input.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
Tuples keep order and refuse item assignment; sets keep unique membership.
- Tuples are ordered and immutable
- Parentheses build a tuple: `(10, 20, 30)`. Indexing works the same as a list. Assigning `t[0] = 1` raises TypeError — that is the point of a tuple.
- Concatenating tuples
- `+` joins two tuples into a new tuple. The originals stay unchanged. A lab that says "concatenate member tuples" wants this, not a rewrite of either input.
- Sets store unique membership
- Curly braces with values (not key:value pairs) build a set: `{1, 2, 2}` is `{1, 2}`. Duplicates disappear.
- Lab: join tuples, test a set
- Concatenate two member tuples and print a set membership test.
Practise Tuples and Sets
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 1 quick check with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device