WEB DEVELOPER SITE
PHP7
 

Python -Triples


Triples are generally sequences list.Triple can't change list

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Tuple

Example 1

thistuple = ("Vegetables ", "Fruit", "Lean ")
print(thistuple)
Result Answer: ('Vegetable', 'Fruit', 'Lean')

Example 2


thistuple = ("Vegetables ", "Fruit", "Lean ")

print(thistuple[2])
Result Answer: ('Fruit')

Example 3

thistuple = ("Vegetables ", "Fruit", "Lean ")
thistuple[1] = "Milk" # test changeability


Result Answer: ('Vegetables', 'Fruit', 'Lean')

Indexing, Slicing, and Matrixes

L = ('welookups', 'Welookups', 'WELOOKUPS!')
Python expression Description Example
L[2] offset is start with zero 'WELOOKUPS!'
L[-2] Negative
L[1:] Slicing fetches sections ['Welookups', 'WELOOKUPS!']

tuple() Constructor

len() method returns the number of items

It is also possible to use the tuple() constructor to make a tuple.

Example

Using the tuple() method to make a tuple:
thistuple = tuple(("Vegetables ", "Fruit", "Milk"))
print(len(thistuple))
Run Example »

Built-in Tuple Functions

Python includes the following tuple functions −

Sr.No. Function with Description
1 cmp(tuple1, tuple2)

Compares elements of both tuples.

2 len(tuple)

Gives the total length of the tuple.

3 max(tuple)

Returns item from the tuple with max value.

4 min(tuple)

Returns item from the tuple with min value.

5 tuple(seq)

Converts a list into tuple.