Python Tuple

Introduction

In this chapter, you will learn what Python tuple is and how is it useful?

Every programming language supports the basic data structure as a sequence and to store the data in sequence or store co-related data as a sequence.

Python supports six built-in sequences but most common are list and tuple.

Python also has a built in function for the manipulation and performing an operation on the tuple.

Python tuple

Tuple is a versatile data type of Python. The tuple can support any data type element in the same tuple.

Python tuple element is separated by comma(,).

Syntax of tuple-

          tuple_name=(tuple_element)

Example-

     tuple=(“c#corner”,1998)

Accessing value from tuple-

To access an individual value, the tuple must use square brackets with an index for slicing an individual value.

Example-
  1. tuple1 = ("ajay malik"229990600495)# tuple with the name number and ph number.  
  2. print("name= ", tuple1[0])# prints the first element form tuple  
  3. print("age=", tuple1[1])# prints the second element fot tuple  
  4. print("ph number", tuple1[2])# prints third element from tuple  
Output-
Updating tuple in python

We can’t update a tuple because tuple is immutable, which means tuple have a constant value.

Deleting tuple element

Deleting of an individual value from the tuple is impossible but we can delete whole tuple.

Example-
  1. tuple1 = ("ajay malik"229990600495)# tuple with the name number and ph number  
  2. print("tuple before delete operator:", tuple1)  
  3. del tuple1  
  4. print("tupes is not avilable:", tuple1)  
Output-
 
tuple operations

Python list is almost similar to string but list slightly different

Most common operations are listed below-

tuple1=(1,2,3) and tuple2(4,5,6)

Operation Expression  Result 
Membership 3 in tuple1True 
Length len(tuple1)
+(concatenation)
tuple1+tuple2 (1,2,3,4,5,6)
Iteration
For item in tuple1:
print(item)
 
123

 Indexing, slicing and matrixes

Indexing, slicing and matrixes works similar to the string.

Tuple1=(‘hello’,’Hello’,’HELLO’)
 Expression ResultDescription
 tuple1[1] HelloItem at index 1 start from 0
 tuple1[-1] HelloCount from right 
 tuple1[1:] [‘Hello’,’HELLO’]Start from 1 to end

Built-in list function are given below-

1) len(tuple1)

It counts the length of the list and returns the number of elements in the list.

Syntax-
 
         len(tuple)

Return value- It returns a number or a length of the tuple.

Example-
  1. tuple1=(1,2,3)  
  2. tuple2=(4,5,6)  
  3. n=len(tuple1)  
  4. print("lenth of tuple1 is :",n)  
Output-
 
2) max(tuple)

It finds the maximum value in the tuple and returns the maximum value.

Syntax-

         max(tuple)

 return value- maximum value from tuple

Example-
  1. tuple1=(1,2,3,4,5,6#return maximum  
  2. m=max(tuple1) #print max element  
  3. print("maximum element:",m)  
Output-

3) min(tuple)

It finds the minimum value element and returns the minimum value element.

Syntax-

         min(tuple)

 return value:- It returns the minimum value element.

 Example-
  1. tuple1=(1,2,3,4,5,6#return minimum  
  2. m=min(tuple1) #print min element  
  3. print("minimum element:",m)  
Output-
4) tuple(seq)

This method converts the sequence into the tuple.

Syntax-

        tuple(seq)

 Example-
  1. seq=(1,2,3,4#convert tuple in list.  
  2. tuple1=tuple(seq) #print tuple  
  3. print("tuple is:",tuple1)  
Output-
 
Summary

In this article, you learnt some concepts of Python tuple like how to create a tuple, accessing a tuple element, insertion of element into tuple, deleting the element and you also friendly with some predefined function.
Next » Python Dictionary