Variables

Tuples in Python

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


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

Notes

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.


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

< List Comprehensions   |   Dictionaries >

© 2019 SyntaxDB. All Rights Reserved.