OKPEDIA PYTHON EN

How to make tuples in Python

To define a tuple in python write the elements in parentheses separating them with a comma.

nametuple = ( x1, x2, ... )

The tuple variable name is to the left of the equals sign.

The elements of the tuple are listed on the right.

What is a tuple in python

A tuple is a set of numeric or alphanumeric x elements.

Example

tuple = ( 'Rome', 'Italy', 2019)

The elements of the tuple can also be of different types.

It is an iterable object similar to list.

What's the difference between a tuple and a list? Both are collections of python iterable objects. However, they are not the same. A tuple is an immutable collection. Elements inside a tuple cannot be deleted or replaced, nor added. A list, on the other hand, is an editable collection.

Example

Create a tuple with the first six letters of the alphabet.

>>> vote = ( 'A', 'B', 'C', 'D', 'E', 'F' )

View the contents of the tuple

>>> vote
('A', 'B', 'C', 'D', 'E', 'F')

https://how.okpedia.org/en/python/how-to-make-tuples-in-python


Report an error or share a suggestion to enhance this page



FacebookTwitterLinkedinLinkedin