×

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 open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python pow() Function

Python pow() Function The pow() function in Python return the parameter ‘x’ to the power ‘y’ and if the parameter ‘z’ is present, it returns x to the power y, modulo z (computed more efficiently than pow(x, y) % z). Syntax pow(x, y[, z]) Parameter x: This parameter represents the base...

1 minute read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables. These are taken into account to do operations on values and variables. There are some special symbols in python...

4 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Standard GUI Unit Converter 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...

12 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.