Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 220 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,230 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#2. Create an empty dictionary called `inventory`.\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Number of t-shirt: 4\n",
"Number of mug: 5\n",
"Number of hat: 6\n",
"Number of book: 8\n",
"Number of keychain: 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 8, 'keychain': 9}\n"
]
}
],
"source": [
"# 3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"for item in products:\n",
" inventory[item] = int(input(\"Number of \" + item + \": \"))\n",
"print(inventory)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# 4. Create an empty set called `customer_orders`.\n",
"customer_orders = set ()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose a product: hat\n",
"Choose a product: book\n",
"Choose a product: tshirt\n"
]
}
],
"source": [
"# Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n",
"for i in range(3):\n",
" product = input(\"Choose a product: \")\n",
" customer_orders.add(product)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'tshirt', 'book', 'hat'}\n"
]
}
],
"source": [
"# 6. Print the products in the `customer_orders` set.\n",
"print(\"Customer orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order status: (3, 60.0)\n"
]
}
],
"source": [
"# 7. Calculate the following order statistics:\n",
" # - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
"total = len(customer_orders)\n",
"\n",
" #- Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"percentage = (total / len(products)) * 100\n",
"\n",
" #Store these statistics in a tuple called `order_status`.\n",
"order_status = (total, percentage)\n",
"\n",
"print(\"Order status:\", order_status)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0 %\n"
]
}
],
"source": [
"# 8. Print the order statistics using the following format:\n",
" #``` Order Statistics:\n",
" # Total Products Ordered: <total_products_ordered>\n",
" #Percentage of Products Ordered: <percentage_ordered>% \n",
"\n",
"print(\"Order Statistics:\")\n",
"print(\"Total Products Ordered:\", order_status[0])\n",
"print(\"Percentage of Products Ordered:\", order_status[1], \"%\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 5, 'hat': 5, 'book': 7, 'keychain': 9}\n"
]
}
],
"source": [
"# 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"for item in inventory:\n",
" if item in customer_orders:\n",
" inventory[item] -= 1\n",
"print (inventory) "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 4\n",
"mug: 5\n",
"hat: 5\n",
"book: 7\n",
"keychain: 9\n"
]
}
],
"source": [
"# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(\"Updated Inventory:\")\n",
"for item, quantity in inventory.items():\n",
" print(f\"{item}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# mudança qualquer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -68,7 +285,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down