Python for beginners - Python Tuples

 Tuple

Tuple is one of the inbuilt datatypes in python for storing data. The data is stored in ordered way and it cannot be changeable.

The syntax for creating tuple is

Tuplename=(“data1”,”data2”,”data3”)

Example is given below.

mytuple=(“carrot”,”beetroot”,”beans”)

print(mytuple)

While executing this code,the output is look like below.

('carrot', 'beetroot', 'beans')

Features:

Tuple has some features.

Tuples are indexed. The index value starts from 0 to n values.

Once created, it cannot be changed.

Sometimes, same values can be duplicated.

 

Next,the datatypes like int,Boolean and strings are given below.

Da.py

#This is a tuple which contains integer values.

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

 #This is a tuple which contains Boolean values.

Botuple=(True,False,True)

#This is a tuple which contains string values.

Strtuple=(“happy”,”days”,”today”)

print(Nutuple)

print(Botuple)

print(Strtuple)

while executing this program, the user get the output.



The combination of all data types in a tuple is defined as follows.

mytuple=(“Jeeva”,39,”Maruthi nagar”,”Chennai”)

print(mytuple)

while executing this, you get this output.


Usage of constructor in tuple:

Constructor is used to create a object for a tuple.

Eg:

Mytuple=tuple((“carrot”,”beetroot”,”Beans”)

Print(mytuple)

 The operations on tuple is finding length of a tuple. The coding is given below.



 These are ways of creating a tuple and its operations are explained.

No comments:

Post a Comment