×

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 pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

Exrex Python

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

4 minutes read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Reading a File Line by Line in Python

Introduction In this tutorial, we will learn about reading files in python line by line. Before reading the files, let us know a little information about the files first. Files A file is...

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

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

Python And Operator

Python's and operator takes two operands that can be either object, Boolean expressions, or both. And operator creates more complex expressions using those operands. Conditions are the common name for...

8 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Run exec python from PHP

PHP (which is a recursive abbreviation for PHP Hypertext Preprocessor) is one of the most broadly involved web improvement innovations on the planet. PHP code utilized for creating sites and...

4 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.