Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.2 Lesson Period 4 - Tuples Data Abstraction
Tuples - 3.2.5
Definition
A tuple is an ordered, immutable collection of elements.
Tuples are similar to lists, but unlike lists, they cannot be changed once defined.
Tuples are created using parentheses ()
.
Example of a Tuple
# Creating a tuple
coordinates = (10.5, 12.7)
# Accessing elements in a tuple by index
x = coordinates[0] # First element (index 0)
y = coordinates[1] # Second element (index 1)
# Output
print("Tuple Example:")
print(f"X coordinate: {x}") # Output: X coordinate: 10.5
print(f"Y coordinate: {y}") # Output: Y coordinate: 12.7
```python
# Tuples are often used in different scenarios, such as storing RGB color values, coordinates, or points in geometry.
# Tuples are immutable, meaning their contents can't be changed after creation.
colorTuple = (255, 0, 128) # RGB color value (red, green, blue)
print("Color Tuple:", colorTuple)
print("-"*50)
# Tuple Length
print("Length of colorTuple:")
print(len(colorTuple))
print("-"*50)
# Tuple Unpacking
print("Unpacking the tuple:") # Quickly assign each value to a variable
red, green, blue = colorTuple # Assigns the RGB values to separate variables
print("Red:", red)
print("Green:", green)
print("Blue:", blue)
print("-"*50)
# Tuple Repetition
print("Repetition of colorTuple:") # Repeats the tuple values 2 times
print(colorTuple * 2)
print("-"*50)
# Min, Max, Sum
print("Minimum value in colorTuple:") # Finds the smallest value
print(min(colorTuple))
print("Maximum value:")
print(max(colorTuple))
print("Sum of RGB values:")
print(sum(colorTuple))
print("-"*50)
Color Tuple: (255, 0, 128)
--------------------------------------------------
Length of colorTuple:
3
--------------------------------------------------
Unpacking the tuple:
Red: 255
Green: 0
Blue: 128
--------------------------------------------------
Repetition of colorTuple:
(255, 0, 128, 255, 0, 128)
--------------------------------------------------
Minimum value in colorTuple:
0
Maximum value:
255
Sum of RGB values:
383
--------------------------------------------------
Javascript Version
// Tuples are often used in different scenarios, such as storing RGB color values, coordinates, or points in geometry.
// Tuples are immutable, meaning their contents cant be changed after creation.
const colorTuple = [255, 0, 128]; // RGB color value (red, green, blue)
console.log("Color Tuple:", colorTuple);
console.log("-".repeat(50));
// Tuple Length
console.log("Length of colorTuple:");
console.log(colorTuple.length);
console.log("-".repeat(50));
// Tuple Unpacking
console.log("Unpacking the tuple:"); // Quickly assign each value to a variable
const [red, green, blue] = colorTuple; // Assigns the RGB values to separate variables
console.log("Red:", red);
console.log("Green:", green);
console.log("Blue:", blue);
console.log("-".repeat(50));
// Tuple Repetition
console.log("Repetition of colorTuple:"); // Repeats the tuple values 2 times
console.log(colorTuple.concat(colorTuple));
console.log("-".repeat(50));
// Min, Max, Sum
console.log("Minimum value in colorTuple:"); // Finds the smallest value
console.log(Math.min(...colorTuple));
console.log("Maximum value:");
console.log(Math.max(...colorTuple));
console.log("Sum of RGB values:");
console.log(colorTuple.reduce((acc, value) => acc + value, 0));
console.log("-".repeat(50));