Empty Tuple PythonWhat are Tuples in Python?A tuple is an arrangement of immutable, ordered items. Since both tuples and Python lists are sequences, they are analogous. Tuples and lists, however, vary since we cannot edit tuples; however, we can change lists after initializing them. Additionally, we build tuples using parentheses, whereas we make lists using square brackets. A tuple is created by putting different values inside the parenthesis, separated by commas. For example, Example of a TupleYou can create an empty tuple object by giving no elements in parentheses in an assignment statement. The built-in function of Python, tuple(), also creates a blank tuple object when it is called without any arguments. Code Output: () () How to Check Empty Tuple in Python?You can generate an empty tuple by placing no components in parenthesis in the assignment phrase. The built-in method tuple() also creates an empty tuple object when called without passing any arguments. Using the not OperatorCode Output: The given tuple is empty () Using the len() Function Code Output: The given tuple is empty () An empty tuple called "my tuple" was initialized in the instance above. The length of the tuple was then determined using the built-in Python function len() and saved in the variable name "len_tuple." The length of my_tuple was then checked using an if statement to see if it equaled zero. The tuple is considered empty if the condition stands true. The tuple is considered as not empty otherwise. Changing a Tuple to Empty TupleLet's suppose we have a tuple that has elements in it. We need to change it to an empty tuple. Let us see how to do this. Code Output: Original tuple: ('a', 3, 'b', 'c', 'd', 'e', 'g', 's', 'k', 'v', 'l') After removing a single item:- ('a', 3, 'b', 'c', 'e', 'g', 's', 'k', 'v', 'l') New empty tuple:- () Comparing with Another Empty TupleWe will see the results if we compare two tuples Code Output: my_tuple1 is not empty Next TopicPlot Line in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India