What Is A Tuple In Python?
A Python tuple is a collection type data structure which is immutable by design and holds a sequence of heterogeneous elements. It functions almost like a python list but with the following distinctions.
Tuples store a fixed set of elements and don’t allow changes whereas the list has the provision to update its content.
The list uses square brackets for opening and closing whereas, and a tuple has got parentheses for the enclosure.
Instantiate A Tuple In Python
Using commas inside a pair of round brackets (), i.e., parentheses.
You can create a tuple even without using the parentheses. Also, the elements of a tuple can be of any valid python data type ranging from numbers strings lists, etc.
Tuple With Different Inputs
Create an empty tuple.
Empty tuple:
It used in between rounded tuple.
Example:1
tuple = ()
print("An Empty tuple", tuple)
Tuple of Numbers:
Example:2
tuple = (3,5,8)
print("An Empty tuple", tuple)
Using The Built-In Function “Tuple()”
Creating tuple from set
Example:
>>> tuple1 = tuple({3,5,8})
>>> type(tuple1 )
<class 'tuple'>
>>>tuple1
If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at contact@codersarts.com.
Please write your suggestion in comment section below if you find anything incorrect in this blog post
Comments