From ba3ed76bf9c895dd1280a8fad119916015934f35 Mon Sep 17 00:00:00 2001 From: Samuella Ojo Date: Mon, 11 May 2026 21:26:43 -0400 Subject: [PATCH] Add files via upload --- 02_exercises/Day_3_Exercise.ipynb | 364 ++++++++++++++++++++++++++++++ 1 file changed, 364 insertions(+) create mode 100644 02_exercises/Day_3_Exercise.ipynb diff --git a/02_exercises/Day_3_Exercise.ipynb b/02_exercises/Day_3_Exercise.ipynb new file mode 100644 index 0000000..fe584ed --- /dev/null +++ b/02_exercises/Day_3_Exercise.ipynb @@ -0,0 +1,364 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "031285e0", + "metadata": { + "id": "031285e0" + }, + "source": [ + "# Day 3 Exercises\n", + "## Control flow" + ] + }, + { + "cell_type": "markdown", + "id": "fbcba899", + "metadata": { + "id": "fbcba899" + }, + "source": [ + "#### 1. Write a conditional that prints different messages if a bank account balance is below 3,000, between 3,000 and 10,000, or over 10,000." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "607dbccc", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "607dbccc", + "outputId": "7e18972a-b974-48b6-ae85-b8fc4da537de" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Your balance is below 3000\n" + ] + } + ], + "source": [ + "bank_account_balance = 2000\n", + "if bank_account_balance < 3000:\n", + " print(\"Your balance is below 3000\")\n", + "elif bank_account_balance >= 3000 and bank_account_balance <= 10000:\n", + " print(\"Your balance is between 3000 and 10000\")\n", + "elif bank_account_balance > 10000:\n", + " print(\"Your balance is over 10000\")" + ] + }, + { + "cell_type": "markdown", + "id": "70d0c36a", + "metadata": { + "id": "70d0c36a" + }, + "source": [ + "#### 2. Create a list, `books`, containing the following items: 'War and Peace', 'Pride and Prejudice', 'Mockingjay', 'Three Musketeers', 'The Adventures of Robinson Crusoe', 'Yevgeniy Onegin'.\n", + "\n", + "Then using slicing or indexing, create the following:\n", + "- An empty list\n", + "- The last item of books\n", + "- List of three items: 'Three Musketeers', 'The Adventures of Robinson Crusoe', 'Yevgeniy Onegin'." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66765f0c", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "66765f0c", + "outputId": "c25040c5-e013-4164-ad5a-fcb3c6e9e24f" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[]\n", + "Yevgeniy Onegin\n", + "['Three Muskateers', 'The Adventures of Robinson Crusoe', 'Yevgeniy Onegin']\n" + ] + } + ], + "source": [ + "books = ['War and Peace', 'Pride and Prejudice', 'Mockingjay', 'Three Muskateers', 'The Adventures of Robinson Crusoe', 'Yevgeniy Onegin']\n", + "empty_list = []\n", + "last_item = books[-1]\n", + "three_items = books[3:6]\n", + "\n", + "print(empty_list)\n", + "print(last_item)\n", + "print(three_items)" + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "KTWLFzE6ceOg" + }, + "id": "KTWLFzE6ceOg", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "49132b34", + "metadata": { + "id": "49132b34" + }, + "source": [ + "Using list methods:\n", + "\n", + "- Remove 'Pride and Prejudice' from the list.\n", + "- Insert 'Harry Potter and the Chamber of Secrets' after 'Mockingjay'." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6bbf4b07", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6bbf4b07", + "outputId": "cde66552-8e36-4f4f-ee8c-5b7184c751ed" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['War and Peace',\n", + " 'Mockingjay',\n", + " 'Harry Potter and the Chamber of Secrets',\n", + " 'Three Muskateers',\n", + " 'The Adventures of Robinson Crusoe',\n", + " 'Yevgeniy Onegin']" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ], + "source": [ + "books.remove('Pride and Prejudice')\n", + "books.insert(2, 'Harry Potter and the Chamber of Secrets')\n", + "books" + ] + }, + { + "cell_type": "markdown", + "id": "71402dbe", + "metadata": { + "id": "71402dbe" + }, + "source": [ + "#### 3. Given the list `people`, sort it by people's first name, last name and age. Store the sorted lists as by_first_name, by_last_name, and by_age, respectively.\n", + "\n", + "people = [('Mark', 'Harrison', 56), ('Ken', 'Wolseley', 23), ('Emily', 'Robinson', 77)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dd57f86d", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dd57f86d", + "outputId": "629965a8-9a76-465d-d512-b546cd1f48b3" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[('Emily', 'Robinson', 77), ('Ken', 'Wolseley', 23), ('Mark', 'Harrison', 56)]\n", + "[('Mark', 'Harrison', 56), ('Emily', 'Robinson', 77), ('Ken', 'Wolseley', 23)]\n", + "[('Ken', 'Wolseley', 23), ('Mark', 'Harrison', 56), ('Emily', 'Robinson', 77)]\n" + ] + } + ], + "source": [ + "people = [('Mark', 'Harrison', 56), ('Ken', 'Wolseley', 23), ('Emily', 'Robinson', 77)]\n", + "people\n", + "sorted_by_first_name = sorted(people, key=lambda x: x[0])\n", + "sorted_by_last_name = sorted(people, key=lambda x: x[1])\n", + "sorted_by_age = sorted(people, key=lambda x: x[2])\n", + "print(sorted_by_first_name)\n", + "print(sorted_by_last_name)\n", + "print(sorted_by_age)" + ] + }, + { + "cell_type": "markdown", + "id": "1c2ba142", + "metadata": { + "id": "1c2ba142" + }, + "source": [ + "#### 4. Write a function called `dict_intersect` that takes two dictionaries, d1 and d2, as arguments and returns a set that contains only the keys found in both of the original dictionaries." + ] + }, + { + "cell_type": "code", + "source": [ + "def dict_intersect(d1, d2):\n", + " return set(d1.keys()) & set(d2.keys())\n", + "d1 = {\n", + " \"A\" : \"Apple\",\n", + " \"B\" : \"Ball\",\n", + " \"C\" : \"Cat\",\n", + " \"D\" : \"Dog\"\n", + "}\n", + "d2 = {\n", + " \"B\" : \"Ball\",\n", + " \"C\" : \"Cat\",\n", + " \"D\" : \"Dog\",\n", + " \"E\" : \"Elephant\"\n", + "}\n", + "dict_intersect(d1, d2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_OqrBc4whJM6", + "outputId": "51dc4c5c-baf3-41e2-f598-5b62f78e34db" + }, + "id": "_OqrBc4whJM6", + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'B', 'C', 'D'}" + ] + }, + "metadata": {}, + "execution_count": 27 + } + ] + }, + { + "cell_type": "markdown", + "id": "3b2e1242", + "metadata": { + "id": "3b2e1242" + }, + "source": [ + "## Reading and Writing" + ] + }, + { + "cell_type": "markdown", + "id": "70ac8b82", + "metadata": { + "id": "70ac8b82" + }, + "source": [ + "#### 1. Using `w`, create a file called fruits.txt and write the string \"tangerines\" to it." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f62d6bae", + "metadata": { + "id": "f62d6bae" + }, + "outputs": [], + "source": [ + "with open(\"fruits.txt\", \"w\") as f:\n", + " f.write(\"tangerines\")" + ] + }, + { + "cell_type": "markdown", + "id": "c59870b7", + "metadata": { + "id": "c59870b7" + }, + "source": [ + "#### 2. Next, use a to append the string \"dragon fruit\" to the file on a new line." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d8b37e9a", + "metadata": { + "id": "d8b37e9a" + }, + "outputs": [], + "source": [ + "with open(\"fruits.txt\", \"a\") as f:\n", + " f.write(\"\\ndragon fruit\")" + ] + }, + { + "cell_type": "markdown", + "id": "3f34d3b1", + "metadata": { + "id": "3f34d3b1" + }, + "source": [ + "#### 3. Finally, use the `r` mode to read and print the entire file." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6ce0cf9a", + "metadata": { + "id": "6ce0cf9a", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "820e31ac-de76-4e84-9232-4a88c5cfa9f5" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "tangerines\n", + "dragon fruit\n" + ] + } + ], + "source": [ + "with open(\"fruits.txt\", \"r\") as f:\n", + " print(f.read())" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "lcr-env", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.11.13" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file