Empty tuple creation in Python

Python | empty tuple: Here, we are going to learn how to create an empty tuple in Python programming language? By IncludeHelp Last updated : December 21, 2023

Prerequisite

Python | Empty Tuple

In Python, we can also create a tuple without having any element i.e., an empty tuple. An empty tuple is created by using two approaches, use a pair of round brackets () without any value, or use the tuple() method.

Create an empty tuple using pair of round brackets ()

As written above, we can create an empty tuple by using the pair of round brackets ().

Syntax

Below is the syntax to create an empty tuple:

tuple_name = ()

Example to empty tuple using pair of round brackets ()

Create an empty tuple, print the tuple, its length, and type.

# Python empty tuple creation`
tuple1 = () 

# printing tuple 
print("tuple1: ", tuple1)
# printing length
print("len(tuple1): ", len(tuple1))
# printing type
print("type(tuple1): ", type(tuple1))

Output

tuple1:  ()
len(tuple1):  0
type(tuple1):  <class 'tuple'>

To print the tuple, we used the print() method. To print its length we used len() method and to print its type, we used the type() method.

Create an empty tuple using tuple() method

We can also create an empty tuple by initializing the tuple with tuple() – generally this method is used to clear/reinitialize the tuple.

Syntax

Below is the syntax to create an empty tuple:

tuple_name = tuple()

Example to create an empty tuple using tuple() method

# Python empty tuple creation`
tuple1 = tuple() 

# printing tuple 
print("tuple1: ", tuple1)
# printing length
print("len(tuple1): ", len(tuple1))
# printing type
print("type(tuple1): ", type(tuple1))
print()

# non-empty tuple
tuple2 = (10, 20, 30, 40, 50)
# printing original tuple
print("tuple2: ", tuple2)
print()

# reinitialize
tuple2 = tuple()
print("After reinitialize...")
# printing tuple 
print("tuple2: ", tuple2)
# printing length
print("len(tuple2): ", len(tuple2))
# printing type
print("type(tuple2): ", type(tuple2))

Output

tuple1:  ()
len(tuple1):  0
type(tuple1):  <class 'tuple'>

tuple2:  (10, 20, 30, 40, 50)

After reinitialize...
tuple2:  ()
len(tuple2):  0
type(tuple2):  <class 'tuple'>


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.