{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 1: Practice problems (Python basics)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 1, Tuesday.\n",
    "\n",
    "Fifteen problems in three levels, on the same ideas and at the same level as the lesson notebook.\n",
    "\n",
    "How to work\n",
    "\n",
    "- Write your code in the cell under each problem. Where the cell already has some lines, they give you the starting values.\n",
    "- Run the cell with Shift + Enter and read the output. Does it answer the question?\n",
    "- A hint and one possible solution are under each problem. Open them only after you have tried. There is usually more than one correct answer."
   ],
   "id": "t01p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Warm-up"
   ],
   "id": "t01p-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. A sentence about you\n",
    "\n",
    "Create variables for your name, age, and favourite number, then print a sentence using an f-string."
   ],
   "id": "t01p-002"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Put an `f` before the quotes and the variable names inside `{ }`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "my_name = \"Aylin\"\n",
    "my_age = 20\n",
    "favourite_number = 7\n",
    "print(f\"My name is {my_name}, I am {my_age} and my favourite number is {favourite_number}.\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "My name is Aylin, I am 20 and my favourite number is 7.\n",
    "```\n",
    "\n",
    "Your own values and wording can be different.\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-004"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. Area of a rectangle\n",
    "\n",
    "Calculate the area of a rectangle with width 8 and height 5, and store it in a variable called `area`."
   ],
   "id": "t01p-005"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "width = 8\n",
    "height = 5\n",
    "# Write your code here"
   ],
   "id": "t01p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Area is width times height. Store the numbers in variables first.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "width = 8\n",
    "height = 5\n",
    "area = width * height\n",
    "print(area)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "40\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. Even or odd\n",
    "\n",
    "Use `%` to check whether the number 47 is even or odd."
   ],
   "id": "t01p-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 47\n",
    "# Write your code here"
   ],
   "id": "t01p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A number is even when `n % 2 == 0`. What is the remainder when you divide 47 by 2?\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "number = 47\n",
    "print(number % 2)        # remainder 1 means odd\n",
    "print(number % 2 == 0)   # is it even?\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "1\n",
    "False\n",
    "```\n",
    "\n",
    "Your own values and wording can be different.\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. Favourite foods\n",
    "\n",
    "Make a list of 5 favourite foods, then print the first and last items."
   ],
   "id": "t01p-011"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The first item is at position `0`. The last item is at position `-1`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "foods = [\"pizza\", \"sushi\", \"pasta\", \"salad\", \"pancakes\"]\n",
    "print(foods[0])\n",
    "print(foods[-1])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "pizza\n",
    "pancakes\n",
    "```\n",
    "\n",
    "Your own values and wording can be different.\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-013"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. A book as a dictionary\n",
    "\n",
    "Create a dictionary describing a book (title, author, pages) and print the author."
   ],
   "id": "t01p-014"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01p-015"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Use curly brackets with key: value pairs, then `book[\"author\"]`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "book = {\"title\": \"Animal Farm\", \"author\": \"George Orwell\", \"pages\": 112}\n",
    "print(book[\"author\"])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "George Orwell\n",
    "```\n",
    "\n",
    "Your own values and wording can be different.\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. Old enough to vote?\n",
    "\n",
    "Write a comparison that checks whether a person aged 17 can vote (age 18 or over) and print the result."
   ],
   "id": "t01p-017"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 17\n",
    "# Write your code here"
   ],
   "id": "t01p-018"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The comparison `>=` gives `True` or `False`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "age = 17\n",
    "can_vote = age >= 18\n",
    "print(can_vote)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "False\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Core practice"
   ],
   "id": "t01p-020"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. Celsius to Fahrenheit\n",
    "\n",
    "Convert `25` degrees Celsius to Fahrenheit with `F = C * 9 / 5 + 32` and print a sentence with both values."
   ],
   "id": "t01p-021"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "celsius = 25\n",
    "# Write your code here"
   ],
   "id": "t01p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Store celsius in a variable, calculate fahrenheit, then use an f-string.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "celsius = 25\n",
    "fahrenheit = celsius * 9 / 5 + 32\n",
    "print(f\"{celsius} C is {fahrenheit} F\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "25 C is 77.0 F\n",
    "```\n",
    "\n",
    "Your own values and wording can be different.\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. Clean a text\n",
    "\n",
    "Clean the text `\"  python FOR data science  \"`: remove the outer spaces, make it Title Case, print it, then print how many words it has."
   ],
   "id": "t01p-024"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw = \"  python FOR data science  \"\n",
    "# Write your code here"
   ],
   "id": "t01p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Chain methods: `raw.strip().title()`. Count words with `len(text.split())`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "raw = \"  python FOR data science  \"\n",
    "clean = raw.strip().title()\n",
    "print(clean)\n",
    "print(len(clean.split()))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Python For Data Science\n",
    "4\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. Hours and minutes\n",
    "\n",
    "Turn 200 minutes into hours and minutes, and print it like 3 h 20 min."
   ],
   "id": "t01p-027"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_minutes = 200\n",
    "# Write your code here"
   ],
   "id": "t01p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Hours: `// 60`. Minutes left over: `% 60`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "total_minutes = 200\n",
    "hours = total_minutes // 60\n",
    "minutes = total_minutes % 60\n",
    "print(f\"{hours} h {minutes} min\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "3 h 20 min\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-029"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. Shopping list\n",
    "\n",
    "Start with `shopping = [\"milk\", \"bread\"]`. Add \"eggs\" at the end, put \"apples\" first, remove \"bread\", then print the list and its length."
   ],
   "id": "t01p-030"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shopping = [\"milk\", \"bread\"]\n",
    "# Write your code here"
   ],
   "id": "t01p-031"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Use `append`, `insert(0, ...)` and `remove`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "shopping = [\"milk\", \"bread\"]\n",
    "shopping.append(\"eggs\")\n",
    "shopping.insert(0, \"apples\")\n",
    "shopping.remove(\"bread\")\n",
    "print(shopping)\n",
    "print(len(shopping))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "['apples', 'milk', 'eggs']\n",
    "3\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. Grades\n",
    "\n",
    "Store grades in `{\"Aylin\": 88, \"Omar\": 92, \"Lea\": 75}`. Add Sam with 81, then print the dictionary, the average grade, and the highest grade."
   ],
   "id": "t01p-033"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grades = {\"Aylin\": 88, \"Omar\": 92, \"Lea\": 75}\n",
    "# Write your code here"
   ],
   "id": "t01p-034"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Average: `sum(grades.values()) / len(grades)`. Highest: `max(grades.values())`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "grades = {\"Aylin\": 88, \"Omar\": 92, \"Lea\": 75}\n",
    "grades[\"Sam\"] = 81\n",
    "average = sum(grades.values()) / len(grades)\n",
    "print(grades)\n",
    "print(average)\n",
    "print(max(grades.values()))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "{'Aylin': 88, 'Omar': 92, 'Lea': 75, 'Sam': 81}\n",
    "84.0\n",
    "92\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. Visitors\n",
    "\n",
    "Count the different names in `[\"anna\", \"ben\", \"anna\", \"cara\", \"ben\"]`. Then find who visited on both days: Monday `{\"anna\", \"ben\", \"cara\"}` and Tuesday `{\"ben\", \"dan\"}`."
   ],
   "id": "t01p-036"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "visitors = [\"anna\", \"ben\", \"anna\", \"cara\", \"ben\"]\n",
    "monday = {\"anna\", \"ben\", \"cara\"}\n",
    "tuesday = {\"ben\", \"dan\"}\n",
    "# Write your code here"
   ],
   "id": "t01p-037"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A set removes duplicates; `len(set(...))` counts them. Both days: `&`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "visitors = [\"anna\", \"ben\", \"anna\", \"cara\", \"ben\"]\n",
    "print(len(set(visitors)))\n",
    "\n",
    "monday = {\"anna\", \"ben\", \"cara\"}\n",
    "tuesday = {\"ben\", \"dan\"}\n",
    "print(sorted(monday & tuesday))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "3\n",
    "['ben']\n",
    "```\n",
    "\n",
    "Your own values and wording can be different.\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge"
   ],
   "id": "t01p-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. Shop receipt\n",
    "\n",
    "A shop sells 3 items at 4.50 each with 18% tax. Print the subtotal, the tax and the total, each with 2 decimal places."
   ],
   "id": "t01p-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "price = 4.5\n",
    "quantity = 3\n",
    "tax_rate = 0.18\n",
    "# Write your code here"
   ],
   "id": "t01p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Subtotal = price * quantity. Tax = subtotal * 0.18. Format with `:.2f` inside an f-string.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "price = 4.5\n",
    "quantity = 3\n",
    "tax_rate = 0.18\n",
    "\n",
    "subtotal = price * quantity\n",
    "tax = subtotal * tax_rate\n",
    "total = subtotal + tax\n",
    "print(f\"Subtotal: {subtotal:.2f}\")\n",
    "print(f\"Tax:      {tax:.2f}\")\n",
    "print(f\"Total:    {total:.2f}\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Subtotal: 13.50\n",
    "Tax:      2.43\n",
    "Total:    15.93\n",
    "```\n",
    "\n",
    "Your own values and wording can be different.\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-042"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. Leap year\n",
    "\n",
    "A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. Check 2024 and 2026."
   ],
   "id": "t01p-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "year = 2024\n",
    "# Write your code here"
   ],
   "id": "t01p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Combine `%`, `==`, `!=`, `and`, `or` and brackets.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "year = 2024\n",
    "is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "print(is_leap)\n",
    "\n",
    "year = 2026\n",
    "is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "print(is_leap)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "True\n",
    "False\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-045"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. Username\n",
    "\n",
    "Build a username from `first = \"Aylin\"`, `last = \"Demir\"`, `birth_year = 2005`: the first letter of the first name, the last name, all in small letters, then the last two digits of the year."
   ],
   "id": "t01p-046"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first = \"Aylin\"\n",
    "last = \"Demir\"\n",
    "birth_year = 2005\n",
    "# Write your code here"
   ],
   "id": "t01p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`first[0]` gives the first letter. Convert the year with `str()` and slice `[-2:]`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "first = \"Aylin\"\n",
    "last = \"Demir\"\n",
    "birth_year = 2005\n",
    "username = (first[0] + last).lower() + str(birth_year)[-2:]\n",
    "print(username)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "ademir05\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01p-048"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you have finished, compare your solutions with the hidden ones. A different solution that gives the right result is just as good."
   ],
   "id": "t01p-049"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
