diff --git a/.ipynb_checkpoints/m1-01-ipython-python-basics-lab-checkpoint.ipynb b/.ipynb_checkpoints/m1-01-ipython-python-basics-lab-checkpoint.ipynb new file mode 100644 index 0000000..9844494 --- /dev/null +++ b/.ipynb_checkpoints/m1-01-ipython-python-basics-lab-checkpoint.ipynb @@ -0,0 +1,552 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "813714e6-069f-4a18-8345-fc250517c51e", + "metadata": {}, + "source": [ + "task 1, first,we create values and assign them variable " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "aed222f3-b0ed-4157-b709-b2773b646010", + "metadata": {}, + "outputs": [], + "source": [ + "my_int=19\n", + "my_float=22.5\n", + "my_str=\"ironhack\"\n", + "my_list=[2,4,6,8,10]\n", + "my_dict={\"apple\":\"green\",\"banana\":\"yellow\"}" + ] + }, + { + "cell_type": "markdown", + "id": "7541b79c-4a57-420f-a814-7c7503e4dd3e", + "metadata": {}, + "source": [ + "we check the type of each variable" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ad3bcd65-2d54-458b-b283-489793fa77f6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "print(type(my_int))\n", + "print(type(my_float))\n", + "print(type(my_str))\n", + "print(type(my_list))\n", + "print(type(my_dict))" + ] + }, + { + "cell_type": "markdown", + "id": "f9e53042-ddd9-4663-8936-8701e591ef71", + "metadata": {}, + "source": [ + "type of my_int: integer, type of my_float: float, type of my_str: string, type of my_list: list, type of my_dict: dict" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "31f74414-83ba-428e-9176-6d119f37b35f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10, 12]\n", + "dict_keys(['apple', 'banana'])\n", + "IRONHACK\n" + ] + } + ], + "source": [ + "my_list.append(12)\n", + "print(my_list)\n", + "print(my_dict.keys())\n", + "print(my_str.upper())" + ] + }, + { + "cell_type": "markdown", + "id": "4b376c3f-61c3-4f7f-a9b4-b64ead8e4a8a", + "metadata": {}, + "source": [ + "append(): It added an element to the end of list, \n", + "keys(): It returned a dict_keys object containing all the keys currently in the dictionary\n", + "upper(): It returned a new string with all characters converted to uppercase." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "122a680e-6daa-4a27-9c88-201bcb0fc2c2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[31mSignature:\u001b[39m my_list.append(object, /)\n", + "\u001b[31mDocstring:\u001b[39m Append object to the end of the list.\n", + "\u001b[31mType:\u001b[39m builtin_function_or_method" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "my_list.append?" + ] + }, + { + "cell_type": "markdown", + "id": "039a4bec-ca71-4485-91e2-71d1d20051e6", + "metadata": {}, + "source": [ + "I used the ? operator to see the documentation for .append() and I saw that it is a built-in method used to add an item to the end of a list" + ] + }, + { + "cell_type": "markdown", + "id": "ff4a3fe9-4ddb-48c2-be76-d28bf88426c7", + "metadata": {}, + "source": [ + "task 2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "b0715755-ae2d-4331-b787-08e887546592", + "metadata": {}, + "outputs": [], + "source": [ + "values = [10, 20, 30]\n", + "alias = values\n", + "alias.append(40)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "dbebdc87-8978-4d07-a36c-fa99c3b35f5e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "values: [10, 20, 30, 40]\n", + "alias: [10, 20, 30, 40]\n" + ] + } + ], + "source": [ + "print(\"values: \",values)\n", + "print(\"alias: \",alias)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "8ba87f78-f88c-465d-9267-7d4e502ab1d0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "values: [10, 20, 30, 40]\n", + "copy of values: [10, 20, 30, 40, 50]\n" + ] + } + ], + "source": [ + "values_copy=values.copy()\n", + "values_copy.append(50)\n", + "print(\"values: \",values)\n", + "print(\"copy of values: \",values_copy)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "78bc140d-a6c3-4772-87c9-6d605a44ec67", + "metadata": {}, + "outputs": [], + "source": [ + "record={\"name\":\"Nurana\",\"Surname\":\"Aliyarli\",\"age\":20}\n", + "record_alias=record\n", + "record_alias[\"city\"]=\"Baku\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c6e6a23d-3f1c-4cc7-88de-0a128c9b095d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "record: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku'}\n", + "record_alias: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku'}\n" + ] + } + ], + "source": [ + "print(\"record: \",record)\n", + "print(\"record_alias: \",record_alias)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "2b5e3795-09b5-4776-ad5e-843ca0d91e86", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "record: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku'}\n", + "record_copy: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku', 'university': 'Khazar'}\n" + ] + } + ], + "source": [ + "record_copy=record.copy()\n", + "record_copy[\"university\"]=\"Khazar\"\n", + "print(\"record: \",record)\n", + "print(\"record_copy: \",record_copy)" + ] + }, + { + "cell_type": "markdown", + "id": "1e10b750-4467-495b-928a-c312bddbfa40", + "metadata": {}, + "source": [ + "When I use an alias, both names point to the same object in memory,so if I change one, the other changes too. But when I use .copy(), I create a new, independent object, so the original stays safe.\n", + "This is also same for functions,if I pass a list into a function, the function can accidentally change my original data because it uses the same reference.To prevent this, I should pass a copy of the list to the function instead." + ] + }, + { + "cell_type": "markdown", + "id": "5c34e31f-0eb1-454c-89ee-fe1350858414", + "metadata": {}, + "source": [ + "task 4" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "5033e1ed-e820-4044-bfa8-4c50dff15971", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "cleaned count: 9\n", + "{'user_id': 2, 'event_type': 'view', 'duration_seconds': 120, 'duration_minutes': 2.0}\n", + "{'user_id': 4, 'event_type': 'view', 'duration_seconds': 75, 'duration_minutes': 1.25}\n" + ] + } + ], + "source": [ + "user_events= [\n", + " {\"user_id\":1, \"event_type\":\"login\", \"duration_seconds\":60},\n", + " {\"user_id\":2, \"event_type\":\"view\", \"duration_seconds\":120},\n", + " {\"user_id\":3, \"event_type\":\"click\", \"duration_seconds\":-30}, \n", + " {\"user_id\":4, \"event_type\":\"view\", \"duration_seconds\":75},\n", + " {\"user_id\":5, \"event_type\":\"logout\", \"duration_seconds\":45},\n", + " {\"user_id\":6, \"event_type\":\"login\", \"duration_seconds\":\"none\"},\n", + " {\"user_id\":7, \"event_type\":\"click\", \"duration_seconds\":180},\n", + " {\"user_id\":8, \"event_type\":\"view\", \"duration_seconds\":240},\n", + " {\"user_id\":9, \"event_type\":\"login\", \"duration_seconds\":-90},\n", + " {\"user_id\":10, \"event_type\":\"view\", \"duration_seconds\":600},\n", + " {\"user_id\":11, \"event_type\":\"click\", \"duration_seconds\":15},\n", + " {\"user_id\":12, \"event_type\":\"logout\", \"duration_seconds\":50}]\n", + "\n", + "cleaned_events = []\n", + "\n", + "for event in user_events:\n", + " duration = event[\"duration_seconds\"]\n", + " \n", + " if type(duration) in (int, float) and duration >= 0:\n", + " event[\"duration_minutes\"] = duration / 60\n", + " cleaned_events.append(event)\n", + " \n", + "print(\"cleaned count:\", len(cleaned_events))\n", + "\n", + "for event in cleaned_events:\n", + " if not all(key in event for key in [\"user_id\", \"event_type\", \"duration_seconds\", \"duration_minutes\"]):\n", + " print(\"missing key!\")\n", + "\n", + "print(cleaned_events[1])\n", + "print(cleaned_events[2])\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "07715d60-8395-4c0d-8322-aa3569ab78b5", + "metadata": {}, + "source": [ + "we build a dictionary of event counts by event_type" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "85b2ed07-1089-48d1-a4b4-5e91b6c15c9c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Event counts: {'login': 2, 'view': 4, 'logout': 2, 'click': 2}\n" + ] + } + ], + "source": [ + "event_counts = {}\n", + "\n", + "for event in cleaned_events:\n", + " event_type = event[\"event_type\"]\n", + " \n", + " if event_type in event_counts:\n", + " event_counts[event_type] += 1\n", + " else:\n", + " event_counts[event_type] = 1\n", + "\n", + "print(\"Event counts:\", event_counts)\n" + ] + }, + { + "cell_type": "markdown", + "id": "2e47b8e7-0e53-43a9-9db6-7b573e40e1e8", + "metadata": {}, + "source": [ + "then we build a dictionary of average duration_minutes by event_type. " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "09a7a361-14a2-407a-9063-cdcdd78af391", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average duration: {'login': 1.25, 'view': 4.3125, 'logout': 0.7916666666666667, 'click': 1.625}\n" + ] + } + ], + "source": [ + "duration_totals = {}\n", + "event_numbers = {}\n", + "\n", + "for event in cleaned_events:\n", + " event_type = event[\"event_type\"]\n", + " duration = event[\"duration_minutes\"]\n", + " \n", + " if event_type in duration_totals:\n", + " duration_totals[event_type] += duration\n", + " event_numbers[event_type] += 1\n", + " else:\n", + " duration_totals[event_type] = duration\n", + " event_numbers[event_type] = 1\n", + "\n", + "avg_duration = {}\n", + "for event_type in duration_totals:\n", + " avg_duration[event_type] = duration_totals[event_type] / event_numbers[event_type]\n", + "\n", + "print(\"average duration:\", avg_duration)" + ] + }, + { + "cell_type": "markdown", + "id": "5e3ae55c-2e69-4a89-8caa-35b3570b21b7", + "metadata": {}, + "source": [ + "we compute number of unique users" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f036d370-88a0-44f1-9c87-47aa93ed85ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "number of unique users: 10\n" + ] + } + ], + "source": [ + "unique_users = set()\n", + "for event in cleaned_events:\n", + " unique_users.add(event[\"user_id\"])\n", + "\n", + "print(\"number of unique users:\", len(unique_users))" + ] + }, + { + "cell_type": "markdown", + "id": "8ec3bec0-4c9a-4cb6-ae6f-eaa367727d57", + "metadata": {}, + "source": [ + "we check the the one validation here" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "98109304-c338-4b80-91f2-078e267a8205", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "validation passed\n" + ] + } + ], + "source": [ + "total_count = sum(event_counts.values()) \n", + "if total_count == len(cleaned_events):\n", + " print(\"validation passed\")\n", + "else:\n", + " print(\"validation failed\")" + ] + }, + { + "cell_type": "markdown", + "id": "a53546b4-f862-45c8-a513-4be794daa9f7", + "metadata": {}, + "source": [ + "task 5" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "2d6754aa-e97b-4463-8985-1fab9ff17afd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "metric,key,value\n", + "count,login,2\n", + "count,view,4\n", + "count,logout,2\n", + "count,click,2\n", + "average,login,1.25\n", + "average,view,4.3125\n", + "average,logout,0.7916666666666667\n", + "average,click,1.625\n", + "unique_users,all,10\n", + "\n" + ] + } + ], + "source": [ + "import io\n", + "import csv\n", + "\n", + "csv_rows = [[\"metric\", \"key\", \"value\"]]\n", + "\n", + "#one row per event type for the counts\n", + "for event_type, count in event_counts.items():\n", + " csv_rows.append([\"count\", event_type, count])\n", + "\n", + "#one row per event type for the averages\n", + "for event_type, avg in avg_duration.items():\n", + " csv_rows.append([\"average\", event_type, avg])\n", + "\n", + "#final row for the unique user count\n", + "csv_rows.append([\"unique_users\", \"all\", len(unique_users)])\n", + "\n", + "output = io.StringIO()\n", + "writer = csv.writer(output)\n", + "writer.writerows(csv_rows)\n", + "csv_string = output.getvalue()\n", + "\n", + "print(csv_string)" + ] + }, + { + "cell_type": "markdown", + "id": "e5ddd327-befb-4eee-b0f0-c22d6bbcd954", + "metadata": {}, + "source": [ + "THE CSV FILE\n", + "\n", + "metric,key,value\n", + "count,login,2\n", + "count,view,4\n", + "count,logout,2\n", + "count,click,2\n", + "average,login,1.25\n", + "average,view,4.3125\n", + "average,logout,0.7916666666666667\n", + "average,click,1.625\n", + "unique_users,all,10" + ] + } + ], + "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.13.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/.ipynb_checkpoints/untitled-checkpoint.py b/.ipynb_checkpoints/untitled-checkpoint.py new file mode 100644 index 0000000..e5c45bb --- /dev/null +++ b/.ipynb_checkpoints/untitled-checkpoint.py @@ -0,0 +1,17 @@ +def to_float(s): + try: + return float(s) + except (ValueError, TypeError): + return None + +def clean(s): + return str(s).strip().lower() + +test_data = [" 42.5 ", "PYTHON", " 100 ", "alma123", " "] + +for item in test_data: + cleaned_text = clean(item) + float_value = to_float(cleaned_text) + + # Nəticəni "expression" şəklində göstərək + print(f"Giriş: {repr(item):<10} | Təmiz: {repr(cleaned_text):<10} | Rəqəm: {float_value}") \ No newline at end of file diff --git a/m1-01-ipython-python-basics-lab.ipynb b/m1-01-ipython-python-basics-lab.ipynb new file mode 100644 index 0000000..9844494 --- /dev/null +++ b/m1-01-ipython-python-basics-lab.ipynb @@ -0,0 +1,552 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "813714e6-069f-4a18-8345-fc250517c51e", + "metadata": {}, + "source": [ + "task 1, first,we create values and assign them variable " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "aed222f3-b0ed-4157-b709-b2773b646010", + "metadata": {}, + "outputs": [], + "source": [ + "my_int=19\n", + "my_float=22.5\n", + "my_str=\"ironhack\"\n", + "my_list=[2,4,6,8,10]\n", + "my_dict={\"apple\":\"green\",\"banana\":\"yellow\"}" + ] + }, + { + "cell_type": "markdown", + "id": "7541b79c-4a57-420f-a814-7c7503e4dd3e", + "metadata": {}, + "source": [ + "we check the type of each variable" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ad3bcd65-2d54-458b-b283-489793fa77f6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "print(type(my_int))\n", + "print(type(my_float))\n", + "print(type(my_str))\n", + "print(type(my_list))\n", + "print(type(my_dict))" + ] + }, + { + "cell_type": "markdown", + "id": "f9e53042-ddd9-4663-8936-8701e591ef71", + "metadata": {}, + "source": [ + "type of my_int: integer, type of my_float: float, type of my_str: string, type of my_list: list, type of my_dict: dict" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "31f74414-83ba-428e-9176-6d119f37b35f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10, 12]\n", + "dict_keys(['apple', 'banana'])\n", + "IRONHACK\n" + ] + } + ], + "source": [ + "my_list.append(12)\n", + "print(my_list)\n", + "print(my_dict.keys())\n", + "print(my_str.upper())" + ] + }, + { + "cell_type": "markdown", + "id": "4b376c3f-61c3-4f7f-a9b4-b64ead8e4a8a", + "metadata": {}, + "source": [ + "append(): It added an element to the end of list, \n", + "keys(): It returned a dict_keys object containing all the keys currently in the dictionary\n", + "upper(): It returned a new string with all characters converted to uppercase." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "122a680e-6daa-4a27-9c88-201bcb0fc2c2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[31mSignature:\u001b[39m my_list.append(object, /)\n", + "\u001b[31mDocstring:\u001b[39m Append object to the end of the list.\n", + "\u001b[31mType:\u001b[39m builtin_function_or_method" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "my_list.append?" + ] + }, + { + "cell_type": "markdown", + "id": "039a4bec-ca71-4485-91e2-71d1d20051e6", + "metadata": {}, + "source": [ + "I used the ? operator to see the documentation for .append() and I saw that it is a built-in method used to add an item to the end of a list" + ] + }, + { + "cell_type": "markdown", + "id": "ff4a3fe9-4ddb-48c2-be76-d28bf88426c7", + "metadata": {}, + "source": [ + "task 2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "b0715755-ae2d-4331-b787-08e887546592", + "metadata": {}, + "outputs": [], + "source": [ + "values = [10, 20, 30]\n", + "alias = values\n", + "alias.append(40)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "dbebdc87-8978-4d07-a36c-fa99c3b35f5e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "values: [10, 20, 30, 40]\n", + "alias: [10, 20, 30, 40]\n" + ] + } + ], + "source": [ + "print(\"values: \",values)\n", + "print(\"alias: \",alias)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "8ba87f78-f88c-465d-9267-7d4e502ab1d0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "values: [10, 20, 30, 40]\n", + "copy of values: [10, 20, 30, 40, 50]\n" + ] + } + ], + "source": [ + "values_copy=values.copy()\n", + "values_copy.append(50)\n", + "print(\"values: \",values)\n", + "print(\"copy of values: \",values_copy)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "78bc140d-a6c3-4772-87c9-6d605a44ec67", + "metadata": {}, + "outputs": [], + "source": [ + "record={\"name\":\"Nurana\",\"Surname\":\"Aliyarli\",\"age\":20}\n", + "record_alias=record\n", + "record_alias[\"city\"]=\"Baku\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c6e6a23d-3f1c-4cc7-88de-0a128c9b095d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "record: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku'}\n", + "record_alias: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku'}\n" + ] + } + ], + "source": [ + "print(\"record: \",record)\n", + "print(\"record_alias: \",record_alias)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "2b5e3795-09b5-4776-ad5e-843ca0d91e86", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "record: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku'}\n", + "record_copy: {'name': 'Nurana', 'Surname': 'Aliyarli', 'age': 20, 'city': 'Baku', 'university': 'Khazar'}\n" + ] + } + ], + "source": [ + "record_copy=record.copy()\n", + "record_copy[\"university\"]=\"Khazar\"\n", + "print(\"record: \",record)\n", + "print(\"record_copy: \",record_copy)" + ] + }, + { + "cell_type": "markdown", + "id": "1e10b750-4467-495b-928a-c312bddbfa40", + "metadata": {}, + "source": [ + "When I use an alias, both names point to the same object in memory,so if I change one, the other changes too. But when I use .copy(), I create a new, independent object, so the original stays safe.\n", + "This is also same for functions,if I pass a list into a function, the function can accidentally change my original data because it uses the same reference.To prevent this, I should pass a copy of the list to the function instead." + ] + }, + { + "cell_type": "markdown", + "id": "5c34e31f-0eb1-454c-89ee-fe1350858414", + "metadata": {}, + "source": [ + "task 4" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "5033e1ed-e820-4044-bfa8-4c50dff15971", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "cleaned count: 9\n", + "{'user_id': 2, 'event_type': 'view', 'duration_seconds': 120, 'duration_minutes': 2.0}\n", + "{'user_id': 4, 'event_type': 'view', 'duration_seconds': 75, 'duration_minutes': 1.25}\n" + ] + } + ], + "source": [ + "user_events= [\n", + " {\"user_id\":1, \"event_type\":\"login\", \"duration_seconds\":60},\n", + " {\"user_id\":2, \"event_type\":\"view\", \"duration_seconds\":120},\n", + " {\"user_id\":3, \"event_type\":\"click\", \"duration_seconds\":-30}, \n", + " {\"user_id\":4, \"event_type\":\"view\", \"duration_seconds\":75},\n", + " {\"user_id\":5, \"event_type\":\"logout\", \"duration_seconds\":45},\n", + " {\"user_id\":6, \"event_type\":\"login\", \"duration_seconds\":\"none\"},\n", + " {\"user_id\":7, \"event_type\":\"click\", \"duration_seconds\":180},\n", + " {\"user_id\":8, \"event_type\":\"view\", \"duration_seconds\":240},\n", + " {\"user_id\":9, \"event_type\":\"login\", \"duration_seconds\":-90},\n", + " {\"user_id\":10, \"event_type\":\"view\", \"duration_seconds\":600},\n", + " {\"user_id\":11, \"event_type\":\"click\", \"duration_seconds\":15},\n", + " {\"user_id\":12, \"event_type\":\"logout\", \"duration_seconds\":50}]\n", + "\n", + "cleaned_events = []\n", + "\n", + "for event in user_events:\n", + " duration = event[\"duration_seconds\"]\n", + " \n", + " if type(duration) in (int, float) and duration >= 0:\n", + " event[\"duration_minutes\"] = duration / 60\n", + " cleaned_events.append(event)\n", + " \n", + "print(\"cleaned count:\", len(cleaned_events))\n", + "\n", + "for event in cleaned_events:\n", + " if not all(key in event for key in [\"user_id\", \"event_type\", \"duration_seconds\", \"duration_minutes\"]):\n", + " print(\"missing key!\")\n", + "\n", + "print(cleaned_events[1])\n", + "print(cleaned_events[2])\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "07715d60-8395-4c0d-8322-aa3569ab78b5", + "metadata": {}, + "source": [ + "we build a dictionary of event counts by event_type" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "85b2ed07-1089-48d1-a4b4-5e91b6c15c9c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Event counts: {'login': 2, 'view': 4, 'logout': 2, 'click': 2}\n" + ] + } + ], + "source": [ + "event_counts = {}\n", + "\n", + "for event in cleaned_events:\n", + " event_type = event[\"event_type\"]\n", + " \n", + " if event_type in event_counts:\n", + " event_counts[event_type] += 1\n", + " else:\n", + " event_counts[event_type] = 1\n", + "\n", + "print(\"Event counts:\", event_counts)\n" + ] + }, + { + "cell_type": "markdown", + "id": "2e47b8e7-0e53-43a9-9db6-7b573e40e1e8", + "metadata": {}, + "source": [ + "then we build a dictionary of average duration_minutes by event_type. " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "09a7a361-14a2-407a-9063-cdcdd78af391", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average duration: {'login': 1.25, 'view': 4.3125, 'logout': 0.7916666666666667, 'click': 1.625}\n" + ] + } + ], + "source": [ + "duration_totals = {}\n", + "event_numbers = {}\n", + "\n", + "for event in cleaned_events:\n", + " event_type = event[\"event_type\"]\n", + " duration = event[\"duration_minutes\"]\n", + " \n", + " if event_type in duration_totals:\n", + " duration_totals[event_type] += duration\n", + " event_numbers[event_type] += 1\n", + " else:\n", + " duration_totals[event_type] = duration\n", + " event_numbers[event_type] = 1\n", + "\n", + "avg_duration = {}\n", + "for event_type in duration_totals:\n", + " avg_duration[event_type] = duration_totals[event_type] / event_numbers[event_type]\n", + "\n", + "print(\"average duration:\", avg_duration)" + ] + }, + { + "cell_type": "markdown", + "id": "5e3ae55c-2e69-4a89-8caa-35b3570b21b7", + "metadata": {}, + "source": [ + "we compute number of unique users" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f036d370-88a0-44f1-9c87-47aa93ed85ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "number of unique users: 10\n" + ] + } + ], + "source": [ + "unique_users = set()\n", + "for event in cleaned_events:\n", + " unique_users.add(event[\"user_id\"])\n", + "\n", + "print(\"number of unique users:\", len(unique_users))" + ] + }, + { + "cell_type": "markdown", + "id": "8ec3bec0-4c9a-4cb6-ae6f-eaa367727d57", + "metadata": {}, + "source": [ + "we check the the one validation here" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "98109304-c338-4b80-91f2-078e267a8205", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "validation passed\n" + ] + } + ], + "source": [ + "total_count = sum(event_counts.values()) \n", + "if total_count == len(cleaned_events):\n", + " print(\"validation passed\")\n", + "else:\n", + " print(\"validation failed\")" + ] + }, + { + "cell_type": "markdown", + "id": "a53546b4-f862-45c8-a513-4be794daa9f7", + "metadata": {}, + "source": [ + "task 5" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "2d6754aa-e97b-4463-8985-1fab9ff17afd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "metric,key,value\n", + "count,login,2\n", + "count,view,4\n", + "count,logout,2\n", + "count,click,2\n", + "average,login,1.25\n", + "average,view,4.3125\n", + "average,logout,0.7916666666666667\n", + "average,click,1.625\n", + "unique_users,all,10\n", + "\n" + ] + } + ], + "source": [ + "import io\n", + "import csv\n", + "\n", + "csv_rows = [[\"metric\", \"key\", \"value\"]]\n", + "\n", + "#one row per event type for the counts\n", + "for event_type, count in event_counts.items():\n", + " csv_rows.append([\"count\", event_type, count])\n", + "\n", + "#one row per event type for the averages\n", + "for event_type, avg in avg_duration.items():\n", + " csv_rows.append([\"average\", event_type, avg])\n", + "\n", + "#final row for the unique user count\n", + "csv_rows.append([\"unique_users\", \"all\", len(unique_users)])\n", + "\n", + "output = io.StringIO()\n", + "writer = csv.writer(output)\n", + "writer.writerows(csv_rows)\n", + "csv_string = output.getvalue()\n", + "\n", + "print(csv_string)" + ] + }, + { + "cell_type": "markdown", + "id": "e5ddd327-befb-4eee-b0f0-c22d6bbcd954", + "metadata": {}, + "source": [ + "THE CSV FILE\n", + "\n", + "metric,key,value\n", + "count,login,2\n", + "count,view,4\n", + "count,logout,2\n", + "count,click,2\n", + "average,login,1.25\n", + "average,view,4.3125\n", + "average,logout,0.7916666666666667\n", + "average,click,1.625\n", + "unique_users,all,10" + ] + } + ], + "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.13.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/m1-01-task-3-functions.py b/m1-01-task-3-functions.py new file mode 100644 index 0000000..6e90697 --- /dev/null +++ b/m1-01-task-3-functions.py @@ -0,0 +1,16 @@ +def to_float(a): + try: + return float(a) + except (ValueError, TypeError): + return None + +def clean(a): + return str(a).strip().lower() + +test= ["IronHACK", " 2006 ", "data", " "," 20.33 "] + +for item in test: + cleaned_text = clean(item) + float_value = to_float(cleaned_text) + + print("Input: ", item, " Clean: ", cleaned_text, " Float: ", float_value) \ No newline at end of file