Python Tuples & Its Type() With Examples

We cover Python tuples with examples in this article, where we hope to satisfy the needs of our readers.

Python tuples are similar to lists. One of the differences between tuples and lists is that a tuple cannot change its elements once it has been assigned.

Python Tuples & Types Mr Examples



Create Tuple With One Item

If you want to construct a tuple with only one item, add a comma after it. Otherwise, Python will not detect it as a tuple.

Comma-separated one-item tuple:

Example

vartuple = ("United States Of America",) print(type(vartuple))#NOT a tuple vartuple = ("Germany") print(type(vartuple))

Python Tuple

A Python tuple is a collection of multiple items stored in a single variable.

In Python, there are 4 built-in data types: Tuple, List, Set and Dictionary all of which have different attributes and purposes.

Generally, a tuple is an ordered, unchangeable collection.

Round brackets are used to indicate multiples.

Make a tuple:

Example

vartuple = ("World Health Organization", "United Nations", "United States Of America") print(vartuple)

Unchangeable

In Python, tuples are unmodifiable, which implies that items can’t be added, removed or changed after being generated.


Tuple Length

To determine how many items a Python tuple contains, execute the len() function:

Count the items in the tuple:

Example

pythontuple = ("World Health Organization", "United Nations", "United States Of America", "United Kingdom", "Germany") print(len(pythontuple))

Tuple Items Data Types

There is no restriction on the type of data in a tuple item:

A string, an integer, and a boolean data type:

Example

vartuple1 = ("World Health Organization", "United Nations", "United States Of America", "United Kingdom", "Germany") vartuple2 = (9, 8, 7, 6, 5) vartuple3 = (False, True, False) print(vartuple1) print(vartuple2) print(vartuple3)

Different types of data can be contained in a tuple:

The following tuple contains strings, integers, and booleans:

Example

vartuple = ("Elon Musk", 51, True, 100, "Male") print(vartuple)

Tuple Items

Python tuples are ordered, unchangeable, and allow duplicate values.

There are multiple items indexed. The first item has index [0] and the second item has index [1].


Tuple Ordered

An ordered tuple indicates that the items have a specified order, and that order will remain unchanged.


Python Tuple type()

Tuples are represented by Python as objects with the data type ‘tuple’:

<class ‘tupletype’>

Can you tell me what a tuple data type is?

Example

vartuple = ("World Health Organization", "United Nations", "United States Of America", "United Kingdom", "Germany") print(type(vartuple))

Allow Duplicates

It is important to remember that tuples are indexed, which means they can have similar items:

The following values can be duplicated in a tuple:

Example

vartuple = ("WHO", "UN", "USA", "UK", "Germany") print(vartuple)


The tuple() Constructor

Tuples can also be constructed using the tuple() constructor.

Making a tuple with tuple():

Example

vartuple = tuple(("World Health Organization", "United Nations", "United States Of America", "United Kingdom", "Germany")) # note the double round-brackets print(vartuple)

Python’s Tuple over Lists advantages

Tuples and lists are both used in similar situations, since they are quite similar.

Tuples offer certain advantages over lists, however:

  • When dealing with heterogeneous (different) types of data, we generally use tuples, and when dealing with homogeneous (similar) types of data, we generally use lists.

  • Tuples are immutable and therefore faster to iterate than lists. Therefore, performance has been slightly improved.

  • Keys for dictionaries can be composed of tuples that contain immutable elements. There is no way to do this with lists.

  • Implementing the data as tuples will ensure that it remains write-protected if it doesn’t change.


Python Collections (Arrays)

Python has four types of collection data:

  1. A list is a collection that can be ordered and changed. Members can be duplicated.
  2. A tuple is an ordered and unchangeable collection. Members can be duplicated.
  3. Sets are unindexed and unordered collections. The same member cannot be duplicated.
  4. Dictionary is a collection of items that can be ordered and changed. Duplicates are not allowed.

Additional Information: The order of dictionaries has been introduced with Python version 3.7. Dictionary entries are unordered in Python 3.6 and earlier.

In order to make the right choice when it comes to selecting a collection type, it is critical to understand the properties of that type.

The correct type of data set to use for a particular data set can ensure the retention of meaning, as well as an increase in efficiency and security.

Your reaction is valuable fuel for our improvement. Share your thoughts with us.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *