Tuples are immutable and typically contain a sequence of dissimilar elements that can be accessed through unboxing or indexing (or even by attribute in the case of named tuples). Lists are mutable, and their elements are usually uniform and accessible by iterating over the list.
In Python, tuples are created by placing a sequence of values, separated by commas, with or without parentheses to group the data sequence. Tuples can contain any number of elements and any data type (e.g. strings, integers, list, etc.). Tuples can also be created with a single element, but this is a bit tricky. Having one element in parentheses is not enough, there must be a comma to make it a tuple.
Note. Creating a Python tuple without using parentheses is called Tuple Packing.
Initial empty Tuple: () Tuple with the use of String: (`Geeks`,` For`) Tuple using List: (1, 2, 4, 5, 6) Tuple with a loop (`Geeks`,) ( (`Geeks`,),) (((` Geeks`,),),) ((((`Geeks`,),),),) (((((` Geeks`,),),), ),) Tuple with the use of function: (`G`,` e`, `e`,` k`, `s`) Tuple with Mixed Datatypes: (5,` Welcome`, 7, `Geeks`) Tuple with nested tuples: ((0, 1, 2, 3), (`python`,` geek`)) Tuple with repetition: (`Geeks`,` Geeks`, `Geeks`)
Tuple Concatenation — it is the process of combining two or more tuples. Concatenation is done using the "+" operator. Concatenation of tuples is always performed from the end of the original tuple. Other arithmetic operations do not apply to tuples.
Note. In a union, only the same data types can be combined; an error occurs when combining list and tuple.
Removal of First Element: (`E`,` E`, `K`,` S`, `F`,` O`, `R`,` G`, `E` , `E`,` K`, `S`) Tuple after sequence of Element is reversed: (` S`, `K`,` E`, `E`,` G`, `R`,` O`, `F`,` S`, `K`,` E`, `E`,` G`) Printing elements between Range 4-9: (`S`,` F`, `O`,` R`, ` G`)
Tuples are immutable and therefore do not allow part of it to be deleted. The entire tuple is removed using the del () method.
Note. Printing a tuple after deletion results in an error.
Traceback (most recent call last):
File “/ home / efa50fd0709dec0 8434191f32275928a.py ”, line 7, in
NameError: name `Tuple1` is not defined
|Built-in Function th >||Description|
|all ()||Returns true if all element are true or if tuple is empty|
|any ()||return true if any element of the tuple is true. if tuple is empty, return false|
|len ()||Returns length of the tuple or size of the tuple|
|enumerate ()||Returns enumerate object of tuple|
|max ()||return maximum element of given tuple|
|min()||return minimum element of given tuple|
|sum ()||Sums up the numbers in the tuple|
|sorted ()||input elements in the tuple and return a new sorted list|
|tuple ()||Convert an iterable to a tuple.|
< h3> Latest articles on Tuple