3.2 Homework and Popcorn Hacks
3.2 Homework and Popcorn Hacks
Popcorn Hack 1
## Step 1: Create a Set
# Create a set: {1, 2, 3, 4, 5}.
my_set = {1, 2, 3, 4, 5}
## Step 2: Add an Element
# Add 6 to the set.
my_set.add(6)
print(my_set)
## Step 3: Remove an Element
# Remove 2 from the set.
my_set.remove(2)
print(my_set)
## Step 4: Union of Sets
# Union with {7, 8, 9}.
my_set = my_set.union({7, 8, 9})
print(my_set)
## Step 5: Clear the Set
# Clear the set.
my_set.clear()
print(my_set)
## Bonus
# Create a set with duplicates: {1, 2, 2, 3, 3, 4}.
new_set = {1, 2, 2, 3, 3, 4}
print(new_set)
# Find the length with len().
print(len(new_set))
{1, 2, 3, 4, 5, 6}
{1, 3, 4, 5, 6}
{1, 3, 4, 5, 6, 7, 8, 9}
set()
{1, 2, 3, 4}
4
Popcorn Hack 2
# Step 1: Create a String
# Create a string: "Learning Python is not fun".
my_string = "Learning Python is not fun"
# Step 2: String Length
# Find the length of the string using len().
print(len(my_string))
# Step 3: String Slicing
# Extract "Python" from the string using slicing.
print(my_string[9:15])
# Step 4: String Uppercase
# Convert the string to all uppercase letters.
print(my_string.upper())
# Step 5: String Replace
# Replace "fun" with "good".
print(my_string.replace("fun", "good"))
# Bonus
# Reverse the string using slicing.
print(my_string[::-1])
26
Python
LEARNING PYTHON IS NOT FUN
Learning Python is not good
nuf ton si nohtyP gninraeL
Popcorn Hack 3
## Step 1: Create a List
# Create a list of numbers: [3, 5, 7, 9, 11].
my_list = [3, 5, 7, 9, 11]
## Step 2: Access an Element
# Access the third element of the list.
print(my_list[2])
## Step 3: Modify an Element
# Change the second element to 6.
my_list[1] = 6
print(my_list)
## Step 4: Add an Element
# Append 13 to the list.
my_list.append(13)
print(my_list)
## Step 5: Remove an Element
# Remove the element 9 from the list.
my_list.remove(9)
print(my_list)
## Bonus
# Sort the list in descending order.
my_list.sort(reverse=True)
print(my_list)
7
[3, 6, 7, 9, 11]
[3, 6, 7, 9, 11, 13]
[3, 6, 7, 11, 13]
[13, 11, 7, 6, 3]
Popcorn Hack 4
## Step 1: Create a Dictionary
# Create a dictionary called personal_info.
personal_info = {
"name": "John Doe",
"email": "john@gmail.com",
"phone number": "123-456-7890"
}
## Step 2: Print out the Dictionary
# Print the entire dictionary.
print(personal_info)
## Step 3: Print out the name from your dictionary
# Print the name from the dictionary.
print("My Name is:", personal_info["name"])
## Step 4: Print the Length
# Print the length of the dictionary.
print(len(personal_info))
## Step 5: Print the Type
# Print the type of the dictionary.
print(type(personal_info))
## Bonus
# Create another dictionary called personal_info_2.
personal_info_2 = {
"name": "Jane Doe",
"email": "jane@gmail.com",
"phone number": "987-654-3210"
}
# Print the name from the second dictionary.
print("My Name is:", personal_info_2["name"])
print(personal_info_2)
{'name': 'John Doe', 'email': 'john@gmail.com', 'phone number': '123-456-7890'}
My Name is: John Doe
3
<class 'dict'>
My Name is: Jane Doe
{'name': 'Jane Doe', 'email': 'jane@gmail.com', 'phone number': '987-654-3210'}
Homework Hacks
Python
## Part 1: Create Personal Info (dict)
# Create a dictionary called personal_info.
personal_info = {
"full_name": "John Doe",
"years": 30,
"location": "New York",
"favorite_food": "Pizza"
}
print(personal_info)
## Part 2: Create a List of Activities (list)
# Create a list called activities.
activities = ["Biking", "Coding", "Gaming"]
print(activities)
## Part 3: Add Activities to Personal Info (dict and list)
# Add the activities list to the personal_info dictionary.
personal_info["activities"] = activities
print(personal_info)
## Part 4: Check Availability of an Activity (bool)
# Create a boolean variable called activity_available.
activity_available = True
print("Is Biking available today?", activity_available)
## Part 5: Total Number of Activities (int)
# Create a variable called total_activities.
total_activities = len(activities)
print("I have", total_activities, "activities.")
## Part 6: Favorite Activities (tuple)
# Create a tuple called favorite_activities.
favorite_activities = ("Biking", "Coding")
print(favorite_activities)
## Part 7: Add a New Set of Skills (set)
# Create a set called skills.
skills = {"Python", "JavaScript", "HTML"}
print
## Part 8: Consider a New Skill (NoneType)
# Create a variable called new_skill.
new_skill = None
print(new_skill)
## Part 9: Calculate Total Hobby and Skill Cost (float)
# Calculate the total cost.
total_cost = total_activities * 5 + len(skills) * 10
print(total_cost)
{'full_name': 'John Doe', 'years': 30, 'location': 'New York', 'favorite_food': 'Pizza'}
['Biking', 'Coding', 'Gaming']
{'full_name': 'John Doe', 'years': 30, 'location': 'New York', 'favorite_food': 'Pizza', 'activities': ['Biking', 'Coding', 'Gaming']}
Is Biking available today? True
I have 3 activities.
('Biking', 'Coding')
None
45
JavaScript
%% js
// Make an application for an applicant, ask them questions about their name, age, and experiences.
var applicant = {
name: prompt("What is your name?"),
age: prompt("How old are you?"),
experiences: prompt("What are your experiences?")
};
// Store these inputs into a Dictionary, sorting each of their responses into “name”, “age”, and “experiences”
var applicantDictionary = {
name: applicant.name,
age: applicant.age,
experiences: applicant.experiences
};
// Log the users input
console.log(applicantDictionary);
Javascript Popcorn Hacks
Sets
%%js
// Create a set called [Set1] of 3 numbers (can be any number)
const Set1 = new Set([1, 2, 3]);
// Create a set called [Set2] of different 3 numbers (can be any number)
const Set2 = new Set([4, 5, 6]);
// log both sets separately
console.log(Set1);
console.log(Set2);
// Add a number and Remove the 1st number from set1
Set1.add(8);
Set1.delete(1);
// log the union of both Set1 and Set2.
const union = new Set([...Set1, ...Set2]);
console.log(union);
Dictionaries
%%js
// Create a dictionary called “application” that has someones “name”, “age”, “experiences”, and “money”.
application = {
name: "John",
age: 30,
experiences: "Software Engineer",
money: 1000
}
// log the dictionary to make sure it works
console.log(application);
// log only the dictionary's money
console.log(application.money);
<IPython.core.display.Javascript object>