Ticker

6/recent/ticker-posts

Python tuples

 Python tuples

Python tuples are one of the four inbuilt data types which are used to store collections of data. Other types are List, Set, and Dictionaries. 

Tuple is ordered and unchangeable. Tuple is like the list but unlike the list tuple is unchangeable. Unchangeable means after creating tuples elements cannot be added or removed.

Creating tuples:

Tuples are created by placing the datas inside a round bracket separated by commas. Datas can be of integers, strings or mixed data types. Nested tuple can be created by placing many lists inside, separated by commas.

Creating tuples in python


Accessing elements in the tuples:

Since tuples are ordered we can access the elements by indexing. Either by positive indexing or negative indexing.

Accessing elements in the tuples in python


 Slicing elements in the tuples:

Elements can be sliced in tuple by specifying starting and ending index. It can be sliced either by using positive index or negative index.

 

Slicing elements in tuples in python

Changing the elements in the tuples:

Tuples are unchangeable, but if the elements like list in the tuples can be changed by specifying the location by indexing.

The elements cannot be removed are deleted but the whole tuple can be reassigned.

 

Changing the elements in tuples in python

Inbuilt Python tuple functions:

count( )- Counts the number of ocuurence of the specified element.

index( )- Returns the index of the specified element.

del( )- Deletes the whole tuple. 


Post a Comment

0 Comments