Introduction To “Tuple”
6 min readAug 11, 2023
What is a “Tuple”?
- “Python Tuples” are the “Immutable Sequences of Arbitrary Objects”.
- Once “Created”, the “Elements” within the “Python Tuples” “Cannot” be “Replaced” or “Removed”, and, “New Elements” “Cannot” be “Added” to the “Python Tuples”.
How “Python Tuples” are “Created”?
- “Python Tuples” are “Delimited” by “Parentheses”, i.e., “()”.
- The “Items” within the “Tuples” are “Separated” by “Commas”, i.e., “,”.
“Create” the “Tuple” Without Using “Parentheses”
- In many cases, the “Parentheses” of a “Literal Tuple” may be “Omitted”.
- The “Feature” of “Not Using” the “Parentheses” is often used when “Returning Multiple Values” from a “Function”.
Create a “Heterogeneous Tuple”
- “Python Tuples” can be “Heterogeneous”. That means “Each” of the “Elements” inside a “Python Tuple” can be of “Different Data Types”.
Create an “Empty Tuple”
- It is possible to “Create” an “Empty Tuple” by using an “Empty Parentheses”.
Create a “Tuple” From “Other Collection Data Types”
- “Python Tuples” can be “Created” from “Other Collection Data Types”, such as — “Strings”, using the “tuple () Constructor”.
Create a “Tuple” With “Single Element”
- It is “Not Possible” to “Create” a “Tuple” with “Single Element” by providing a “Simple Number” in “Parentheses”.
- This is because “Python” “Parses” a “Simple Number” in “Parentheses” as an “Integer” that is “Enclosed” in the “Precedence Controlling Parentheses” of a “Math Expression”.
To “Create” a “Tuple” with “Single Element”, the “Trailing Comma Separator”, i.e., “,” is used.
A “Single Element” with a “Trailing Comma” is “Parsed” as a “Tuple” with “Single Element” in “Python”.
Tuple Unpacking
- “Returning Multiple Values” as a “Tuple” is often used in conjunction with a wonderful “Feature” of “Python”, called “Tuple Unpacking”.
- “Tuple Unpacking” is a “De-Structuring Operation”, which allows to “Unpack” the “Data Structures” into the “Named References”.
“Retrieve Elements” From a “Tuple”
- It is possible to “Retrieve” the “Elements” of a “Tuple” by using the “Square Brackets”, i.e., “[]”, with a “Zero-Based Index”.
- Example — To “Retrieve” the “First Element” of a “Tuple”, “[0]” should be used, to “Retrieve” the “Second Element” of a “Tuple”, “[1]” should be used, and so on.
If the “Tuple Index” of the “Element” to be “Retrieved” from a “Tuple” is “Not Present” in the “Tuple”, the “IndexError Exception” is “Thrown”.
“Count” the “Number of Elements” of a “Python Tuple”
- It is possible to “Determine” the “Number of Elements” in a “Tuple” using the “Python Built-In Function”, i.e., “len ()”.
“Iterating Over” the “Elements” of “Python Tuple”
- Since “Python Tuples” are “Iterables”, the “for Loop” can be used to “Iterate Over” the “Elements” of “Python Tuples”.
“Concatenate” the “Tuples”
- There are “Multiple Techniques” available to “Concatenate” the “Tuples”.
- All of the “Techniques” will work with “Any Iterable Series” on the “Right-Hand Side”.
“Concatenation” of the “Tuples” Using the “Plus Operator”
- It is possible to “Concatenate” the “Tuples” using the “Plus Operator”, i.e., “+”. This “Results” in a “New Tuple”, “Without Modification” of the “Tuples” that are “Concatenated”.
“Usage” of “Multiplication Operator” on “Tuple”
- As with “Strings” and “Lists”, the “Tuples” support “Repetition” using the “Multiplication Operator”.
- The “Multiplication Operator” is often used for “Initializing” a “Tuple” of a “Size, Known in Advance” with a “Constant Value” for the “Repetition”.
“Search” the “Elements” in a “Tuple”
- There are “Multiple Methods” available to “Find” the “Elements” in a “Tuple”.
1. “Search” the “Elements” in a “Tuple” Using “index ()” Method
- The “index ()” Method “Takes” the “Element” to be “Searched” as the “Argument”.
- The “Elements” present in “Each Index” of the “Tuple” are “Compared” with the “Element” to be “Searched” for “Equivalence” until the “Element” to be “Searched” is “Found”.
- The “index ()” Method “Returns” the “Index” of the “First Tuple Element”, which is “Equal” to the “Argument” sent to the “index ()” Method.
- If an “Element” is “Searched” that “Does Not Exist” in the “Tuple” using the “index ()” Method, the “ValueError Exception” is “Thrown”.
2. “Count” the “Number of Times” an “Element” “Appears” in a “Tuple” Using “count ()” Method
- The “count ()” Method “Takes” the “Element” to be “Searched” as the “Argument”.
- The “count ()” Method “Returns” the “Number of Times” the “Argument”, sent to the “count ()” Method, has “Appeared” in the “Tuple”.
- If an “Element” is “Searched” that “Does Not Exist” in the “Tuple” using the “count ()” Method, “No Exception” is “Thrown”. Instead “0” is “Returned”.
3. “Membership Verification” of the “Elements” in a “Tuple”
- “Membership” is a “Fundamental Operation” for “Tuples”.
- As with “Other Collection Data Types”, the “Membership Operation” is “Performed” using the “IN” and “NOT IN” Operators on “Tuples”.
A. “Membership Verification” of the “Elements” in a “Tuple” Using “IN Operator”
- To “Verify” that “If” an “Element” is “Present” in a “Tuple” using “Membership”, then “IN Operator” is used.
- If the “Element” is “Present” in the “Tuple”, “True” is “Returned”.
- If the “Element” is “Not Present” in the “Tuple”, “False” is “Returned”.
“Membership Verification” of the “Elements” in a “Tuple” Using “NOT IN Operator”
- To “Verify” that “If” an “Element” is “Not Present” in a “Tuple” using “Membership”, then “NOT IN Operator” is used.
- If the “Element” is “Not Present” in the “Tuple”, “True” is “Returned”.
- If the “Element” is “Present” in the “Tuple”, “False” is “Returned”.