Tuple Data Type in 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 ListSet, and Dictionary, all with different qualities and usage.

A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets. Tuple items are ordered, unchangeable, and allow duplicate values.

Tuple items are indexed, the first item has index [0], the second item has index [1] etc.

When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. Since tuples are indexed, they can have items with the same value:

Tuple का यूज multiple items को single variable मे स्टोर करने के लिए होता है। Python मे data के collection को स्टोर करने के लिए 4 तरह के data types का यूज होता है जिसमे tuple के अतिरिक्त List, Set ओर Dictionary होते हैं। सभी data type का यूज ओर quality अलग अलग हैं।

Tuple एक ordered unchangeable data type है ओर यह एक sequence मे काम करता है। यानि ये index number के साथ काम करता है ओर tuple को round brackets मे लिखा जाता है। यह एक immutable डाटा टाइप है, इसके अंडर की वैल्यू को बदला या हटाया नहीं किया जा सकता।

Example 1

Create a Tuple:

tup = ("apple""banana""orange")
print(tup)

Output:

('apple', 'banana', ‘orange’)

Example 2

Tuples allow duplicate values:

tup = ("apple""banana""orange""apple""orange")
print(tup)

Output:

('apple', 'banana', ‘orange’, 'apple', ‘orange’)

Tuple Length

To determine how many items a tuple has, use the len() function:

Example 3

Print the number of items in the tuple:

tup = ("apple""banana""orange")
print(len(tup))

Output:

3

Create Tuple with One Item

To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.

Example 4

One item tuple, remember the comma:

tup = ("apple",)
print(type(tup))

#NOT a tuple
tup = (
"apple")
print(type(tup))

Output:

<class 'tuple'>
<class 'str'>

Tuple Items - Data Types

Tuple items can be of any data type:

Example 5

String, int and Boolean data types:

tup1 = ("apple""banana""orange")
tup2 = (
15793)
tup3 = (
TrueFalseFalse)

print(tup1)

print(tup2)

print(tup3)

Output:

('apple', 'banana', 'cherry')

(1, 5, 7, 9, 3)

(True, False, False)

Example 6

A tuple can contain different data types: AA tuple with strings, integers and Boolean values:

tup = ("abc"34True40"male")

print(tup)

Output:

('abc', 34, True, 40, 'male')

From Python's perspective, tuples are defined as objects with the data type 'tuple':

Example 7

What is the data type of a tuple?

tup = ("apple""banana""cherry")
print(type(tup))

Output:

<class 'tuple'>

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

Example 8

Using the tuple() method to make a tuple:

tup = tuple(("apple""banana""orange")) # note the double round-brackets
print
(tup)

Output:

('apple', 'banana', 'cherry')

Function of Tuple:

·         Max()

·         Min()

·         Count()

·         Index()

·         Sum()

Max()

Its return maximum value of tuple

Example 9

tup1 = ("apple""banana""orange")
tup2 = (
15793)

a = max(tup1)

b = max(tup2)

print(a,b)

Or

tup1 = ("apple""banana""orange")
tup2 = (
15793)

print(max(tup1))

print(max(tup2))

Output:

Orange 9

Min()

Its return minimum value of tuple

Example 10

tup1 = ("apple""banana""orange")
tup2 = (
15793)

print(min(tup1))

print(min(tup2))

Output:

apple 1

count()

Its returns numbers of total repeated value in a tuple

Example 11

tup = ("apple""banana""cherry", "banana""orange")
a = tup.count(“banana”)

print(a)

Output:

2

index()

Its returns index number of tuple values:

Example 12

tup = ("apple""banana""cherry", "orange",)
a = tup.index(“banana”)

print(a)

Output:

1

sum()

Its returns the sum of total integer numbers in a tuple

Example 13

tup = (5,10,15,20,25,30)
a = sum(tup)

print(a)

Output:

105

Example 14

tup = (5,10,15,20,25,30)
a = sum(tup,25)

print(a)

Output:

130

How to Iterate tuple value

Example 15

Iterate single value

tup = (5,10,15,20,25,30)
a = tup[3]

print(a)

Output:

20

Example 16

Iterate single value

tup = (5,10,15,20,25,30)
print(tup[3])

Output:

20

Example 17

Iterate multiple value using for loop

tup = (5,10,15,20,25,30)
for a in tup:

print(a)

Output:

5

10

15

20

25

30

 There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered** and changeable. No duplicate members.

*Set items are unchangeable, but you can remove and/or add items whenever you like.

**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.

<<Previous                                                  Next>>

कोई टिप्पणी नहीं

टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click Download VBA Code Notepad file - Click here D...

Blogger द्वारा संचालित.