How python dict() and tuple() python works?

Python dict()

Python dict() function is a constructor which creates a dictionary. Python dictionary provides three different constructors to create a dictionary:

  • If no argument is passed, it creates an empty dictionary.
  • If a positional argument is given, a dictionary is created with the same key-value pairs. Otherwise, pass an iterable object.
  • If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument.

Python dict() Example

# Calling function  
result = dict() # returns an empty dictionary  
result2 = dict(a=1,b=2)  
# Displaying result  


{'a': 1, 'b': 2}

Python tuple() Function

The python tuple() function is used to create a tuple object.

Python tuple() Function Example

t1 = tuple()  
print('t1=', t1)  
# creating a tuple from a list  
t2 = tuple([1, 6, 9])  
print('t2=', t2)  
# creating a tuple from a string  
t1 = tuple('Java')  
# creating a tuple from a dictionary  
t1 = tuple({4: 'four', 5: 'five'})  


t1= ()
t2= (1, 6, 9)
t1= ('J', 'a', 'v', 'a')
t1= (4, 5)