students = {"Alejandro": [70, 85]} def add_scores(): score_list = [] score = 0 while score >= 0: score = 80 if (score > 0): score_list.append(score) #this is only here to exit the while loop score = -1 return score_list def add_student(): student_name = "Bianca" students[student_name] = add_scores() add_student() print(f"students: {students}")
students: {'Alejandro': [70, 85], 'Bianca': [80]}
# Products and their prices fruits = { 'mango': 100, 'watermelon': 200, 'strawberry': 500 } # Product quantity, I guess this should be input from user, here we are just # using a hardcoded/fixed value for this example quantity = 3 # Accumulator for total price total_price = 0 # Iterate through the dictionary, only key and values for key, value in fruits.items(): print(f"{key}: ₡{value}") # Iterate through the dictionary, multiplying by quantity for key, value in fruits.items(): total_price_for_item = quantity * value print(f"{key}: ₡{value}, total for {quantity} items: ₡{total_price_for_item}") total_price = total_price + total_price_for_item print(f'The total price is: ₡{total_price}')
mango: ₡100 watermelon: ₡200 strawberry: ₡500 mango: ₡100, total for 3 items: ₡300 watermelon: ₡200, total for 3 items: ₡600 strawberry: ₡500, total for 3 items: ₡1500 The total price is: ₡2400
def myCustomSortKeyFunction(e): return e['lastname'] people = [ {'id': '212341234', 'name': 'Wendy', 'lastname': 'Jimenez'}, {'id': '112341234', 'name': 'Bianca', 'lastname': 'Bianca'}, {'id': '901230123', 'name': 'Alejandro', 'lastname': 'Alvarado'} ] people.sort(key=myCustomSortKeyFunction) print(people)
[{'id': '901230123', 'name': 'Alejandro', 'lastname': 'Alvarado'}, {'id': '112341234', 'name': 'Bianca', 'lastname': 'Bianca'}, {'id': '212341234', 'name': 'Wendy', 'lastname': 'Jimenez'}]
how to define python dictionary of key value
# To define a Python dictionary with key-value pairs, you can simply use curly b # races `{}` and separate keys from values using a colon `:`. Here's an example: # Example of defining a dictionary my_dict = { 'name': 'Alice', 'age': 30, 'city': 'New York' } print(my_dict) # This code creates a dictionary named `my_dict` with three key-value pairs and # then prints the dictionary.
{'name': 'Alice', 'age': 30, 'city': 'New York'}
how to iterate thru a value pair dictionary
# To iterate through a key-value pair dictionary in Python, you can use a for lo # op. # Here's an example: my_dict = { 'name': 'Alice', 'age': 30, 'city': 'New York' } # Iterate through the dictionary for key, value in my_dict.items(): print(f"{key}: {value}")
name: Alice age: 30 city: New York