Python - Tuple Tuples comprehensions

Introduction

Tuples comprehensions convert tuples.

The following, makes a list from a tuple, adding 20 to each item along the way:

Demo

T = (1, 2, 3, 4, 5) 
L = [x + 20 for x in T] 
print( L )

Result

List comprehensions are sequence operations.

Tuples index and count methods:

Demo

T = (1, 2, 3, 2, 4, 2)         # Tuple methods in 2.6, 3.0, and later 
print( T.index(2) )                     # Offset of first appearance of 2 
print( T.index(2, 2) )                  # Offset of appearance after offset 2 
print( T.count(2) )                     # How many 2s are there?
# from  ww w  .  j  a  va 2s.com

Result

Prior to 2.6 and 3.0, tuples have no methods.

A list inside a tuple, for instance, can be changed as usual:

Demo

T = (1, [2, 3], 4) 
#T[1] = 'test'                  # This fails: can't change tuple itself 
#TypeError: object doesn't support item assignment 
#   w w  w  .ja  v  a  2  s .c  om
T[1][0] = 'test'               # This works: can change mutables inside 
print( T )

Result