3.1 Hacks | 3.2 Hacks | 3.3 Hacks | 3.4 Hacks | 3.5 Hacks | 3.8 Hacks | 3.10a Hacks | 3.10b Hacks | Sprint 2 Personal Learnings Blog | Sprint 2 Team Blog |
3.2 Hacks
Python and Javascript Popcorn and Homework Hacks for 3.2
Python Popcorn and Homework Hacks
#Popcorn Hack 1
set = {1,2,3,4,5}
print("Set is" + str(set))
set.add(6)
print("New Set: " + str(set))
set.remove(2)
print("New Set: " + str(set))
set_b = {7,8,9}
union = set.union(set_b)
print("Union Set and Set B:" + str(union))
#BONUS: ###########
print("Length of Set is " + str(len(set)))
set.clear()
Set is{1, 2, 3, 4, 5}
New Set: {1, 2, 3, 4, 5, 6}
New Set: {1, 3, 4, 5, 6}
Union Set and Set B:{1, 3, 4, 5, 6, 7, 8, 9}
Length of Set is 5
# Popcorn Hack 2
string = "Learning Python is not fun"
print(str(len(string)))
substring = string[9:16]
print(substring)
string_capital = string.upper()
print(string_capital)
replaced_string = string.replace("fun", "good")
print(replaced_string)
26
Python
LEARNING PYTHON IS NOT FUN
Learning Python is not good
# Popcorn Hack 3
list = [3, 5, 7, 9, 11]
print(list[2])
list[1]=6
print(list)
list.append(13)
list.remove(list[3])
print(list)
7
[3, 6, 7, 9, 11]
[3, 6, 7, 11, 13]
# Popcorn Hack 4
dictionary_personal = {
"name": "Risha",
"age": "15",
"favorite_subject": "math",
"grade": "sophomore"
}
print(dictionary_personal)
print("My name is " + dictionary_personal["name"])
print(len(dictionary_personal))
print(type(dictionary_personal))
{'name': 'Risha', 'age': '15', 'favorite_subject': 'math', 'grade': 'sophomore'}
My name is Risha
4
<class 'dict'>
# Homework Hack 1
personal_info = {
"full_name": "Risha Guha",
"years": 15,
"location": "San Diego",
"favorite_food": "pasta"
}
# Homework Hack 2
activities = ["piano", "cybersecurity", "badminton"]
print(activities)
# Homework Hack 3
personal_info["activities"] = activities
print(personal_info)
# Homework Hack 4
# Activity is Badminton
activity_available = False
print("Is badminton available today? " + str(activity_available))
# Homework Hack 5
total_activities = len(activities)
print("I have " + str(total_activities) + " activities.")
# Homework Hack 6
favorite_activites = ("piano", "badminton")
print(favorite_activites)
# Homework Hack 7
skills = {"logic", "math", "writing"}
print(skills)
# Homework Hack 8
new_skill = None
print(new_skill)
# Homework Hack 9
total_cost = 5*len(activities) + 10*len(skills)
print("total cost: $" + str(total_cost))
['piano', 'cybersecurity', 'badminton']
{'full_name': 'Risha Guha', 'years': 15, 'location': 'San Diego', 'favorite_food': 'pasta', 'activities': ['piano', 'cybersecurity', 'badminton']}
Is badminton available today? False
I have 3 activities.
('piano', 'badminton')
{'logic', 'math', 'writing'}
None
total cost: $45
Javascript Popcorn and Homework Hacks
%%javascript
/* Popcorn Hack 1 */
let set1 = new Set([1, 2, 3]);
let set2 = new Set([4, 5, 6]);
console.log("set1:", set1);
console.log("set2:", set2);
set1.add(7);
set1.delete(1);
let unionSet = new Set([...set1, ...set2]);
console.log("Union of set1 and set2:", unionSet);
<IPython.core.display.Javascript object>
%%javascript
/* Popcorn Hack 2 */
let application = {
name: "CS Kid",
age: 21,
experiences: ["Game Developer", "Intern", "Comp Sci Major"],
money: 34000
};
console.log("Application:", application);
console.log("Money:", application.money);
<IPython.core.display.Javascript object>
%%javascript
/* Homework Hack 1 */
function createApplication() {
let name = prompt("Name? ");
let age = prompt("Age?");
let experiences = prompt("List your experiences (separated by commas)");
let application = {
name: name,
age: age,
experiences: experiences.split(",")
};
console.log("Applicant's Information:", application);
}
createApplication();
<IPython.core.display.Javascript object>