Fall 2024 - P2
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.2 Lesson Period 2 - Tuples Data Abstraction
Student led teaching on Abstraction. Teaching how various data types can use abstraction for copmutational efficiency.
Tuples - 3.2.5
# Tuples have many uses, they are often used in math applications for things like coordinates...
# Tuples are immutable meaning they can't be changed after creation
myTuple = (8, 12)
print("Tuple:", myTuple)
print("-"*50)
# Tuple Length
print("Length:")
print(len(myTuple))
print("-"*50)
# Tuple Unpacking
print("Unpacking:") # abstracts having to index then manually assign
x, y = myTuple # assigns each part of tuple to seperate variables
print("x:", x)
print("y:", y)
print("-"*50)
# Tuple Repetition
print("Repetition:") # simplifies having to loop or index then reassign
print(myTuple * 3)
print("-"*50)
# Min, Max, Sum
print("Minimum:") # simplifies having to loop then compute all manually
print(min(myTuple))
print("Maximum:")
print(max(myTuple))
print("Summation:")
print(sum(myTuple))
print("-"*50)
Tuple: (8, 12)
--------------------------------------------------
Length:
2
--------------------------------------------------
Unpacking:
x: 8
y: 12
--------------------------------------------------
Repetition:
(8, 12, 8, 12, 8, 12)
--------------------------------------------------
Minimum:
8
Maximum:
12
Summation:
20
--------------------------------------------------
Javscript Version
%%js
let myTuple = [8, 12]; // Arrays are used in place of tuples in JavaScript
console.log("Tuple:", myTuple);
console.log("-".repeat(50));
// Length of the array
console.log("Length:");
console.log(myTuple.length);
console.log("-".repeat(50));
// Unpacking
let [x, y] = myTuple;
console.log("x:", x);
console.log("y:", y);
console.log("-".repeat(50));
// Repetition
console.log("Repetition:");
console.log([...myTuple, ...myTuple, ...myTuple]); // Repeating array elements
console.log("-".repeat(50));
// Min, Max, Sum
console.log("Minimum:");
console.log(Math.min(...myTuple));
console.log("Maximum:");
console.log(Math.max(...myTuple));
console.log("Summation:");
console.log(myTuple.reduce((a, b) => a + b, 0)); // Summing elements of array
console.log("-".repeat(50));
print("Minimum:")
print(min(myTuple))
print("Maximum:")
print(max(myTuple))
print("Summation:")
print(sum(myTuple))
print("-"*50)