×

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

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

How to Configure Python Interpreter in Eclipse

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....

3 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

Python program to add two number

Python program to add two number This program will add the two numbers and display their sum on the screen. Example: Input: Number1 = 20        Number2 = 30   Output: Sum =...

2 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

How to Open a File in Python with a Path?

File in Python File handling is an important operation; it allows the programmer to access the data in the files, such as text, binary values and excel sheets. The CSV files...

7 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Python List insert() method

Python List insert() method The list.insert () method in Python inserts an item at the specified position. Syntax list.insert(i, x) Parameter i: This parameter represents a number specifying the position to insert the given value. x: This parameter signifies...

1 minute read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

What is Python compiler GDB?

The source code of one programming language is converted into machine code, bytecode, or another programming language by a compiler, a specialised software. A compiler is a tool that converts high-level...

3 minutes read.

Python Graph

Python Graph: In Computer Science and Mathematics, a Graph is a pictorial representation of a group of objects or elements where some elements are connected using the links. A graph...

5 minutes read.

Difference between Python and CPP

Difference between Python and C++ We all know that both C++ and Python are two of the most popular programming languages. Both C++ and Python shares some basic similarities such as...

6 minutes read.

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.