0% found this document useful (0 votes)
74 views

Tuple

Tuples are ordered and unchangeable collections of data in Python. A tuple is defined using parentheses to group items separated by commas. Tuples can contain different data types and allow duplicate values. Common tuple methods include count() to return the number of times a value appears in the tuple, and index() to return the position of the first item found matching a specified value.

Uploaded by

Jatin Deep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Tuple

Tuples are ordered and unchangeable collections of data in Python. A tuple is defined using parentheses to group items separated by commas. Tuples can contain different data types and allow duplicate values. Common tuple methods include count() to return the number of times a value appears in the tuple, and index() to return the position of the first item found matching a specified value.

Uploaded by

Jatin Deep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Tuple

Python Tuples (w3schools.com) Source

Python Tuples

mytuple = ("apple", "banana", "cherry")

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data,
the other 3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

Example
Create a Tuple:

thistuple = ("apple", "banana", "cherry")


print(thistuple)

Output
('apple', 'banana', 'cherry')

Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.

Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.

1
Ordered
When we say that tuples are ordered, it means that the items have a defined
order, and that order will not change.

Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items
after the tuple has been created.

Allow Duplicates
Since tuples are indexed, they can have items with the same value:

Example
Tuples allow duplicate values:

thistuple = ("apple", "banana", "cherry", "apple", "cherry")


print(thistuple)

Output

('apple', 'banana', 'cherry', 'apple', 'cherry')

Tuple Length
To determine how many items a tuple has, use the len() function:

Example
Print the number of items in the tuple:

2
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))

Output

Create Tuple With One Item


To create a tuple with only one item, you have to add a comma after the item,
otherwise Python will not recognize it as a tuple.

Example
One item tuple, remember the comma:

thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))

Output

<class 'tuple'>
<class 'str'>

Tuple Items - Data Types


Tuple items can be of any data type:

Example
String, int and boolean data types:

tuple1 = ("apple", "banana", "cherry")


tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)

3
A tuple can contain different data types:

Example
A tuple with strings, integers and boolean values:

tuple1 = ("abc", 34, True, 40, "male")

type()
From Python's perspective, tuples are defined as objects with the data type
'tuple':

<class 'tuple'>

Example
What is the data type of a tuple?

mytuple = ("apple", "banana", "cherry")


print(type(mytuple))

Output

<class 'tuple'>

4
Tuple Methods

Method Description

count() Returns the number of times a specified value occurs in a tuple

index() Searches the tuple for a specified value and returns the position
of where it was found

Python has two built-in methods that you can use on tuples.

Python Tuple count() Method


Example
Return the number of times the value 5 appears in the tuple:

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.count(5)

print(x)

Output

5
Python Tuple index() Method
Example
Search for the first occurrence of the value 8, and return its position:

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.index(8)

print(x)

Output : 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy