How to Use Tuple Access Method in PythonPython Basics


In Python, we can use the operator [] to access the elements in a tuple.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

tup1 = ('Google', 'Baidu', 'eBay', 'Facebook','Amazon')
tup2 = (1, 2, 3, 4, 5, 6, 7)

print("tup1[0]: ", tup1[0])
print("tup2[1:5]: ", tup2[1:5])

Output:

tup1[0]:  Google
tup2[1:5]:  (2, 3, 4, 5)

Syntax

tuple[n]

Parameters

Name Description
n The index for elements to access

Return Value

It returns the corresponding value of index n in the tuple.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us