Is a tuple iterable in Python
Python tutorial - data type tuples
- Python 3 basic tutorial
- Python tutorial - data type tuples
Created: January-05, 2020 | Updated: November-05, 2020
- Advantages of tuples over lists
- Create tuples
- Access to tuple elements
- Python tuple methods
- Tuple built-in functions:
- Tuple membership check
- Iterating through a tuple
In this section we show you how to create and use tuples.
A tuple is similar to a list with the difference that the tuple is immutable, but the list is mutable.
Advantages of tuples over lists
- Tuples are mostly used when the elements are of different data types, while lists are used when the elements are of the same data type.
- Iterating through tuples is faster because you cannot update a tuple.
- You can use the immutable elements of the tuple as keys in the dictionary. This cannot be done with a list.
- Tuples guarantee the protection of the data because they cannot be changed.
You can define a Python tuple with the parentheses and separate elements with the commas. A tuple can have elements of any data type.
If the tuple contains only one element, e.g. if the element is of the data type string, then the tuple is not regarded as a tuple but as a string. See the following example:
You can see here that the data type is of rather than. To make it a tuple, a trailing comma is used as the tuple:
Access to tuple elements
The elements of the tuple can be accessed in one of the following ways:
An index operator can be used to access elements of a tuple. The index of the tuple starts at 0. If the index is not in the range (element that is not defined at this index in the tuple), you have one. It should be noted here that the index must be an integer, otherwise a will occur.
Just like with Python list, you can use negative indexes to access tuple elements.
A slicing operator is used to extract a range of elements from a tuple.
Two tuples can be concatenated using the operator.
Delete a tuple
The elements of a tuple cannot be deleted because tuples are immutable. But you can delete the entire tuple with the keyword:
Python tuple methods
Tuples only have two methods because tuples are immutable:
|is used to return the number of elements equal.|
|is used to return the index of the first item equal.|
Tuple built-in functions:
Below are tuples of built-in functions that are applicable to to perform various tasks:
|return if there are all elements of the tuple. It also returns when the tuple is empty.|
|return if is one of the elements of the tuple. It returns when the tuple is empty.|
|Returns the index and the value of all elements of the tuple as a tuple. There is an object in return.|
|returns the number of elements in a tuple or the length of the tuple.|
|convert a sequence (tuple, set, string, dictionary) into tuples.|
|returns the maximum number in the tuple.|
|returns the minimum number in the tuple.|
|return a sorted tuple.|
|returns the sum of all elements of the tuple.|
Tuple membership check
The keyword checks whether an element is a member of the tuple or not. See the code example below:
Iterating through a tuple
You can iterate through a tuple using loop:
- A bariatric surgery is worth it
- How am I supposed to study physics now?
- How does the 2nd amendment help
- What do tourists think of Malaysia
- What are puzzles asked in the interview
- Why should one study statistics?
- Why is Miami International Airport so cold
- See your GRE score instantly
- Henry VIII fathered Mary Boleyn's children
- Where does asbestos come from
- Are you worth being happy
- How can I deal with depression
- Does the Dubrow diet work
- What are the denominations in socialism
- What is gold in Hokkien Chinese
- Which is the hardest math question
- Which languages are endangered
- Can dark matter affect us?
- Will energy be cheaper or more expensive?
- How did you miss your interview
- When does space travel begin?
- Who are Gog Magog in today's world
- What is the best brand AR 15
- How safe is it to travel in Pakistan