Introduction to the Julia programming language
6 Tuples¶
Tuples Are Immutable¶
A tuple is a sequence of values. The values can be of any type, and they are indexed by integers, so in that respect tuples are a lot like arrays. The important difference is that tuples are immutable and that each element can have its own type.
In [5]:
t = ('a', 'b', 'c', 'd', 'e')
('a', 'b', 'c', 'd', 'e')
To create a tuple with a single element, you have to include a final comma:
In [6]:
t1 = ('a',)
('a',)
Swapping two variables:¶
In [9]:
a, b = 1, 2
temp = a
a = b
b = temp
1
This solution is cumbersome; tuple assignment is more elegant:
In [11]:
a, b = 1, 2
a, b = b, a
(2, 1)
Tuples as Return Values¶
In [13]:
function minmax(t)
minimum(t), maximum(t)
end
t = (1, 2, 3, 4)
minmax(t)
(1, 4)