Name

Tuple

Examples
# Define a tuple with parentheses
colors = ('Red', 'Blue', 'Green')

# Iterate over tuple with for loop
for item in colors:
  print item

# Get an item from a tuple by index
print(colors[2]) # Prints: Green

# Results in a type error: tuples can't be changed once created!
colors[2] = "chartreuse"
# Convert a list to a tuple with tuple()
rocky = ['Mercury', 'Venus', 'Earth', 'Mars']
rocky_tuple = tuple(rocky)

# Convert a tuple to a list with list()
giants = ('Jupiter', 'Saturn', 'Uranus', 'Neptune')
giant_list = list(giants)
# Create a single-item tuple (comma needed after first item)
one_item = ('So lonely',)
Description
Syntax
Parameters
elem
Related list()
Dictionary
[] (Index brackets)
() (parentheses)
Updated on Wed Jan 18 17:32:21 2017.
Creative Commons License