Python Tuple
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/* ............... START ............... */ tupleExample = ("Mathew", "John", "James") print(tupleExample) print(len(tupleExample)) /* ('Mathew', 'John', 'James') 3 */ tuple1 = ("apple", "banana", "cherry") tuple2 = (1, 5, 7, 9, 3) tuple3 = (True, False, False) print(tuple1) print(tuple2) print(tuple3) /* ('apple', 'banana', 'cherry') (1, 5, 7, 9, 3) (True, False, False) */ tuple1 = ("abc", 34, True, 40, "male") print(tuple1) /* ('abc', 34, True, 40, 'male') */ /* ............... END ............... */ |
Python Tuples are used to store multiple items in single variable. In above program tupleExample is holding three string items.
Tuples is built in data type in pyton to store collections of data. A tuple is a collection which is ordered and unchangeable.
Python tuples are written in round backeets (eg : tuple1 = (“apple”, “banana”, “cherry”)).
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
Tuple is with order and that cannnot be change.
Tuples are unchangeable, so that we cannot change, add or remove items after the tuple has been created.
Python tuple can store duplicate values, so there can have same value items.
len() function python will the size of tuple. First program shows how to find the legnth of tuple.
Tuple can store the items of different data types (eg: tuple1 = (“abc”, 34, True, 40, “male”)), in this you can see that it is storing string, boolean and numbers.