Python | Data Types | Tuple & It's Conversion By. Shivansh Sir

 PYTHON PROGRAMMING

PYTHON | TUPLE

Python के list data type में एक से ज्यादा items होते है | ये list data type के जैसे ही होता है | हर एक item को comma(,) से separate किया जाता है | Tuple के सभी items को parenthesis(()) के अन्दर close किये जाता है | Tuple एक compound data type है जिसमे किसी भी data types के items लिए जा सकते है | Python में 'Tuple' ये data structure होता है | Tuple ये Elements का sequence और immutable (unchangeable) होता है | Python के tuple में जो elements होते है उसे 'items' कहते है |

Python में List और Tuple एक जैसे ही होते है लेकिन List के items को change या उसके items delete किये जा सकते है और Tuple के items को change या उसके items delete नहीं किये जा सकते है | Tuples सिर्फ read किये जा सकते है |

Source Code

tuple = (1, "Hello", 5.6, (1, "Hii"))

for i in tuple:

    print(i)

tuple[0] = 3  #trying to changing 0th index

print(tuple[0])

Output :

1

Hello

5.6

(1, 'Hii')

Traceback (most recent call last):

    tuple[0] = 3  #trying to changing 0th index

TypeError: 'tuple' object does not support item assignment

PYTHON | TUPLE | CREATING TUPLE

Tuple के आखिरी में semi-colon (;) दे या ना दे इससे कोई फर्क नहीं पड़ता है |

tuple1 = (1, 2, 3, 4, 5) #Integer Tuple

tuple2 = (1.5, 2.4, 3.8, 4.4, 5.7); #Float Tuple

tuple3 = (1, 2, "Hello", 4.5); #Mixed Data Types Tuple

tuple4 = (1, 2, (1, 2), 4.5) #Nested Tuple

PYTHON | TUPLE | Parenthesis() 

Tuples बिना parenthesis के भी हो सकते है |

Source Code

tuple = "H", "e", "l", "l", "o"

print(type(tuple))

Output

<class 'tuple'>

PYTHON | TUPLE | POSITIVE INDEXING

Programming Language में index की शुरुआत '0' से होती है उसी तरह से Python में Tuple के पहले item का index '0' होता है |

strTuple = ("H", "e", "l", "l", "o")

PYTHON | TUPLE | NEGATIVE INDEXING

Python में Tuple के आखिरी index '-1' से शुरू होता है |

strTuple = ("H", "e", "l", "l", "o")

PYTHON | TUPLE | ACCESSING TUPLE

Index से Tuple के items को access किया जा सकता है |

Syntax

tuple(index)

Source Code

tuple = (1, 2, "H", 2.8)

print(tuple[0])

print(tuple[1])

print(tuple[2])

print(tuple[3])

Output

1

2

H

2.8

PYTHON | TUPLE | INVALID INDEXING

Invalid index दिया जाता है तो 'indexError' का exception आ जाता है |

Source Code 

tuple = (1, 2, "H", 2.8)

print(tuple[10])

Output :

    print(tuple(10))

IndexError: tuple index out of range

PYTHON | TUPLE | ACCESSING NESTED TUPLE

Source Code

tuple = (1, 2, "Hello", ("R", "U"))

print(tuple[3][0]) 

Output

R

print(tuple[3][1])

Output

U

PYTHON | TUPLE | CAN'T CHANGE BUT REASSIGN

Tuple को change नहीं कर सकते लेकिन उसे re-assign किया जा सकता है |

Source Code

tuple = (1, 2, 3, 4, 5)

print(tuple)

Output

(1, 2, 3, 4, 5)

tuple = (6, 7, 8, 9, 10)

print(tuple)

Output

(6, 7, 8, 9, 10)

PYTHON | TUPLE | DELETING

'del' Operator से पूरा tuple delete किया जा सकता है लेकिन tuple के एक-एक item को delete नहीं किया जा सकता |

Source Code

tuple = (1, 2, 3, 4, 5)

del tuple[0]

Output

del tuple[0]

#TypeError: 'tuple' object doesn't support item deletion

tuple = (1, 2, 3, 4, 5)

del tuple

print(tuple)

Output

<class 'tuple'>

PYTHON | TUPLE | Convert Tuple to Sub-Tuple or Tuple Slicing

Colon(:) के left में sub-Tuple कहा से start करना है और colon(:) के right में कहा पर end करना है वो दिया जाता है | Slicing के लिए colon को 'slicing operator' कहा जाता है |

Syntax

tuple(start:end)

Source Code 

tuple = (1, 2, "H", 2.8)

print(tuple[0:1]) 

print(tuple[0:2])

print(tuple[1:3])

print(tuple[3:1])

Output

(1)

(1, 2)

(2, 'H', 2.8)

()

Note :- अगर colon(:) के left side का index invalid होता है तो blank tuple(()) return होती है और colon(:) के right side का index invalid होता है तो Tuple के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank tuple(()) return होता है |

Example

tuple = (1, 2, "H", 2.8)

print(tuple[0:1]) #Output (1)

print(tuple[0:2]) #Output (1, 2)

print(tuple[1:3]) #Output (2, 'H')

print(tuple[1:15]) #Output (2, 'H', 2.8)

print(tuple[10:4]) #Output ()

PYTHON | TUPLE | Check Length of Tuple's Items

Source Code

tuple = (1, 2, "H", 2.8)

print (len(tuple))

Output

4

PYTHON | TUPLE | CONCATENATION

Source Code

tuple = (1, 2, "H", 2.8) + ("Hello", (2, 8))

print (tuple)

Output

(1, 2, 'H', 2.8, 'Hello', (2, 8))

PYTHON | TUPLE | REPETITION

Source Code

tuple = (1, 2, "H", 2.8) * 3

print (tuple)

Output

(1, 2, 'H', 2.8, 1, 2, 'H', 2.8, 1, 2, 'H', 2.8)

PYTHON | TUPLE | Iterating Tuple Items using For Loop

Source Code

tuple = (1, 2, "H", 2.8)

for i in tuple:

    print(i)

Output :

1

2

H

2.8

PYTHON | TUPLE | TUPLE FUNCTION 

1. all() :- Sequence के सभी items True होते है तो ये तो ये True return करता है |

2. any() :- Sequence का एक या सभी items True होते है तो ये तो ये True return करता है |

3. enumerate() :- Start से index और उसकी value की pair return करता है |

4. len() :- Tuple की length; number में return करता है |

5. max() :- Tuple से max value को return करता है |

6. min() :- Tuple से min value को return करता है |

7. sorted() :- Sequence को sort करके return करता है |

8. sum() :- Sequence के items को add करके उनका sum return करता है |

9. tuple() :- Sequence को tuple में convert करता है |


For Any Doubt Clear on Telegram Discussion Group

Join Us On Social Media
For Any Query WhatsApp Now

Comments

Popular posts from this blog

Python | All in One Program By. Shivansh Sir

Python | Data Types | Number & it's Conversion By. Shivansh Sir

Python | Loop | For Loop By. Shivansh Sir