Saltar al contenido

Python: descripción general de las tuplas – PythonForBeginners.com


Tuple  is another data type in Python. 

A tuple consists of values separated by commas.

Tuples are always enclosed in parentheses.

Tuples are for immutable.

Though tuples may seem similar to lists, they are often used in different
situations and for different purposes. 

Empty tuples are constructed by an empty pair of parentheses.

Tuple with one item is constructed by following a value with a comma.

Ejemplos de tuplas


x = ()				# empty tuple
x = (0,)			# one item tuple
x = (0, 1, 2, "abc")            # four item tuple: indexed x[0]..x[3]
x = 0, 1, 2, "abc"              # parenthesis are optional
x = (0, 1, 2, 3, (1, 2))        # nested subtuples
y = x[0]		      	# indexed item
y = x[4][0]                    	# indexed subtuple
x = (0, 1) * 2                  # repeat
x = (0, 1, 2) + (3, 4)         	# concatenation
for item in x: print item    	# iterate through tuple
b = 3 in x			# test tuple membership

Entrenamiento de Python recomendado

Para el entrenamiento de Python, nuestra principal recomendación es DataCamp.