0% found this document useful (0 votes)
7 views1 page

Python Tuple Methods CheatSheet

This cheat sheet provides an overview of Python tuple methods, including 'count' to find occurrences of an item and 'index' to locate the first occurrence of an item. It also highlights tuple characteristics such as immutability, creation, access, slicing, nested tuples, and the requirement of a comma for single-element tuples. Examples are provided for each method and characteristic to illustrate their usage.

Uploaded by

nithinnt07
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)
7 views1 page

Python Tuple Methods CheatSheet

This cheat sheet provides an overview of Python tuple methods, including 'count' to find occurrences of an item and 'index' to locate the first occurrence of an item. It also highlights tuple characteristics such as immutability, creation, access, slicing, nested tuples, and the requirement of a comma for single-element tuples. Examples are provided for each method and characteristic to illustrate their usage.

Uploaded by

nithinnt07
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

Python Tuple Methods - Cheat Sheet

Built-in Methods:

count(item)

Returns the number of times an item appears in the tuple.

Example:

t = (1, 2, 2, 3)

[Link](2) # 2

index(item)

Returns the index of the first occurrence of an item.

Example:

t = (10, 20, 30)

[Link](20) # 1

Tuple Characteristics & Tips:

Immutability

Tuples cannot be changed after creation.

Creation

t = (1, 2, 3)

Access

t[0] # 1

Slicing

t[1:] # (2, 3)

Nested Tuples

t = (1, (2, 3), 4)

t[1][0] # 2

Single Element

t = (5,) # Must include comma

You might also like