Friday, December 22, 2017

Tuples in Python

Tuples in Python

The data structure in python consist mainly of :- tuples ,list , dict and sets.Each of these types have some different behavior and suits some particular kind of data which we see in detail.The main concept which we need to take care while handling such data structure is the mutability and immutability. Mutable means that can be changed while immutable is just opposite of mutability.

#Tuples :- Tuples are immutable ordered sequence of elements and the individual element can be of any type.They can be represented by parenthesis and the element contained in the tuple can be of any type.

#How to initialize a tuple ?
#empty tuple
a =()

#Tuple with values.

a =(2,"san",6)

#Tuple with one value.

a= (5,)

Note :- The Tuple with single value should have a comma.

#2.) Accessing values in tuple.

#The value in tuple can be accessed via indexing and slicing. The value in the tuple starts from the zero index.

#Example :-

a =(2,"san",6,7,9)

print(a[2])

# The Output will be 6.

print(a[0])

# The Output will be 2.

print(a[2:4])

# The Output will be 6,7.

#3.) Nested Tuples.
#We can have a tuple inside a tuple but to access this is a tricky one.
nestedtuple = (2,3,(4,6,7))
print(nestedtuple[2])
# The output will be (4,6,7).

nestedtuple = (2,3,(4,6,(3,5),7))
print(nestedtuple[2][0])
#The output will be 4.

#Negative Indexing:
nestedtuple = (2,3,(4,6,(3,5),7))
print(nestedtuple[-2]#
The Output will be 3.

#Slicing a tuple:

a =(2,"san",6,7,9)
print(a[2:4])

#The output is 6,7

print(a[:3])

#The output is(2, 'san', 6).

print(a[:])

#The output is (2,"san",6,7,9)

print(a[:-1])

(2, 'san', 6, 7)

# The tuples are immutable but if the element inside tuple is mutable .it can be changed.

a =(2,"san",6,7,9)

a[2]= 4

#we get the below error;
#TypeError: 'tuple' object does not support item assignment.
#but in case of a list ,the result is different.

my_tuple = (4, 2, 3, [6, 5])


my_tuple[3][1]=9

print(my_tuple)


#The output is (4, 2, 3, [6, 9]).

#deleting a tuple.



my_tuple = (4, 2, 3, [6, 5])


del my_tuple

print(my_tuple)

# The output is NameError: name 'my_tuple' is not defined.


#concatenation of Tuples.


a =(2,3,4)

b =(4,5,6)

print(a + b)

# The output is (2, 3, 4, 4, 5, 6)

#  The count function to find the number of a particular element.
a =(2,3,4,4,5)

print(a.count(4))

# The output is 2.

# To find the index of any particular element.
a =(2,3,4,4,5)

print(a.index(3))

# The output is 1.
 

 

No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...