Python : Tuples



Python | Tuple

A tuple in Python is similar to a list.

Tuple is one of 4 built-in data types in Python used to store collections of data

Tuples are  collection of data  which is order and unchangeable 

Tuples are write between round parentheses


Example

    ('mango' ,' banana', 'apple')
    
    (19,22,30,45)
    
     (TrueFalseFalse)



A tuple can also contain different types of data
  
    tuple = (19,22.25,'mango' ,' banana', 30,45 )

    print(tuple)            #Return  (19,22.25,'mango' ,' banana', 30,45 )



print Tuple

print() Function are used for print the tuple


Example

    tuple =    ('mango' ,' banana', 'apple' )

     print(tuple             


Output
    
    'mango' ,' banana', 'apple' )


Tuple Items


Tuple items are ordered, unchangeable, and allow duplicate values

Ordered : tuples data stored Ordered manner and that has not change in any case

unchangeable : tuples are unchangeable , means we cannot add ,or remove items after create a tuple

Tuples are allow to duplicate  and print them as it is


Example

    tuple =    ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')

     print(tuple)             

Output

    'mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple') 



' in ' Keyword

you can check the specified item is exist in tuple using 'in' keyword

Example - 1 

    tuple =    ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')

    if 'apple' in tuple :
         print('yes')             

Output

    yes
         

Example -  2

    tuple =    ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')

    if 'papaya' in tuple :
         print('yes')             
     
    else :
         print('no')             

Output
        
    no


Post a Comment (0)
Previous Post Next Post