×

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.


Related Topics

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

Python vs R

Python: - Python is a popular high-level, general-purpose programming language. Python (the most recent version is Python 3) is a programming language used in the software industry for website development, machine...

3 minutes read.

Working with JSON in Python

Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. In this article, we are going...

4 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Collections in python

In this tutorial, we will see what is collection in python.  Further, we will see in-depth the different kinds of collections in python. Collections Collections in python are the built-in module used...

9 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Python Console

Console in Python is referred as the command line Interpreter- (CLI) and also knows as Shell and it functions as taking input from the human user and interpreting it through...

6 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.