Tuesday, June 12, 2018

Tuple in Python



What is Tuple?


Tuple is an immutable list of value .

Tuple is Sequence data type .

It’s separated by commas.


Enclosed within parenthesis.

Tuple cannot updated value remain constant you cannot modified it. 

Tuple are used to write-protect data.


Examples:



Create a Tuple:


myTuple = (‘Sam’, ‘Bob’, ‘Cherry’, ‘john’)

print(myTuple) #Output: (‘Sam’, ‘Bob’, ‘Cherry’, ‘john’)



Access the Tuple Items:


myTuple = (‘Sam’, ‘Bob’, ‘Cherry’, ‘john’)

print(myTuple[0]) #Output: Sam 


In-Built Function: tuple()


Use to cnstructor the tuple

myTuple = tuple((‘Sam’, ‘Bob’, ‘Cherry’, ‘john’))

print(myTuple) #Output: (‘Sam’, ‘Bob’, ‘Cherry’, ‘john’) 


In-Built Funtcion: len()


To get length of tuple

myTuple = (‘Sam’, ‘Bob’, ‘Cherry’, ‘john’)

print(len(myTuple)) # Output 4



Don’t Do with Tuple:


myTuple = (‘Sam’, ‘Bob’, ‘Cherry’, ‘john’)

myTuple[1] = “Archu” # Output: ERROR!!!!!!!!!!

Later We learn Advance in Tuple





Prev: Lists In Python

Next Sets in Python

No comments:

Post a Comment

Google+ Followers