Python Tutorial | Python tuples 2

Accessing Elements in a Tuple

There are various ways in which we can access the elements of a tuple.

Indexing

We can use the index operator [] to access an item in a tuple where the index starts from 0.
So, a tuple having 6 elements will have an index from 0 to 5. Trying to access an element other that (6, 7,…) will raise an IndexError.
The index must be an integer, so we cannot use float or other types. This will result in TypeError.
Likewise, nested tuple is accessed using nested indexing, as shown in the example below.

c=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)

Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

n=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)
print(n)
print(n[0])
print(n[3])
print(n[6])
print(n[2])
print(n[1.5]) #error (float index is not allowed)

output:

(‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
c
p
e
m

n=('c','o','m','p','u','t','e','r')
print(n)
print(n[0])
print(n[3])
print(n[6])
print(n[2])
print(n[1.5]) #error (float index is not allowed)

Output:

('c', 'o', 'm', 'p', 'u', 't', 'e', 'r')
c
p
e
m
Traceback (most recent call last):
  File "E:/temp_site/python/Step_10_Tuple/tuple_temp.py", line 7, in <module>
    print(n[1.5]) #error (float index is not allowed)
TypeError: tuple indices must be integers or slices, not float
>>> 

Example:

n=(‘c’,’a’,’t’,’a’,’l’,’y’,’s’,’t’)

positive index

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

print(n)
print(n[1])
print(n[6])
print(n[2])
print(n[7])
print(n[3])
print(n[2.4]) #error (float index is not allowed)
print(n[8]) #error (index out of range)

output

(‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’)
a
s
t
t
a

n=('c','a','t','a','l','y','s','t')    
print(n)
print(n[1])
print(n[6])
print(n[2])
print(n[7])
print(n[3])
#print(n[2.4]) #error (float index is not allowed)
#print(n[8]) #error (index out of range)

Output:

('c', 'a', 't', 'a', 'l', 'y', 's', 't')
a
s
t
t
a
>>> 

Negative Indexing

Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.

Example:

n=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)

Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)
print(n)
print(n[-0])
print(n[-3])
print(n[-6])
print(n[-2])
#print(n[1.5]) #error

Ouput:

(‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
c
t
m
e

n=('c','o','m','p','u','t','e','r')
print(n)
print(n[-0])
print(n[-3])
print(n[-6])
print(n[-2])

Output:

('c', 'o', 'm', 'p', 'u', 't', 'e', 'r')
c
t
m
e
>>> 

Example:

n=(‘c’,’a’,’t’,’a’,’l’,’y’,’s’,’t’)

positive index

0

1

2

3

4

5

6

7

c

a

T

a

l

y

s

t

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n=(‘c’,’a’,’t’,’a’,’l’,’y’,’s’,’t’)
print(n)
print(n[-1])
print(n[-6])
print(n[-2])
print(n[-7])
print(n[-3])
#print(n[2.4])#error
#print(n[8])#error (out of index)

Output:

(‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’)
t
t
s
a
y

n=('c','a','t','a','l','y','s','t')    
print(n)
print(n[-1])
print(n[-6])
print(n[-2])
print(n[-7])
print(n[-3])

Output:

('c', 'a', 't', 'a', 'l', 'y', 's', 't')
t
t
s
a
y
>>> 

Visitor Counter

0056734