diff --git a/csv_string.csv b/csv_string.csv new file mode 100644 index 0000000..4410112 --- /dev/null +++ b/csv_string.csv @@ -0,0 +1,12 @@ +metric , key , value +count, login , 3 +count, logout , 2 +count, purchase , 2 +count, video_play , 2 +count, share , 1 +avg, login , 3 +avg, logout , 4 +avg, purchase , 4 +avg, video_play , 4 +avg, share , 4 +unique_users,total , 10 \ 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..c0c98c1 --- /dev/null +++ b/m1-01-ipython-python-basics-lab.ipynb @@ -0,0 +1,370 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 132, + "id": "cb2e5530-130e-4a76-b935-e74e96acc37b", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 133, + "id": "0a58bdde-7bf1-4157-99d7-5df96dcfbf1b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " type of list object is : \n", + " type of integer object is : \n", + " type of float object is : \n", + " type of dictionary object is : \n", + " type of string object is : \n" + ] + } + ], + "source": [ + "# ----- TASK 1 -----\n", + "\n", + "list_object = [1,2,3]\n", + "integer_object = 12\n", + "float_object = 12.5\n", + "dictionary_object = {\"Name\" : \"Firuddin\" , \"Surname\" : \"Rzayev\"}\n", + "string_object = \"Firuddin\"\n", + "\n", + "print(f\" type of list object is : {type(list_object)}\")\n", + "print(f\" type of integer object is : {type(integer_object)}\")\n", + "print(f\" type of float object is : {type(float_object)}\")\n", + "print(f\" type of dictionary object is : {type(dictionary_object)}\")\n", + "print(f\" type of string object is : {type(string_object)}\")\n", + "\n", + "# list_object.append?\n", + "# Signature: list_object.append(object, /)\n", + "# Docstring: Append object to the end of the list.\n", + "# Type: builtin_function_or_method\n" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "id": "abe025a2-ecc8-45cb-949a-df1371e6d798", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " values : [1, 2, 3, 4]\n", + " alias : [1, 2, 3, 4]\n", + " values_copy : [1, 2, 3, 4, 4]\n", + " ------------- \n", + " record : [1, 2, 3, 4]\n", + " alias : {'Name': 'Firuddin', 'Surname': 'Rzayev'}\n", + " record_copy : [1, 2, 3, 4, 4]\n" + ] + } + ], + "source": [ + "# ----- TASK 2 -----\n", + "\n", + "values = [1,2,3]\n", + "alias = values\n", + "values_copy = values.copy()\n", + "\n", + "alias.append(4)\n", + "values_copy.append(4)\n", + "values_copy.append(4)\n", + "\n", + "print(f\" values : {values}\")\n", + "print(f\" alias : {alias}\")\n", + "print(f\" values_copy : {values_copy}\")\n", + "\n", + "print(\" ------------- \" )\n", + "\n", + "record = {\"Name\" : \"Firuddin\"}\n", + "alias = record\n", + "record_copy = record.copy()\n", + "\n", + "alias.setdefault(\"Surname\",\"Rzayev\")\n", + "record_copy.setdefault(\"Surname\",\"Rzayev\")\n", + "record_copy.setdefault(\"Age\",20)\n", + "\n", + "print(f\" record : {values}\")\n", + "print(f\" alias : {alias}\")\n", + "print(f\" record_copy : {values_copy}\")\n", + "\n", + "# if we assign new value to the previous one, we will get exactly the same two variables. In contrast, copy() method creates different variable with same value or values." + ] + }, + { + "cell_type": "code", + "execution_count": 135, + "id": "1b158113-f569-414b-8156-be527d787456", + "metadata": {}, + "outputs": [], + "source": [ + "# --- TASK 4 ---\n", + "\n", + "events = [\n", + " {\"user_id\" : 1 , \"event_type\" : \"login\" , \"duration_seconds\" : 100},\n", + " {\"user_id\" : 2 , \"event_type\" : \"video_play\" , \"duration_seconds\" : -120},\n", + " {\"user_id\" : 3 , \"event_type\" : \"logout\" , \"duration_seconds\" : 140},\n", + " {\"user_id\" : 4 , \"event_type\" : \"purchase\" , \"duration_seconds\" : 160},\n", + " {\"user_id\" : 5 , \"event_type\" : \"login\" , \"duration_seconds\" : 180},\n", + " {\"user_id\" : 6 , \"event_type\" : \"video_play\" , \"duration_seconds\" : 200},\n", + " {\"user_id\" : 7 , \"event_type\" : \"comment\" , \"duration_seconds\" : \"abc\"},\n", + " {\"user_id\" : 8 , \"event_type\" : \"share\" , \"duration_seconds\" : 240},\n", + " {\"user_id\" : 9 , \"event_type\" : \"video_play\" , \"duration_seconds\" : 260},\n", + " {\"user_id\" : 10 , \"event_type\" : \"login\" , \"duration_seconds\" : 280},\n", + " {\"user_id\" : 11 , \"event_type\" : \"logout\" , \"duration_seconds\" : 300},\n", + " {\"user_id\" : 12 , \"event_type\" : \"purchase\" , \"duration_seconds\" : 320}\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 136, + "id": "a326a933-662d-49b4-a949-fe06c3d41de0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'user_id': 1, 'event_type': 'login', 'duration_seconds': 100, 'duration_minutes': 1.6666666666666667}, {'user_id': 3, 'event_type': 'logout', 'duration_seconds': 140, 'duration_minutes': 2.3333333333333335}, {'user_id': 4, 'event_type': 'purchase', 'duration_seconds': 160, 'duration_minutes': 2.6666666666666665}, {'user_id': 5, 'event_type': 'login', 'duration_seconds': 180, 'duration_minutes': 3.0}, {'user_id': 6, 'event_type': 'video_play', 'duration_seconds': 200, 'duration_minutes': 3.3333333333333335}, {'user_id': 8, 'event_type': 'share', 'duration_seconds': 240, 'duration_minutes': 4.0}, {'user_id': 9, 'event_type': 'video_play', 'duration_seconds': 260, 'duration_minutes': 4.333333333333333}, {'user_id': 10, 'event_type': 'login', 'duration_seconds': 280, 'duration_minutes': 4.666666666666667}, {'user_id': 11, 'event_type': 'logout', 'duration_seconds': 300, 'duration_minutes': 5.0}, {'user_id': 12, 'event_type': 'purchase', 'duration_seconds': 320, 'duration_minutes': 5.333333333333333}]\n" + ] + } + ], + "source": [ + "cleaned_events = []\n", + "for i in events:\n", + " if type(i[\"duration_seconds\"]) == int:\n", + " if i[\"duration_seconds\"] > 0:\n", + " cleaned_events.append(i)\n", + "\n", + "for i in cleaned_events:\n", + " i[\"duration_minutes\"] = i[\"duration_seconds\"] / 60\n", + "print(cleaned_events)" + ] + }, + { + "cell_type": "code", + "execution_count": 137, + "id": "6197570a-5c89-4612-b15d-cac22452fdd0", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'user_id': 1,\n", + " 'event_type': 'login',\n", + " 'duration_seconds': 100,\n", + " 'duration_minutes': 1.6666666666666667},\n", + " {'user_id': 3,\n", + " 'event_type': 'logout',\n", + " 'duration_seconds': 140,\n", + " 'duration_minutes': 2.3333333333333335},\n", + " {'user_id': 4,\n", + " 'event_type': 'purchase',\n", + " 'duration_seconds': 160,\n", + " 'duration_minutes': 2.6666666666666665},\n", + " {'user_id': 5,\n", + " 'event_type': 'login',\n", + " 'duration_seconds': 180,\n", + " 'duration_minutes': 3.0},\n", + " {'user_id': 6,\n", + " 'event_type': 'video_play',\n", + " 'duration_seconds': 200,\n", + " 'duration_minutes': 3.3333333333333335},\n", + " {'user_id': 8,\n", + " 'event_type': 'share',\n", + " 'duration_seconds': 240,\n", + " 'duration_minutes': 4.0},\n", + " {'user_id': 9,\n", + " 'event_type': 'video_play',\n", + " 'duration_seconds': 260,\n", + " 'duration_minutes': 4.333333333333333},\n", + " {'user_id': 10,\n", + " 'event_type': 'login',\n", + " 'duration_seconds': 280,\n", + " 'duration_minutes': 4.666666666666667},\n", + " {'user_id': 11,\n", + " 'event_type': 'logout',\n", + " 'duration_seconds': 300,\n", + " 'duration_minutes': 5.0},\n", + " {'user_id': 12,\n", + " 'event_type': 'purchase',\n", + " 'duration_seconds': 320,\n", + " 'duration_minutes': 5.333333333333333}]" + ] + }, + "execution_count": 137, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cleaned_events" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "id": "2a8c7546-de0c-43b6-8a4a-f6a27cdbf6fc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'login': 3, 'logout': 2, 'purchase': 2, 'video_play': 2, 'share': 1}" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "event_counts = {}\n", + "for event in cleaned_events:\n", + " etype = event['event_type']\n", + " event_counts[etype] = event_counts.get(etype, 0) + 1\n", + "event_counts" + ] + }, + { + "cell_type": "code", + "execution_count": 139, + "id": "40f4ca86-bb48-4f5c-9967-a3df5431c2e8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'login': 3, 'logout': 4, 'purchase': 4, 'video_play': 4, 'share': 4}\n" + ] + } + ], + "source": [ + "duration_sums = {}\n", + "duration_counts = {}\n", + "for i in cleaned_events:\n", + " i_type = i[\"event_type\"]\n", + " duration = float(i[\"duration_minutes\"])\n", + " duration_sums[i_type] = duration_sums.get(i_type,0) + duration\n", + " duration_counts[i_type] = duration_counts.get(i_type,0) + 1\n", + "avg_duration_minutes = {k:round(duration_sums[k]/duration_counts[k]) for k in duration_sums}\n", + "print(avg_duration_minutes)" + ] + }, + { + "cell_type": "code", + "execution_count": 140, + "id": "112ecf29-c8cf-4d4d-b5fc-9040fe70bcaa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 140, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_users = {i[\"user_id\"] for i in cleaned_events}\n", + "len_unique_users = len(unique_users)\n", + "len_unique_users" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "id": "d3f431d4-7019-43ec-a8ed-0ffdbbefbb57", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "metric , key , value \n", + "count, login , 3\n", + "count, logout , 2\n", + "count, purchase , 2\n", + "count, video_play , 2\n", + "count, share , 1\n", + "avg, login , 3\n", + "avg, logout , 4\n", + "avg, purchase , 4\n", + "avg, video_play , 4\n", + "avg, share , 4\n", + "unique_users,total , 10\n" + ] + } + ], + "source": [ + "csv_columns = [\"metric \" , \" key \" , \" value \"]\n", + "header = \",\".join(csv_columns)\n", + "\n", + "final_rows = [header]\n", + "\n", + "for i_type, count in event_counts.items():\n", + " final_rows.append(f\"count, {i_type} , {count}\")\n", + "\n", + "for i_type, avg in avg_duration_minutes.items():\n", + " final_rows.append(f\"avg, {i_type} , {avg}\")\n", + "\n", + "final_rows.append(f\"unique_users,total , {len_unique_users}\")\n", + "\n", + "csv_string = \"\\n\".join(final_rows)\n", + "\n", + "print(csv_string)" + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "id": "04f1e672-d591-4cc6-a8e8-96d28867f13f", + "metadata": {}, + "outputs": [], + "source": [ + "# csv_string dəyişənini \"summary_report.csv\" adlı fayla yazırıq\n", + "with open(\"csv_string.csv\", \"w\", encoding=\"utf-8\") as f:\n", + " f.write(csv_string)" + ] + } + ], + "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.9" + } + }, + "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..2966922 --- /dev/null +++ b/m1-01-task-3-functions.py @@ -0,0 +1,79 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 21, + "id": "0065aa78-7883-4b71-8606-9a0847876041", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1.0, 2.0, 3.0, 4.0, 5.0]\n" + ] + } + ], + "source": [ + "def lower_strip(a):\n", + " b = []\n", + " for x in a:\n", + " if x.isdigit() == True:\n", + " x = float(x)\n", + " b.append(x)\n", + " else:\n", + " return \"None\"\n", + " return b \n", + "a = [\"1\",\"2\",\"3\",\"4\",\"5\"]\n", + "print(lower_strip(a))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "aa95bf2e-5cc6-40a4-96d8-ad95fe4f47df", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c', 'd', 'e']\n" + ] + } + ], + "source": [ + "def lower_strip(a):\n", + " b = []\n", + " for x in a:\n", + " c = x.lower().strip()\n", + " b.append(c)\n", + " return b \n", + "\n", + "a = [\"A \",\"B \",\"C \",\" D \",\" E\"]\n", + "print(lower_strip(a))" + ] + } + ], + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}