Skip to the content.

Lessons 3.2 Hacks

3.2

Popcorn Hacks (Python)

3.2.7 Sets

Hack 1

set = {1, 2, 3, 4, 5}
print(set)

set.add(6)
print(set)

set.remove(2)
print(set)

new_set = set.union({7, 8, 9})
print(new_set)

set.clear()
print(set)

# bonus

other_set = {1, 2, 2, 3, 3, 4}
print(other_set)
print("Length", len(other_set))
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6}
{1, 3, 4, 5, 6}
{1, 3, 4, 5, 6, 7, 8, 9}
set()
{1, 2, 3, 4}
Length 4

3.2.3 Strings

Hack 2

string = "Learning Python is not fun"
print(string)

print("Length", len(string))

python_word = string[9:15]
print(python_word)

upper_string = string.upper()
print(upper_string)

replaced_string = string.replace("fun", "good")
print(replaced_string)

# bonus

reversed_string = string[::-1]
print(reversed_string)
Learning Python is not fun
Length 26
Python
LEARNING PYTHON IS NOT FUN
Learning Python is not good
nuf ton si nohtyP gninraeL

3.2.4 Lists

Hack 3

list = [3, 5, 7, 9, 11]
print(list)

third_element = list[2]
print(third_element)

list[1] = 6
print(list)

list.append(13)
print(list)

list.remove(9)
print(list)

# bonus 

list.sort(reverse=True)
print(list)
[3, 5, 7, 9, 11]
7
[3, 6, 7, 9, 11]
[3, 6, 7, 9, 11, 13]
[3, 6, 7, 11, 13]
[13, 11, 7, 6, 3]

3.2.6 Dictionaries

Hack 4

personal_info = {
    "name": "Maryam",
    "email": "coolkid@gmail.com",
    "phone_number": 858555555
}

print(personal_info)

print("My name is", personal_info["name"])

print("Length", len(personal_info))

print("Type", type(personal_info))

# bonus

new_info = {
    "name": "Barak Obama",
    "email": "46@gmail.com",
    "phone_number": 2024561111
}

print(new_info["name"])
print(new_info["email"])
print(new_info["phone_number"])
{'name': 'Maryam', 'email': 'coolkid@gmail.com', 'phone_number': 858555555}
My name is Maryam
Length 3
Type <class 'dict'>
Barak Obama
46@gmail.com
2024561111

Hacks (Python)

Hack 1: Create Personal Info (dict)

Create a dictionary called personal_info that includes the following keys. Assign values to the keys: full_name, years, location, and favorite_food. The full_name should be a string, years an integer, location a string, and favorite_food a string

personal_info2 = {
    "full_name": "Maryam Abdul-Aziz",
    "years": 7,
    "location": "San Diego",
    "favorite_food": "pasta",
}

print(personal_info2)
{'full_name': 'Maryam Abdul-Aziz', 'years': 7, 'location': 'San Diego', 'favorite_food': 'pasta'}

Hack 2: Create a List of Activities (list)

Create a list called activities that includes three of your favorite activities as strings. Print the activities list.

favorite_actitivies = ["reading", "writing", "listening to music"]

print(favorite_actitivies)
['reading', 'writing', 'listening to music']

Hack 3: Add Activities to Personal Info (dict and list)

Add the activities list to the personal_info dictionary under the key activities. Print the updated personal_info dictionary.

personal_info2["activities"] = favorite_actitivies

print(personal_info2)

{'full_name': 'Maryam Abdul-Aziz', 'years': 7, 'location': 'San Diego', 'favorite_food': 'pasta', 'activities': ['reading', 'writing', 'listening to music']}

Hack 4: Check Availability of an Activity (bool)

Choose one of your activities and create a boolean variable called activity_available. Set activity_available to True if your activity is available today, otherwise set it to False. Print a message using activity_available like: “Is «your activity» available today? «True/False»”

activity_available = True # i picked reading

print("Is reading available today?", activity_available)
Is reading available today? True

Hack 5: Total Number of Activities (int)

Create a variable called total_activities and set it to the number of activities in your activities list. Print a message like: “I have «total_activities» activities.”

total_activities = len(favorite_actitivies)

print(f"I have {total_activities} activities.")
I have 3 activities.

Hack 6: Favorite Activities (tuple)

Create a tuple called favorite_activities that contains your two most favorite activities. Print the favorite_activities tuple.

favorite_activities2 = ("reading", "writing")
print(favorite_activities2)
('reading', 'writing')

Hack 7: Add a New Set of Skills (set)

Create a set called skills and add three unique skills you have. Print the set of skills.

skills = {"coding", "baking", "writing"}
print(skills)
{'coding', 'baking', 'writing'}

Hack 8: Consider a New Skill (NoneType)

You are thinking about learning a new skill. Create a variable called new_skill and set it to None (you haven’t decided yet). Print the value of new_skill.

new_skill = None

print(new_skill)
None

Hack 9: Calculate Total Hobby and Skill Cost (float)

Assume each activity costs $5 to pursue and each skill costs $10 to develop. Create a variable called total_cost as a float, and calculate how much it would cost to develop all your activities and skills. Print the total cost.

total_cost = (len(favorite_actitivies) * 5) + (len(skills) * 10) 
print(f"The total cost is ${total_cost}")
The total cost is $45

Popcorn Hacks (JavaScript)

Hack 1 (SETS)

  • Create a set called [Set1] of 3 numbers (can be any number)

  • Create a set called [Set2] of different 3 numbers (can be any number)

  • log both sets separately

  • Add a number and Remove the 1st number from set1

  • log the union of both Set1 and Set2.

%%html
<script>
let Set1 = new Set([1, 2, 3]);
let Set2 = new Set([20, 45, 7]);

console.log("Set1:", Set1);
console.log("Set2:", Set2);

Set1.add(4);
Set1.delete(1);

let unionSet = new Set([...Set1, ...Set2]);
console.log("Union of Set1 and Set2:", unionSet);

</script>

Hack 2 (Dictionaries):

  • Create a Dictionary called “application” that has someones “name”, “age”, “experiences”, and “money”.

  • log the dictionary to make sure it works

  • log only the dictionarie’s money

%%html
<script>
let application = {
    name: "Kushi Gade",
    age: 47,
    experiences: ["coder", "history lover", "follower"],
    money: 5000
};

console.log(application);
console.log("Money:", application.money);
</script>

Hack (JavaScript)

  • Make an application for an applicant, ask them questions about their name, age, and experiences.

  • Store these inputs into a Dictionary, sorting each of their responses into “name”, “age”, and “experiences”

  • Log the users input

%%html
<script>
let application = {};

application.name = prompt("What is your name?");
application.age = prompt("How old are you?");
application.experience = prompt("Please list your experiences (separated by commas)").split(",");

console.log("Application summary:", application);

</script>