Python Empty Tuple
How to Create an Empty Tuple
Tuple
A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String datatype and other datatypes. A tuple is immutable (we can't change the values stored in the tuple).
Syntax:
tuple = (“jack”, ”joe”, ”john”)
In python, 4 built-in(collection) data types are present and used to store data collections.
- List
A list is a collection of data elements that are enclosed within square brackets([]) and separated by comma (,). The list is mutable (we can change elements within the list).
- Tuple
A tuple is a collection of data elements enclosed within brackets () and separated by a comma (,). A tuple is immutable.
- Set
Set is a collection of data elements which is enclosed within flower brackets({}) and separated by a comma (,).
- Dictionary
Dictionary is a collection of data elements which is used to store values in keys, and these are enclosed within flower brackets ({}) and separated by comma (,).
Tuple Items
Three items are present in the tuple. They are
- Ordered
- Immutable
- Allow duplicates
Ordered
If the tuple is said to be ordered, then the items of the tuple are defined in a specific order, and that order will not change.
Immutable
Tuples are immutable, meaning we can't change, insert or delete items after the tuple is created.
Allow Duplicates
We can have similar values in tuples because tuples are indexed, and the value will be stored in the indexes.
Example
#program for creating a tuple
X = ((“jack”, “joe”, “john”))
print(x)
Output:
(“jack”, “joe”, “john”)
Explanation
At first, we store a tuple in a variable x and print that tuple as shown above.
Tuple Length
We can determine the number of values or items present in the tuple using the len() function.
Example
#program to print the length of the tuple
A = (“s”, 21, true, “good”)
Print(len(A))
Output:
4
We have given 4 values in the tuple, and in the print statement, we have given the length function so that it will print output as 4.
Type() Function
This function is used to tell about the datatype of the tuple.
Example
#program to display the type of the tuple
X = (“w”, ”ha”)
Y = (“s”)
print(type(X))
print(type(Y))
Output:
< class ‘tuple’>
< class ‘str’>
Explanation
In the x variable, we store the tuple so that it will print the type like a tuple.
In the y variable, we have stored a string value so that it will print the type as str (string).
Creating an Empty tuple:
We can create an empty tuple in two ways.
- By taking parenthesis directly without any values in it.
Example:
#program for creating an empty tuple
A = ()
print(A)
Output:
()
We have created a tuple and have not given any value to it. So it will print an empty tuple.
- We can use the tuple () function to create the empty tuple.
Example:
#program for creating empty tuple using tuple() function
I = tuple()
print(I)
Output:
()
Tuple Indexing:
We can access tuple values using an index of that value. Always the index value starts from 0.
Example:
#program for indexing of tuple
T = (“live”, 22, “s”, ”om”)
print (T[0])
Output:
Live
Explanation:
The 0th index value is live.
Example 2:
#program for slicing of tuple
A = (“this”, 43, “a”)
print(A[0:2])
Output:
(‘this’, 43)
Explanation:
The slicing operator print the 0th and 1st index values.