Tuples in Python

Tuples are used to store multiple items sequentially in one variable. Similar to lists, but immutable.

tuple_name = (item1, item2, item3)
#accessing item at index


Tuples are immutable, which means their values at the indices cannot be changed, unless there is a value swap.

The array index range is from 0 to size minus 1. Negative indices start at the end of the array and work backwards.

Tuples can be combined using the addition operator.

When declaring a tuple, the parentheses are optional.

provinces = ("Ontario", "BC", "Alberta", "Quebec")
provinces[0] #will return Ontario

< List Comprehensions   |   Dictionaries >

© 2019 SyntaxDB. All Rights Reserved.