Skip to the content.

3.2 data abstraction

data abstrction

#3.2 python popcorn hack 1

#step 1
integers = {1, 2, 3, 4, 5}
print("original set:", integers)

#step 2
integers.add(6)
print("adding 6:", integers)

#step 3
integers.remove(2)
print("remove 2:", integers)

#step 4
integer2 = {7,8,9}
union_integers = integers.union(integer2)
print("Union: ", union_integers)

#step 5
integers.clear()
print("clear: ", integers)

original set: {1, 2, 3, 4, 5}
adding 6: {1, 2, 3, 4, 5, 6}
remove 2: {1, 3, 4, 5, 6}
Union:  {1, 3, 4, 5, 6, 7, 8, 9}
clear:  set()
#popcorn hack 2

#step 1
string = "learning python is not fun"
print(string)

#step 2
string_length = len(string)
print(string_length)

#step 3
extract_string = string[9:15]
print(extract_string)

#step 4
uppercase_string = string.upper()
print(uppercase_string)

#step 5
replace_string = string.replace ("fun","good")
print(replace_string)

#bonusd
reversed_string = string[::-1]
print("reversed with slicing", reversed_string)
learning python is not fun
26
python
LEARNING PYTHON IS NOT FUN
learning python is not good
reversed with slicing nuf ton si nohtyp gninrael
#popcorn hack 3

#step 1:
numbers = [3,5,7,9,11]
print(numbers)

#step 2:
third_number = numbers[2]
print("third number in the list:", third_number)

#step 3: 
numbers[1] = 6
print("replace 5 with 6:", numbers)

#step 4:
numbers.append(13)
print("adding 13:", numbers)

#step 5: 
numbers.remove(9)
print("removing 9:", numbers)

#bonus: 
numbers.sort(reverse=True)
print("sorted in reverse order: ", numbers)
[3, 5, 7, 9, 11]
third number in the list: 7
replace 5 with 6: [3, 6, 7, 9, 11]
adding 13: [3, 6, 7, 9, 11, 13]
removing 9: [3, 6, 7, 11, 13]
sorted in reverse order:  [13, 11, 7, 6, 3]
#popcorn hack 4

#step 1:
personal_info = {
    "name": "Joanna",
    "email": "csp@gmail.com",
    "phone number": "123-456-7890",
}

#step 2:
print("original dictionary: ", personal_info)

#step 3:
print("My name is:", personal_info["name"])

#step 4:
personal_info_length = len(personal_info)
print("length of dictionary: ", personal_info_length)

#step 5: 
print(type(personal_info))

original dictionary:  {'name': 'Joanna', 'email': 'csp@gmail.com', 'phone number': '123-456-7890'}
My name is: Joanna
length of dictionary:  3
<class 'dict'>

3.2 Homework hacks python

#part 1: dictionary

personal_info2 = {
    "full_name": "Joanna Hu",
    "years": "17",
    "location:": "San Diego",
    "favorite_food": "dumplings",
}
print("personal info dictionary", personal_info2)

#part 2: list
activites = ["eating wingstop", "roblox", "sleeping"]
print("some of my favorite activies are: ", activites)

#part 3: dict and list
personal_info2["activities"] = activites
print(personal_info2)

#part 4: bool
chosen_activity = "roblox"
activity_available = True 
print(f"Is '{chosen_activity}' available to do right now? {activity_available}")

#part 5: int
total_activities = len(activites)
print(f"I have {total_activities} activities.")

#part 6: tuple
favorite_activities = ("homework", "coding")
print("tuple:", favorite_activities)

#part 7: set
skills = {"walking", "running", "skipping"}
print("set:", skills)

#part 8: nonetype
new_skill = None
print("new skill: ", new_skill)

#part 9: float
activity_cost = 5
skill_cost = 10

total_cost = (len(activites) * activity_cost) + (len(skills) * skill_cost)
print(f"The total cost for all activities and skills is: ${total_cost}")
personal info dictionary {'full_name': 'Joanna Hu', 'years': '17', 'location:': 'San Diego', 'favorite_food': 'dumplings'}
some of my favorite activies are:  ['eating wingstop', 'roblox', 'sleeping']
{'full_name': 'Joanna Hu', 'years': '17', 'location:': 'San Diego', 'favorite_food': 'dumplings', 'activities': ['eating wingstop', 'roblox', 'sleeping']}
Is 'roblox' available to do right now? True
I have 3 activities.
tuple: ('homework', 'coding')
set: {'running', 'skipping', 'walking'}
new skill:  None
The total cost for all activities and skills is: $45

js hacks

//js popcorn hack 1

const Set1 = new Set([9,8,7]); 
console.log("Set1:", Set1);

const Set2 = new Set([6,5,4]);
console.log("Set2:", Set2);

Set1.add(7); // Adding number 7 to Set1
console.log("Set1 after adding a number:", Set1);

//Remove 1st number from Set1
const firstElement = Array.from(Set1)[0]; // Get the first element
Set1.delete(firstElement); // Remove the first element
console.log("Set1 after removing the first number:", Set1);

//Union
const unionSet = new Set([...Set1, ...Set2]);
console.log("Union of Set1 and Set2:", unionSet);
//js popcorn hack 2

const application = {
    name: "Joanna Hu", 
    age: 17,
    experiences: ["education", "swim"],
    money: 1000000 
};

//log dictionary
console.log("Application:", application);

//log only money property
console.log("Money:", application.money);

// js homework hack


function createApplication() {
    const name = prompt("What is your name?");
    const age = prompt("What is your age?");
    const experiences = prompt("Please list your experiences, separated with commas").split(",");

    //dictionary
    const application = {
        name: name,
        age: age,
        experiences: experiences.map(exp => exp.trim()), // Trim whitespace
    };

    // Log the user's input
    console.log("Application:", application);
}

// Call the function to create the application
createApplication();