Tuples
Part of the Fundamentals section of Coddy's Swift journey — lesson 68 of 86.
While arrays store multiple values of the same type, sometimes you need to group a few related values of different types together. This is where tuples come in handy.
A tuple is a fixed collection of values that can have different types. You create one by placing values inside parentheses, separated by commas:
let person = ("Alice", 25, true)
print(person) // ("Alice", 25, true)To access individual values, use their index with a dot:
let coordinates = (10.5, 20.3)
print(coordinates.0) // 10.5
print(coordinates.1) // 20.3You can also name the elements for better readability:
let product = (name: "Laptop", price: 999.99)
print(product.name) // Laptop
print(product.price) // 999.99Another useful feature is decomposing a tuple into separate variables:
let point = (3, 7)
let (x, y) = point
print(x) // 3
print(y) // 7Tuples are perfect for returning multiple values from a function or grouping small amounts of related data without creating a full structure.
Challenge
EasyYou will receive three lines of input:
- A name (String)
- An age (Int)
- A score (Double)
Create a tuple called student with named elements: name, age, and score.
Then, decompose the tuple into three separate variables: studentName, studentAge, and studentScore.
Print the following three lines:
- The student's name accessed using the named element
- The student's age accessed using the index (dot notation with number)
- The decomposed score variable
Example: If the inputs are Emma, 21, and 95.5, your output should be:
Emma
21
95.5Cheat sheet
A tuple is a fixed collection of values that can have different types. Create one using parentheses with comma-separated values:
let person = ("Alice", 25, true)Access tuple elements by index using dot notation:
let coordinates = (10.5, 20.3)
print(coordinates.0) // 10.5
print(coordinates.1) // 20.3Name tuple elements for better readability:
let product = (name: "Laptop", price: 999.99)
print(product.name) // Laptop
print(product.price) // 999.99Decompose a tuple into separate variables:
let point = (3, 7)
let (x, y) = point
print(x) // 3
print(y) // 7Try it yourself
// Read input
let name = readLine()!
let age = Int(readLine()!)!
let score = Double(readLine()!)!
// TODO: Write your code below
// 1. Create a tuple called 'student' with named elements: name, age, and score
// 2. Decompose the tuple into three separate variables: studentName, studentAge, and studentScore
// 3. Print the student's name using the named element (student.name)
// 4. Print the student's age using index notation (student.1)
// 5. Print the decomposed score variable (studentScore)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input