×

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

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

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

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

How to Iterate through a Dictionary in Python

In this tutorial, we will learn how to interate throught the dictionary in the Python programming, using different approaches with example. Introduction to Dictionary in Python Dictionary is a special type of...

4 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

Conditional Expressions in Python

In Python conditional expression are sometimes referred to operator called as ternary operator. Not only Python supports ternary operator but many other programming languages supports it. Ternary operator are the...

2 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Python ltrim() function

Python ltrim() function The ltrim() function in PHP removes whitespace or other predefined characters from the beginning or left side of a string. The related functions are as follows: rtrim() – This function is used to...

1 minute read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.