diff --git a/m1-01-ipython-python-basics-lab.ipynb b/m1-01-ipython-python-basics-lab.ipynb new file mode 100644 index 0000000..75e5903 --- /dev/null +++ b/m1-01-ipython-python-basics-lab.ipynb @@ -0,0 +1,415 @@ +{ + "cells": [ + { + "metadata": {}, + "cell_type": "markdown", + "source": "Task1", + "id": "ea3b1054e6f32f40" + }, + { + "cell_type": "code", + "id": "5dc49819-0b39-47ce-9d6b-93d5a76031d6", + "metadata": {}, + "source": [ + "number=30\n", + "point=29.5\n", + "word=\"hello\"\n", + "grades=[23,39,14,45]\n", + "information={\"Name\":\"John\",\"Age\":35}" + ], + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "print(type(number))\n", + "print(type(point))\n", + "print(type(word))\n", + "print(type(grades))\n", + "print(type(information))" + ], + "id": "41cdeb06a6ddc367", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "int,float,str,list,dictionary", + "id": "16dd4173041ab50c" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "word.capitalize()\n", + "grades.append(50)\n", + "information.update({\"Age\": 40})" + ], + "id": "dfef53be6e36abbf", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "First one returns str with first character large others small.Second one adds 50 to list.Third one updates age to 40 in dictionary", + "id": "24264817a3e05450" + }, + { + "metadata": {}, + "cell_type": "code", + "source": "help(grades)", + "id": "f8b26e2e4ef2c741", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "Methods defined here:\n", + "\n", + " | append(self, object, /)\n", + "\n", + " | clear(self, /)\n", + " |\n", + " | copy(self, /)\n", + " |\n", + " | count(self, value, /)\n", + " |\n", + " | extend(self, iterable, /)\n", + " |\n", + " | index(self, value, start=0, stop=9223372036854775807, /)\n", + " |\n", + " | insert(self, index, object, /)\n", + " |\n", + " | pop(self, index=-1, /)\n", + " |\n", + " | remove(self, value, /)\n", + " |\n", + " | reverse(self, /)\n", + " |\n", + " | sort(self, /, *, key=None, reverse=False)using tab,help() or dot" + ], + "id": "a8cf347c0e83f64d" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "Task2", + "id": "34b250dba8d01821" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "values=[1,2,3,4,5,6]\n", + "alias=values\n", + "alias.append(7)\n", + "print(values)\n", + "print(alias)" + ], + "id": "7199ed9e5345243e", + "outputs": [], + "execution_count": null + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-02-18T09:12:55.469821Z", + "start_time": "2026-02-18T09:12:55.446296700Z" + } + }, + "cell_type": "code", + "source": [ + "copied=values.copy()\n", + "copied.append(10)\n", + "print(values)\n", + "print(copied)" + ], + "id": "21f651888726ac01", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7]\n", + "[1, 2, 3, 4, 5, 6, 7, 10]\n" + ] + } + ], + "execution_count": 18 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-02-18T09:12:56.949351400Z", + "start_time": "2026-02-18T09:12:56.940132Z" + } + }, + "cell_type": "code", + "source": [ + "record={\"one\":1,\"two\":2,\"three\":3}\n", + "record_alias=record\n", + "record_alias[\"four\"]=4\n", + "print(record)\n", + "print(record_alias)" + ], + "id": "8549e1b7778ec996", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'one': 1, 'two': 2, 'three': 3, 'four': 4}\n", + "{'one': 1, 'two': 2, 'three': 3, 'four': 4}\n" + ] + } + ], + "execution_count": 19 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-02-18T09:12:58.295337600Z", + "start_time": "2026-02-18T09:12:58.261010600Z" + } + }, + "cell_type": "code", + "source": [ + "record_copy=record.copy()\n", + "record_copy[\"five\"]=5\n", + "print(record)\n", + "print(record_copy)" + ], + "id": "6d3ecf0e88102b41", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'one': 1, 'two': 2, 'three': 3, 'four': 4}\n", + "{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}\n" + ] + } + ], + "execution_count": 20 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": ".copy creates shallow copy change on this doesn't affect main one.But changing assigned one changes first one also.", + "id": "86d5c3a04673edb4" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "Task4", + "id": "f2877dcb53a6f66e" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-02-18T09:14:38.640593600Z", + "start_time": "2026-02-18T09:14:38.615982200Z" + } + }, + "cell_type": "code", + "source": [ + "user_activity = [\n", + " {\"user_id\": 101, \"event_type\": \"login\", \"duration_seconds\": 12},\n", + " {\"user_id\": 102, \"event_type\": \"video_play\", \"duration_seconds\": 360},\n", + " {\"user_id\": 103, \"event_type\": \"file_upload\", \"duration_seconds\": 95},\n", + " {\"user_id\": 104, \"event_type\": \"logout\", \"duration_seconds\": 5},\n", + " {\"user_id\": 105, \"event_type\": \"page_view\", \"duration_seconds\": 45},\n", + " {\"user_id\": 106, \"event_type\": \"comment_post\", \"duration_seconds\": 18},\n", + " {\"user_id\": 107, \"event_type\": \"profile_update\", \"duration_seconds\": 27},\n", + " {\"user_id\": 108, \"event_type\": \"video_play\", \"duration_seconds\": 540},\n", + " {\"user_id\": 109, \"event_type\": \"download\", \"duration_seconds\": 120},\n", + " {\"user_id\": 110, \"event_type\": \"login\", \"duration_seconds\": -15},\n", + " {\"user_id\": 111, \"event_type\": \"file_upload\", \"duration_seconds\": \"60\"},\n", + " {\"user_id\": 108, \"event_type\": \"page_view\", \"duration_seconds\": 33},\n", + "]\n", + "cleaned_events=[]\n", + "for item in user_activity:\n", + " if type(item[\"duration_seconds\"]) == int and item[\"duration_seconds\"]>0:\n", + " item[\"duration_minutes\"]=item[\"duration_seconds\"]/60\n", + " cleaned_events.append(item)\n", + "print(len(cleaned_events))\n", + "print(cleaned_events)" + ], + "id": "605a96fa4f618c0f", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "[{'user_id': 101, 'event_type': 'login', 'duration_seconds': 12, 'duration_minutes': 0.2}, {'user_id': 102, 'event_type': 'video_play', 'duration_seconds': 360, 'duration_minutes': 6.0}, {'user_id': 103, 'event_type': 'file_upload', 'duration_seconds': 95, 'duration_minutes': 1.5833333333333333}, {'user_id': 104, 'event_type': 'logout', 'duration_seconds': 5, 'duration_minutes': 0.08333333333333333}, {'user_id': 105, 'event_type': 'page_view', 'duration_seconds': 45, 'duration_minutes': 0.75}, {'user_id': 106, 'event_type': 'comment_post', 'duration_seconds': 18, 'duration_minutes': 0.3}, {'user_id': 107, 'event_type': 'profile_update', 'duration_seconds': 27, 'duration_minutes': 0.45}, {'user_id': 108, 'event_type': 'video_play', 'duration_seconds': 540, 'duration_minutes': 9.0}, {'user_id': 109, 'event_type': 'download', 'duration_seconds': 120, 'duration_minutes': 2.0}, {'user_id': 108, 'event_type': 'page_view', 'duration_seconds': 33, 'duration_minutes': 0.55}]\n" + ] + } + ], + "execution_count": 31 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "Task5", + "id": "73e7f82dade7e165" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-02-18T09:13:33.551221600Z", + "start_time": "2026-02-18T09:13:33.522042600Z" + } + }, + "cell_type": "code", + "source": [ + "event_counts={}\n", + "for item in cleaned_events:\n", + " event_counts[item[\"event_type\"]]=0\n", + "for x in cleaned_events:\n", + " event_counts[x[\"event_type\"]]+=1\n", + "print(event_counts)" + ], + "id": "c4f2c5c79115a66a", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'login': 1, 'video_play': 2, 'file_upload': 1, 'logout': 1, 'page_view': 2, 'comment_post': 1, 'profile_update': 1, 'download': 1}\n" + ] + } + ], + "execution_count": 26 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-02-18T09:13:34.872576700Z", + "start_time": "2026-02-18T09:13:34.864320300Z" + } + }, + "cell_type": "code", + "source": [ + "total_minutes = {}\n", + "counts = {}\n", + "\n", + "for item in cleaned_events:\n", + " event = item[\"event_type\"]\n", + " minutes = item[\"duration_minutes\"]\n", + "\n", + " if event not in total_minutes:\n", + " total_minutes[event] = 0\n", + " counts[event] = 0\n", + "\n", + " total_minutes[event] += minutes\n", + " counts[event] += 1\n", + "average_minute = {event: total_minutes[event]/counts[event] for event in total_minutes}\n", + "\n", + "print(average_minute)\n" + ], + "id": "b8f538b1cb3709b1", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'login': 0.2, 'video_play': 7.5, 'file_upload': 1.5833333333333333, 'logout': 0.08333333333333333, 'page_view': 0.65, 'comment_post': 0.3, 'profile_update': 0.45, 'download': 2.0}\n" + ] + } + ], + "execution_count": 27 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-02-18T09:14:44.120939200Z", + "start_time": "2026-02-18T09:14:44.094385Z" + } + }, + "cell_type": "code", + "source": [ + "import csv\n", + "import io\n", + "\n", + "output = io.StringIO()\n", + "writer = csv.writer(output)\n", + "writer.writerow([\"metric\", \"key\", \"value\"])\n", + "\n", + "for event, count in event_counts.items():\n", + " writer.writerow([\"count\", event, count])\n", + "\n", + "for event, avg in average_minute.items():\n", + " writer.writerow([\"average_duration_seconds\", event, round(avg, 2)])\n", + "\n", + "# Unique users\n", + "unique=[]\n", + "for item in cleaned_events:\n", + " unique.append(item[\"user_id\"])\n", + "unique_users=set(unique)\n", + "writer.writerow([\"unique_users\", \"all\", len(unique_users)])\n", + "\n", + "csv_string = output.getvalue()\n", + "print(csv_string)\n", + "filename = \"metrics.csv\"\n", + "with open(filename, \"w\", newline=\"\") as f:\n", + " f.write(csv_string)\n", + "\n", + "with open(filename, \"r\") as f:\n", + " reader = csv.DictReader(f)\n", + " for row in reader:\n", + " if row[\"metric\"] == \"count\" and row[\"key\"] == \"click\":\n", + " assert int(row[\"value\"]) == counts[\"click\"]\n", + " print(\"Confirmed count for 'click':\", row[\"value\"])\n", + " break" + ], + "id": "300823538be25020", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "metric,key,value\r\n", + "count,login,1\r\n", + "count,video_play,2\r\n", + "count,file_upload,1\r\n", + "count,logout,1\r\n", + "count,page_view,2\r\n", + "count,comment_post,1\r\n", + "count,profile_update,1\r\n", + "count,download,1\r\n", + "average_duration_seconds,login,0.2\r\n", + "average_duration_seconds,video_play,7.5\r\n", + "average_duration_seconds,file_upload,1.58\r\n", + "average_duration_seconds,logout,0.08\r\n", + "average_duration_seconds,page_view,0.65\r\n", + "average_duration_seconds,comment_post,0.3\r\n", + "average_duration_seconds,profile_update,0.45\r\n", + "average_duration_seconds,download,2.0\r\n", + "unique_users,all,9\r\n", + "\n" + ] + } + ], + "execution_count": 32 + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "name": "python3", + "language": "python" + }, + "language_info": { + "name": "" + } + }, + "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..ce37468 --- /dev/null +++ b/m1-01-task-3-functions.py @@ -0,0 +1,25 @@ +def converter(a): + if type(a) == str: + if a.isdigit(): + return float(a) + else: + return None + else: + print("not str") + return 0 + +def cleaner(text): + if type(text) == str: + result=text.strip().lower() + return result + else: + print("not str") + return 0 + +a=["90c","85",74,"79.8",12.4] +b= [" Hello World ", 48," OpenAI ","gPt-5 Mini"," MACHINE learning ","Data Science "," ChatGPT "," tEsTiNg STRINGS "] + +for x in a: + print(converter(x)) +for y in b: + print(cleaner(y)) \ No newline at end of file diff --git a/metrics.csv b/metrics.csv new file mode 100644 index 0000000..6c079d8 --- /dev/null +++ b/metrics.csv @@ -0,0 +1,18 @@ +metric,key,value +count,login,1 +count,video_play,2 +count,file_upload,1 +count,logout,1 +count,page_view,2 +count,comment_post,1 +count,profile_update,1 +count,download,1 +average_duration_seconds,login,0.2 +average_duration_seconds,video_play,7.5 +average_duration_seconds,file_upload,1.58 +average_duration_seconds,logout,0.08 +average_duration_seconds,page_view,0.65 +average_duration_seconds,comment_post,0.3 +average_duration_seconds,profile_update,0.45 +average_duration_seconds,download,2.0 +unique_users,all,9