liveBook · Manning

Tuple

As explained in Julia as a Second Language

A tuple is a fixed-size ordered collection whose elements may have different types and whose contents cannot be changed.

A tuple is an ordered collection written with parentheses, useful when one record combines values of different types. Unlike an array, a tuple is immutable: you can read its values, but you cannot replace them. section 4.9 section 4.13

Use tuples when each position has a distinct meaning and the record should remain unchanged:

  • Write a pizza record as ("hawaiian", 'S', 10.5), combining a String, Char, and Float64. section 4.9
  • Inspect its precise element types with typeof; Julia reports Tuple{String, Char, Float64}. section 4.9
  • Read values by index, iterate with a for loop, or pass numeric tuples to functions such as sum, median, and mean. section 4.9
  • Store multiple records in an array of tuples, then extract a field with a helper such as name(pizza) = pizza[1]. section 4.9

Pizza sales records

julia> sales = [("hawaiian", 'S', 10.5), ("sicilian", 'S', 12.25), ("hawaiian", 'L', 16.5)] julia> sales[1] ("hawaiian", 'S', 10.5) Each tuple represents one sale, while the surrounding array holds the collection. This arrangement preserves the distinct types of the pizza name, size, and price. A tuple can be read but not modified: pza[1] = "pepperoni" raises a setindex! error. section 4.9

pizza_tuple = ("hawaiian", 'S', 10.5)

View the compact pizza tuple literal used to introduce mixed-type records. section 4.9

How Julia as a Second Language covers it

How deep it goes
Explains construction, element types, indexing, iteration, numeric functions, and immutability, while deferring the broader type system.
Examples
Demonstrates pizza records, pizza sales tables, tuple arrays, field-accessor functions, and numeric summaries.
What it assumes
Builds from arrays and basic numbers, characters, strings, Boolean values, indexing, and loops.
What you can do after
Evaluate/understand tuple records, read and iterate their values, inspect their types, and organize them into sales data.
How it connects
Connects tuples with arrays, Char, String, indexing, iteration, accessors, and the Julia type system.

Also explained in

Compare how 15 books explain tuple →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.