Chapter 12 : Tuples


Tuples are a group of multiple values into a single compounded value.

What is a Tuples

Tuples can contain values of any data type. It is not necessary that the values contained in Tuples are of same type.

Syntax : var/let tupleName = (element1,element2, ... , elementN)

Type Aliases can be used when you refer to existing datatypes that you want to rename to more appropriate one,

Example : var value = (100000, "100,0000","One hundred thousand")

In the above example we have a variable assigned a tuple with types (Int, String, String)

You can even assign a tuple to another variable, you can access a tuple var individual elements by using . (dot) followed by the index of the elements.

Assigning Names to individual elements for tuples :

Syntax : let/var tupleName = (element1Name:value , element2Name:value .... elementNName:value)

You can access individual elements using tupleName (dot) elementName

Example : tupleName.element1Name