Introduction To “Tuple”

Oindrila Chakraborty
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 a “Python Tuple” of “Integers”
Create a “Python Tuple” of “Strings”

“Create” the “Tuple” Without Using “Parentheses”

  • In many cases, the “Parentheses” of a “Literal Tuple” may be “Omitted”.
Create a “Python Tuple” of “Integers” Without the “Parentheses”
  • The “Feature” of “Not Using” the “Parentheses” is often used when “Returning Multiple Values” from a “Function”.
“Create” and “Call” a “Function” to “Return” the “Max” and “Min” Values of a “Sequence”

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 a “Heterogeneous Python Tuple”

Create an “Empty Tuple”

  • It is possible to “Create” an “Empty Tuple” by using an “Empty Parentheses”.
Create an “Empty Python Tuple”

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 “Python Tuple” From a “String”

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”.
This is “Not” the “Right Way” to “Create” a “Tuple” with “Single Element”

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”.

The “Right Way” to “Create” a “Tuple” with “Single Element”

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”.
“Create” and “Call” a “Function” to “Return” the “Max” and “Min” Values of a “Sequence”

“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.
“Retrieve” the “Third Element” of a “Literal Tuple”

If the “Tuple Index” of the “Element” to be “Retrieved” from a “Tuple” is “Not Present” in the “Tuple”, the “IndexError Exception” is “Thrown”.

“Retrieve” the “Fifteenth Element”, Present at “Index = 14”, of a “Literal Tuple”

“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 ()”.
“Count” the “Number of Elements” of a “Python Tuple”

“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”.
“Iterate Over” the “Elements” of a “Python Tuple” Using the “for Loop”

“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”.
“Concatenate” “Multiple Tuples” Using the “Plus Operator”, i.e., “+”

“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”.
“Initialize” a “Tuple” of “Multiple Elements” Using “Multiplication Operator”
“Initialize” a “Tuple” of “Single Element” Using “Multiplication Operator”

“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.
“Find” the “First Occurrence” of an “Element” in a “Tuple” Using the “index ()” Method
  • If an “Element” is “Searched” that “Does Not Exist” in the “Tuple” using the “index ()” Method, the “ValueError Exception” is “Thrown”.
“Find” an “Element” That “Does Not Exist” in a “Tuple” Using the “index ()” Method — “ValueError”

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”.
“Find” the “Number of Times” an “Element” is “Present” in the “Tuple” Using the “count ()” Method
  • If an “Element” is “Searched” that “Does Not Exist” in the “Tuple” using the “count ()” Method, “No Exception” is “Thrown”. Instead “0” is “Returned”.
“Find” the “Number of Times” an “Element” is “Present” in the “Tuple”, which “Does Not Exist” in the “Tuple”, Using the “count ()” Method

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”.
“Verify” If an “Element” is “Present” in a “Tuple” Using the “IN Operator”

“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”.
“Verify” If an “Element” is “Not Present” in a “Tuple” Using the “NOT IN Operator”

--

--

Oindrila Chakraborty

I have 11+ experience in IT industry. I love to learn about the data and work with data. I am happy to share my knowledge with all. Hope this will be of help.