From bb4800c097d838c1d33808a5f55bb989e20209d0 Mon Sep 17 00:00:00 2001 From: Axel Barck-Holst <102673853+AxelHolst@users.noreply.github.com> Date: Fri, 12 Jul 2024 10:57:59 +0200 Subject: [PATCH] Complete Medical Insurance Project: Patient data storage, updates, and additional functionalities --- ...onaries Medical Insurance-checkpoint.ipynb | 457 ++++++++++++ ...ython Dictionaries Medical Insurance.ipynb | 656 ++++++++++++++++++ 2 files changed, 1113 insertions(+) create mode 100644 Python Dictionaries Medical Insurance/.ipynb_checkpoints/Python Dictionaries Medical Insurance-checkpoint.ipynb create mode 100644 Python Dictionaries Medical Insurance/Python Dictionaries Medical Insurance.ipynb diff --git a/Python Dictionaries Medical Insurance/.ipynb_checkpoints/Python Dictionaries Medical Insurance-checkpoint.ipynb b/Python Dictionaries Medical Insurance/.ipynb_checkpoints/Python Dictionaries Medical Insurance-checkpoint.ipynb new file mode 100644 index 0000000..83893fd --- /dev/null +++ b/Python Dictionaries Medical Insurance/.ipynb_checkpoints/Python Dictionaries Medical Insurance-checkpoint.ipynb @@ -0,0 +1,457 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "506b51bc", + "metadata": {}, + "source": [ + "# Python Dictionaries: Medical Insurance Project" + ] + }, + { + "cell_type": "markdown", + "id": "862d76b9", + "metadata": {}, + "source": [ + "You have been asked to create a program that organizes and updates medical records efficiently.\n", + "\n", + "In this project, you will use your new knowledge of Python dictionaries to create a database of medical records for patients.\n", + "\n", + "Let's get started!" + ] + }, + { + "cell_type": "markdown", + "id": "269b7e44", + "metadata": {}, + "source": [ + "## Storing Patient Names and Insurance Costs" + ] + }, + { + "cell_type": "markdown", + "id": "ca586fc7", + "metadata": {}, + "source": [ + "1. We would like to keep a record of medical patients and their insurance costs.\n", + "\n", + " First, create an empty dictionary called `medical_costs`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6df17a2", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "e06605b6", + "metadata": {}, + "source": [ + "2. Let's populate our `medical_costs` dictionary by adding the following key-value pairs:\n", + " - Add `\"Marina\"` to `medical_costs` as a key with a value of `6607.0`.\n", + " - Add `\"Vinay\"` to `medical_costs` as a key with a value of `3225.0`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dec12f6a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "a093528d", + "metadata": {}, + "source": [ + "3. Using one line of code, add the following three patients to the `medical_costs` dictionary:\n", + " - `\"Connie\"`, with an insurance cost of `8886.0`.\n", + " - `\"Isaac\"`, with an insurance cost of `16444.0`.\n", + " - `\"Valentina\"`, with an insurance cost of `6420.0`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "015d4658", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "9e2e45fa", + "metadata": {}, + "source": [ + "4. Print `medical_costs`. Make sure the dictionary is what you expected." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b58435b", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "e79e7ee3", + "metadata": {}, + "source": [ + "5. You notice that `Vinay`'s insurance cost was incorrectly inputted. Update the value associated with `Vinay` to `3325.0`.\n", + "\n", + " Print the updated dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b484f83", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "36d43988", + "metadata": {}, + "source": [ + "6. Let's calculate the average medical cost of each patient. Create a variable called `total_cost` and set it equal to 0.\n", + "\n", + " Next, iterate through the values in `medical_costs` and add each value to the `total_cost` variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef636a71", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "0dc5e0ba", + "metadata": {}, + "source": [ + "7. After the loop, create a variable called `average_cost` that stores the `total_cost` divided by the length of the `medical_costs` dictionary.\n", + "\n", + " Print `average_cost` with the following message:\n", + " \n", + " ```\n", + " Average Insurance Cost: {average_cost}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a6d83515", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "0f603e5b", + "metadata": {}, + "source": [ + "## List Comprehension to Dictionary" + ] + }, + { + "cell_type": "markdown", + "id": "b6e9f6f6", + "metadata": {}, + "source": [ + "8. You have been asked to create a second dictionary that maps patient names to their ages.\n", + "\n", + " First, create two lists called `names` and `ages` with the following data:\n", + " \n", + " names | ages\n", + " --- | ---\n", + " Marina | 27\n", + " Vinay | 24\n", + " Connie | 43\n", + " Isaac | 35\n", + " Valentina | 52" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88db2876", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "d213ceb2", + "metadata": {}, + "source": [ + "9. Next, create a variable called `zipped_ages` that is a zipped list of pairs between the `names` list and the `ages` list." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eddb66ba", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "d0df86ec", + "metadata": {}, + "source": [ + "10. Create a dictionary called `names_to_ages` by using a list comprehension that iterates through `zipped_ages` and turns each pair into a key : value item.\n", + "\n", + " Print `names_to_ages` to see the result." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc760b49", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "cb598e55", + "metadata": {}, + "source": [ + "11. Use `.get()` to get the value of Marina's age and store it in a variable called `marina_age`. Use `None` as a default value if the key doesn't exist.\n", + "\n", + " Print `marina_age` with the following message:\n", + " \n", + " ```\n", + " Marina's age is {marina_age}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7c9e288", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "b96b80f0", + "metadata": {}, + "source": [ + "## Using a Dictionary to Create a Medical Database" + ] + }, + { + "cell_type": "markdown", + "id": "4d1e02cf", + "metadata": {}, + "source": [ + "12. Let's create a third dictionary to represent a database of medical records that contains information such as a patient's name, age, sex, gender, BMI, number of children, smoker status, and insurance cost.\n", + "\n", + " First, create an empty dictionary called `medical_records`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "768401f1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "a035c911", + "metadata": {}, + "source": [ + "13. Next, add `\"Marina\"` to `medical_records` as a key with the value being a dictionary of medical data:\n", + "\n", + " ```py\n", + " {\"Age\": 27, \"Sex\": \"Female\", \"BMI\": 31.1, \"Children\": 2, \"Smoker\": \"Non-smoker\", \"Insurance_cost\": 6607.0}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f4a0390", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "739bac2e", + "metadata": {}, + "source": [ + "14. Do the same for the following individuals:\n", + " \n", + " Name | Age | Sex | BMI | Children | Smoker | Insurance Cost\n", + " --- | --- | --- | --- | --- | --- | ---\n", + " Vinay | 24 | Male | 26.9 | 0 | Non-smoker | 3225.0\n", + " Connie | 43 | Female | 25.3 | 3 | Non-smoker | 8886.0\n", + " Isaac | 35 | Male | 20.6 | 4 | Smoker | 16444.0\n", + " Valentina | 52 | Female | 18.7 | 1 | Non-smoker | 6420.0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55de66f6", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "fbe83204", + "metadata": {}, + "source": [ + "15. Print `medical_records` to see the result." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88c952f7", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "96f5661c", + "metadata": {}, + "source": [ + "16. The `medical_records` dictionary acts like a database of medical records. Let's access a specific piece of data in `medical_records`.\n", + "\n", + " Print out `Connie`'s insurance cost with the following message:\n", + " \n", + " ```\n", + " Connie's insurance cost is X dollars.\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2eb86872", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "9b33384f", + "metadata": {}, + "source": [ + "17. `Vinay` has moved to a new country, and we no longer want to include him in our medical records.\n", + "\n", + " Remove `Vinay` from `medical_records`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eeeb5b8d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "0cde5d29", + "metadata": {}, + "source": [ + "18. Let's take a closer look at each patient's medical record.\n", + "\n", + " Use a `for` loop to iterate through the items of `medical_records`. For each key-value pair, print out a string that looks like the following:\n", + " \n", + " ```\n", + " {Name} is a {Age} year old {Sex} {Smoker} with a BMI of {BMI} and insurance cost of {Insurance_cost}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39301cd2", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "087a263c", + "metadata": {}, + "source": [ + "## Extra" + ] + }, + { + "cell_type": "markdown", + "id": "8e3067e6", + "metadata": {}, + "source": [ + "19. Congratulations! In this project, you used Python dictionaries to store and update medical data for individuals.\n", + "\n", + " If you'd like extra practice with dictionaries, here are some suggestions to go further with this project:\n", + " - Create a function called `update_medical_records()` that takes in the name of an individual as well as their medical data, and then updates the `medical_records` dictionary accordingly.\n", + " - Create a new dictionary of your choice - feel free to get creative!\n", + " \n", + " Happy coding!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac494987", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4195bad7", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Python Dictionaries Medical Insurance/Python Dictionaries Medical Insurance.ipynb b/Python Dictionaries Medical Insurance/Python Dictionaries Medical Insurance.ipynb new file mode 100644 index 0000000..4f50553 --- /dev/null +++ b/Python Dictionaries Medical Insurance/Python Dictionaries Medical Insurance.ipynb @@ -0,0 +1,656 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "506b51bc", + "metadata": {}, + "source": [ + "# Python Dictionaries: Medical Insurance Project" + ] + }, + { + "cell_type": "markdown", + "id": "862d76b9", + "metadata": {}, + "source": [ + "You have been asked to create a program that organizes and updates medical records efficiently.\n", + "\n", + "In this project, you will use your new knowledge of Python dictionaries to create a database of medical records for patients.\n", + "\n", + "Let's get started!" + ] + }, + { + "cell_type": "markdown", + "id": "269b7e44", + "metadata": {}, + "source": [ + "## Storing Patient Names and Insurance Costs" + ] + }, + { + "cell_type": "markdown", + "id": "ca586fc7", + "metadata": {}, + "source": [ + "1. We would like to keep a record of medical patients and their insurance costs.\n", + "\n", + " First, create an empty dictionary called `medical_costs`." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e6df17a2", + "metadata": {}, + "outputs": [], + "source": [ + "medical_costs = {}" + ] + }, + { + "cell_type": "markdown", + "id": "e06605b6", + "metadata": {}, + "source": [ + "2. Let's populate our `medical_costs` dictionary by adding the following key-value pairs:\n", + " - Add `\"Marina\"` to `medical_costs` as a key with a value of `6607.0`.\n", + " - Add `\"Vinay\"` to `medical_costs` as a key with a value of `3225.0`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "dec12f6a", + "metadata": {}, + "outputs": [], + "source": [ + "medical_costs[\"Marina\"] = 6607.0\n", + "medical_costs[\"Vinay\"] = 3225.0\n" + ] + }, + { + "cell_type": "markdown", + "id": "a093528d", + "metadata": {}, + "source": [ + "3. Using one line of code, add the following three patients to the `medical_costs` dictionary:\n", + " - `\"Connie\"`, with an insurance cost of `8886.0`.\n", + " - `\"Isaac\"`, with an insurance cost of `16444.0`.\n", + " - `\"Valentina\"`, with an insurance cost of `6420.0`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "015d4658", + "metadata": {}, + "outputs": [], + "source": [ + "medical_costs.update({\"Connie\": 8886.0, \"Isaac\": 16444.0, \"Valentina\": 6420.0})\n" + ] + }, + { + "cell_type": "markdown", + "id": "9e2e45fa", + "metadata": {}, + "source": [ + "4. Print `medical_costs`. Make sure the dictionary is what you expected." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "8b58435b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Marina': 6607.0, 'Vinay': 3225.0, 'Connie': 8886.0, 'Isaac': 16444.0, 'Valentina': 6420.0}\n" + ] + } + ], + "source": [ + "print(medical_costs)\n" + ] + }, + { + "cell_type": "markdown", + "id": "e79e7ee3", + "metadata": {}, + "source": [ + "5. You notice that `Vinay`'s insurance cost was incorrectly inputted. Update the value associated with `Vinay` to `3325.0`.\n", + "\n", + " Print the updated dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "8b484f83", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Marina': 6607.0, 'Vinay': 3325.0, 'Connie': 8886.0, 'Isaac': 16444.0, 'Valentina': 6420.0}\n" + ] + } + ], + "source": [ + "medical_costs[\"Vinay\"] = 3325.0\n", + "print(medical_costs)" + ] + }, + { + "cell_type": "markdown", + "id": "36d43988", + "metadata": {}, + "source": [ + "6. Let's calculate the average medical cost of each patient. Create a variable called `total_cost` and set it equal to 0.\n", + "\n", + " Next, iterate through the values in `medical_costs` and add each value to the `total_cost` variable." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ef636a71", + "metadata": {}, + "outputs": [], + "source": [ + "total_cost = 0\n", + "for cost in medical_costs.values():\n", + " total_cost += cost\n" + ] + }, + { + "cell_type": "markdown", + "id": "0dc5e0ba", + "metadata": {}, + "source": [ + "7. After the loop, create a variable called `average_cost` that stores the `total_cost` divided by the length of the `medical_costs` dictionary.\n", + "\n", + " Print `average_cost` with the following message:\n", + " \n", + " ```\n", + " Average Insurance Cost: {average_cost}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "a6d83515", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average Insurance Cost: 8336.4\n" + ] + } + ], + "source": [ + "average_cost = total_cost / len(medical_costs)\n", + "print(\"Average Insurance Cost: \" + str(average_cost))\n" + ] + }, + { + "cell_type": "markdown", + "id": "0f603e5b", + "metadata": {}, + "source": [ + "## List Comprehension to Dictionary" + ] + }, + { + "cell_type": "markdown", + "id": "b6e9f6f6", + "metadata": {}, + "source": [ + "8. You have been asked to create a second dictionary that maps patient names to their ages.\n", + "\n", + " First, create two lists called `names` and `ages` with the following data:\n", + " \n", + " names | ages\n", + " --- | ---\n", + " Marina | 27\n", + " Vinay | 24\n", + " Connie | 43\n", + " Isaac | 35\n", + " Valentina | 52" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "88db2876", + "metadata": {}, + "outputs": [], + "source": [ + "names = [\"Marina\", \"Vinay\", \"Connie\", \"Isaac\", \"Valentina\"]\n", + "ages = [27, 24, 43, 35, 52]\n" + ] + }, + { + "cell_type": "markdown", + "id": "d213ceb2", + "metadata": {}, + "source": [ + "9. Next, create a variable called `zipped_ages` that is a zipped list of pairs between the `names` list and the `ages` list." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "eddb66ba", + "metadata": {}, + "outputs": [], + "source": [ + "zipped_ages = zip(names, ages)\n" + ] + }, + { + "cell_type": "markdown", + "id": "d0df86ec", + "metadata": {}, + "source": [ + "10. Create a dictionary called `names_to_ages` by using a list comprehension that iterates through `zipped_ages` and turns each pair into a key : value item.\n", + "\n", + " Print `names_to_ages` to see the result." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "dc760b49", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Marina': 27, 'Vinay': 24, 'Connie': 43, 'Isaac': 35, 'Valentina': 52}\n" + ] + } + ], + "source": [ + "names_to_ages = {key: value for key, value in zipped_ages}\n", + "print(names_to_ages)\n" + ] + }, + { + "cell_type": "markdown", + "id": "cb598e55", + "metadata": {}, + "source": [ + "11. Use `.get()` to get the value of Marina's age and store it in a variable called `marina_age`. Use `None` as a default value if the key doesn't exist.\n", + "\n", + " Print `marina_age` with the following message:\n", + " \n", + " ```\n", + " Marina's age is {marina_age}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "a7c9e288", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Marina's age is 27\n" + ] + } + ], + "source": [ + "marina_age = names_to_ages.get(\"Marina\", None)\n", + "print(\"Marina's age is \" + str(marina_age))\n" + ] + }, + { + "cell_type": "markdown", + "id": "b96b80f0", + "metadata": {}, + "source": [ + "## Using a Dictionary to Create a Medical Database" + ] + }, + { + "cell_type": "markdown", + "id": "4d1e02cf", + "metadata": {}, + "source": [ + "12. Let's create a third dictionary to represent a database of medical records that contains information such as a patient's name, age, sex, gender, BMI, number of children, smoker status, and insurance cost.\n", + "\n", + " First, create an empty dictionary called `medical_records`." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "768401f1", + "metadata": {}, + "outputs": [], + "source": [ + "medical_records = {}" + ] + }, + { + "cell_type": "markdown", + "id": "a035c911", + "metadata": {}, + "source": [ + "13. Next, add `\"Marina\"` to `medical_records` as a key with the value being a dictionary of medical data:\n", + "\n", + " ```py\n", + " {\"Age\": 27, \"Sex\": \"Female\", \"BMI\": 31.1, \"Children\": 2, \"Smoker\": \"Non-smoker\", \"Insurance_cost\": 6607.0}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "5f4a0390", + "metadata": {}, + "outputs": [], + "source": [ + "medical_records[\"Marina\"] = {\"Age\": 27, \"Sex\": \"Female\", \"BMI\": 31.1, \"Children\": 2, \"Smoker\": \"Non-smoker\", \"Insurance_cost\": 6607.0}\n" + ] + }, + { + "cell_type": "markdown", + "id": "739bac2e", + "metadata": {}, + "source": [ + "14. Do the same for the following individuals:\n", + " \n", + " Name | Age | Sex | BMI | Children | Smoker | Insurance Cost\n", + " --- | --- | --- | --- | --- | --- | ---\n", + " Vinay | 24 | Male | 26.9 | 0 | Non-smoker | 3225.0\n", + " Connie | 43 | Female | 25.3 | 3 | Non-smoker | 8886.0\n", + " Isaac | 35 | Male | 20.6 | 4 | Smoker | 16444.0\n", + " Valentina | 52 | Female | 18.7 | 1 | Non-smoker | 6420.0" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "55de66f6", + "metadata": {}, + "outputs": [], + "source": [ + "medical_records[\"Vinay\"] = {\"Age\": 24, \"Sex\": \"Male\", \"BMI\": 26.9, \"Children\": 0, \"Smoker\": \"Non-smoker\", \"Insurance_cost\": 3225.0}\n", + "medical_records[\"Connie\"] = {\"Age\": 43, \"Sex\": \"Female\", \"BMI\": 25.3, \"Children\": 3, \"Smoker\": \"Non-smoker\", \"Insurance_cost\": 8886.0}\n", + "medical_records[\"Isaac\"] = {\"Age\": 35, \"Sex\": \"Male\", \"BMI\": 20.6, \"Children\": 4, \"Smoker\": \"Smoker\", \"Insurance_cost\": 16444.0}\n", + "medical_records[\"Valentina\"] = {\"Age\": 52, \"Sex\": \"Female\", \"BMI\": 18.7, \"Children\": 1, \"Smoker\": \"Non-smoker\", \"Insurance_cost\": 6420.0}\n" + ] + }, + { + "cell_type": "markdown", + "id": "fbe83204", + "metadata": {}, + "source": [ + "15. Print `medical_records` to see the result." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "88c952f7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Marina': {'Age': 27, 'Sex': 'Female', 'BMI': 31.1, 'Children': 2, 'Smoker': 'Non-smoker', 'Insurance_cost': 6607.0}, 'Vinay': {'Age': 24, 'Sex': 'Male', 'BMI': 26.9, 'Children': 0, 'Smoker': 'Non-smoker', 'Insurance_cost': 3225.0}, 'Connie': {'Age': 43, 'Sex': 'Female', 'BMI': 25.3, 'Children': 3, 'Smoker': 'Non-smoker', 'Insurance_cost': 8886.0}, 'Isaac': {'Age': 35, 'Sex': 'Male', 'BMI': 20.6, 'Children': 4, 'Smoker': 'Smoker', 'Insurance_cost': 16444.0}, 'Valentina': {'Age': 52, 'Sex': 'Female', 'BMI': 18.7, 'Children': 1, 'Smoker': 'Non-smoker', 'Insurance_cost': 6420.0}}\n" + ] + } + ], + "source": [ + "print(medical_records)\n" + ] + }, + { + "cell_type": "markdown", + "id": "96f5661c", + "metadata": {}, + "source": [ + "16. The `medical_records` dictionary acts like a database of medical records. Let's access a specific piece of data in `medical_records`.\n", + "\n", + " Print out `Connie`'s insurance cost with the following message:\n", + " \n", + " ```\n", + " Connie's insurance cost is X dollars.\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "2eb86872", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connie's insurance cost is 8886.0 dollars.\n" + ] + } + ], + "source": [ + "print(\"Connie's insurance cost is \" + str(medical_records[\"Connie\"][\"Insurance_cost\"]) + \" dollars.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "9b33384f", + "metadata": {}, + "source": [ + "17. `Vinay` has moved to a new country, and we no longer want to include him in our medical records.\n", + "\n", + " Remove `Vinay` from `medical_records`." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "eeeb5b8d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Age': 24,\n", + " 'Sex': 'Male',\n", + " 'BMI': 26.9,\n", + " 'Children': 0,\n", + " 'Smoker': 'Non-smoker',\n", + " 'Insurance_cost': 3225.0}" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "medical_records.pop(\"Vinay\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "0cde5d29", + "metadata": {}, + "source": [ + "18. Let's take a closer look at each patient's medical record.\n", + "\n", + " Use a `for` loop to iterate through the items of `medical_records`. For each key-value pair, print out a string that looks like the following:\n", + " \n", + " ```\n", + " {Name} is a {Age} year old {Sex} {Smoker} with a BMI of {BMI} and insurance cost of {Insurance_cost}\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "39301cd2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Marina is a 27 year old Female Non-smoker with a BMI of 31.1 and insurance cost of 6607.0\n", + "Connie is a 43 year old Female Non-smoker with a BMI of 25.3 and insurance cost of 8886.0\n", + "Isaac is a 35 year old Male Smoker with a BMI of 20.6 and insurance cost of 16444.0\n", + "Valentina is a 52 year old Female Non-smoker with a BMI of 18.7 and insurance cost of 6420.0\n" + ] + } + ], + "source": [ + "for name, record in medical_records.items():\n", + " print(name + \" is a \" + str(record[\"Age\"]) + \\\n", + " \" year old \" + record[\"Sex\"] + \" \" + record[\"Smoker\"] \\\n", + " + \" with a BMI of \" + str(record[\"BMI\"]) + \\\n", + " \" and insurance cost of \" + str(record[\"Insurance_cost\"]))\n" + ] + }, + { + "cell_type": "markdown", + "id": "087a263c", + "metadata": {}, + "source": [ + "## Extra" + ] + }, + { + "cell_type": "markdown", + "id": "8e3067e6", + "metadata": {}, + "source": [ + "19. Congratulations! In this project, you used Python dictionaries to store and update medical data for individuals.\n", + "\n", + " If you'd like extra practice with dictionaries, here are some suggestions to go further with this project:\n", + " - Create a function called `update_medical_records()` that takes in the name of an individual as well as their medical data, and then updates the `medical_records` dictionary accordingly.\n", + " - Create a new dictionary of your choice - feel free to get creative!\n", + " \n", + " Happy coding!" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "ac494987", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Marina': {'Age': 28, 'Sex': 'Female', 'BMI': 32.1, 'Children': 2, 'Smoker': 'Non-smoker', 'Insurance_cost': 6607.0}, 'Connie': {'Age': 43, 'Sex': 'Female', 'BMI': 25.3, 'Children': 3, 'Smoker': 'Non-smoker', 'Insurance_cost': 8886.0}, 'Isaac': {'Age': 35, 'Sex': 'Male', 'BMI': 20.6, 'Children': 4, 'Smoker': 'Smoker', 'Insurance_cost': 16444.0}, 'Valentina': {'Age': 52, 'Sex': 'Female', 'BMI': 18.7, 'Children': 1, 'Smoker': 'Non-smoker', 'Insurance_cost': 6420.0}, 'Vinay': {'Age': 25, 'Sex': 'Male', 'BMI': 27.0, 'Children': 0, 'Smoker': 'Non-smoker', 'Insurance_cost': 3500.0}}\n" + ] + } + ], + "source": [ + "def update_medical_records(name, age=None, sex=None, bmi=None, children=None, smoker=None, insurance_cost=None):\n", + " if name in medical_records:\n", + " if age is not None:\n", + " medical_records[name][\"Age\"] = age\n", + " if sex is not None:\n", + " medical_records[name][\"Sex\"] = sex\n", + " if bmi is not None:\n", + " medical_records[name][\"BMI\"] = bmi\n", + " if children is not None:\n", + " medical_records[name][\"Children\"] = children\n", + " if smoker is not None:\n", + " medical_records[name][\"Smoker\"] = smoker\n", + " if insurance_cost is not None:\n", + " medical_records[name][\"Insurance_cost\"] = insurance_cost\n", + " else:\n", + " medical_records[name] = {\n", + " \"Age\": age,\n", + " \"Sex\": sex,\n", + " \"BMI\": bmi,\n", + " \"Children\": children,\n", + " \"Smoker\": smoker,\n", + " \"Insurance_cost\": insurance_cost\n", + " }\n", + "\n", + "# Test the function\n", + "update_medical_records(\"Marina\", age=28, bmi=32.1)\n", + "update_medical_records(\"Vinay\", age=25, sex=\"Male\", bmi=27.0, children=0, smoker=\"Non-smoker\", insurance_cost=3500.0)\n", + "print(medical_records)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "4195bad7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Dr. Smith': 'Cardiology', 'Dr. Johnson': 'Neurology', 'Dr. Lee': 'Pediatrics', 'Dr. Patel': 'Orthopedics', 'Dr. Kim': 'Dermatology'}\n" + ] + } + ], + "source": [ + "# New dictionary for doctors and their specialties\n", + "doctors_specialties = {\n", + " \"Dr. Smith\": \"Cardiology\",\n", + " \"Dr. Johnson\": \"Neurology\",\n", + " \"Dr. Lee\": \"Pediatrics\",\n", + " \"Dr. Patel\": \"Orthopedics\",\n", + " \"Dr. Kim\": \"Dermatology\"\n", + "}\n", + "\n", + "# Print the new dictionary\n", + "print(doctors_specialties)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c81d2670-093d-4d7b-83b6-6d63d3d552ff", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}