Homework and Popcorn Hacks for 3.2
Homeworks and Popcorn Hacks
Popcorn Hack 1 - Dictionaries
fruits = {1: "Bananas", 2: "Apples", 3: "Pears"}
key_to_access = 1
print(f"Fruit for key {key_to_access}: {fruits[key_to_access]}")
Fruit for key 1: Bananas
Popcorn Hack 2 - Calculator
firstNumber = int(input("Please Enter the First Number Here: "))
secondNumber = int(input("Please Enter the Second Number Here: "))
mathFunction = input("Please Enter the function here (+, -, *, /): ")
operations = {
"+": firstNumber + secondNumber,
"-": firstNumber - secondNumber,
"*": firstNumber * secondNumber,
"/": firstNumber / secondNumber if secondNumber != 0 else "Error: Division by zero"
}
result = operations.get(mathFunction, "Error: Invalid operation.")
print(result)
3
Popcorn Hack 3 - Returning elements a list
def repeat_strings_in_list(strings, n):
result = [] # Creating array
for string in strings:
result.append(string * n) # Repeating the string `n` times
return result
string_list = ["cars", "bikes", "aweosme"]
print(repeat_strings_in_list(string_list, 3))
['carscarscars', 'bikesbikesbikes', 'aweosmeaweosmeaweosme']
Popcorn Hack 4 - Comparison of Sets
set1 = {1,2,3}
set2 = {3,4,5}
def simsets(s1,s2):
similar = False
for i in set1:
for j in set2:
if i == j:
similar = True
return similar
print(simsets(set1, set2))
True
Homework 1,2,3,4
Part 1 - Dictionary with Specified Keys
profile = {
"name": "Darsh Darsh",
"age": 17,
"city": "San Diego",
"favorite_color": "Purple"
}
print(profile)
{'name': 'Darsh Darsh', 'age': 17, 'city': 'San Diego', 'favorite_color': 'Purple'}
Part 2 - Creating a list of hobbies
hobbies = ["Studying", "Badminton", "Biking"]
print("Hobbies:", hobbies)
Hobbies: ['Studying', 'Badminton', 'Biking']
Part 3 - Adding Hobbies to to Profile
profile["hobbies"] = hobbies
print("Profile updated successfully:", profile)
Profile updated successfully: {'name': 'Darsh Darsh', 'age': 17, 'city': 'San Diego', 'favorite_color': 'Purple', 'hobbies': ['Studying', 'Badminton', 'Biking']}
Part 4 - Checking the Availability of a Hobby
has_hobby = True # or False
print(f"Is the hobby '{hobbies[0]}' available today? {'Yes' if has_hobby else 'No'}")
Is the hobby 'Studying' available today? Yes
Part 5 - Total number of Hobbies
total_hobbies = len(hobbies)
print(f"I currently have a total of {total_hobbies} hobbies.")
I currently have a total of 3 hobbies.
Part 6 - Favorite Hobbies
favorite_hobbies = ("Badminton", "Biking")
print("My favorite hobbies are:", favorite_hobbies)
My favorite hobbies are: ('Badminton', 'Biking')
Part 7 - Add a new item to your profile
skills = ["Coding", "Painting", "Cooking"]
print("My skills include:", skills)
My skills include: ['Coding', 'Painting', 'Cooking']
Part 8 - Add a new skill
new_skill = None
print("New skill to learn:", new_skill)
New skill to learn: None
Part 9 - Calculate Total Cost
total_cost = float(total_hobbies * 5 + len(skills) * 10)
print(f"Total cost for pursuing hobbies and skills: ${total_cost:.2f}")
Total cost for pursuing hobbies and skills: $45.00