Menu Bar

Horizontal Scrollable Menu

ads

Search This Blog

Tuple without parenthesis , Tuple unpacking , list inside tuples Python

Tuple without parenthesis

 A tuple is a collection that is ordered and is immutable. Pythons are written in tuples with round brackets. Python tuples are used in python tuples to embrace an immutable object. Python tuples are similar to lists except in certain situations. Python tuples are immutable means that they cannot be modified throughout the program.

Sunn = 'one', 'two', 'three', 'four'print (type (numb))

        # <Class 'tuple'>

Tuple packing and unpacking

Packing and Unpacking the Tuple: Python has a very powerful tuple assignment feature that assigns the right hand side of value to the left hand side. In another way it is called unpacking the topl of values ​​in a variable. In packing, we insert values ​​into a new tuple while in unpacking we extract those values ​​into a single variable.


name='pramod','kumar','yadav'

f_name,m_name,l_name = name

print('first name is:',f_name) 

 # OutPut = first name is: pramod

print("middle name is:",m_name) 
     
# OutPut = middle name is: kumar

print("last name is:",l_name) 
      
 # OutPut = last name is: yadav

list inside tuples

The value inside the tuple is simply a reference to a list, and if I change
 the values in the list then everything is still in order, right? I want to
make sure that if I do this I will not start running into misleading errors
 
favorites = ('apple',['tomato','potato'])

favorites[1].pop()


print(favorites)    
                  
 # OutPut = ('apple', ['tomato'])

favorites[1].append('cauliflower')

print(favorites)  
                      
 # OutPut = ('apple', ['tomato', 'cauliflower'])

No comments:

ads3