diff --git a/m1-01-ipython-python-basics-lab.ipynb.ipynb b/m1-01-ipython-python-basics-lab.ipynb.ipynb new file mode 100644 index 0000000..2a2076f --- /dev/null +++ b/m1-01-ipython-python-basics-lab.ipynb.ipynb @@ -0,0 +1,640 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "id": "76169909-c7ec-4360-9431-e4afccbdd220", + "metadata": {}, + "outputs": [], + "source": [ + "# Task 1\n", + "a = \"Shamil\"\n", + "b = 3.12\n", + "c = 10\n", + "d = [1,2,3,4,5]\n", + "e = {\"a\": 1, \"b\": 2, \"c\": 3}" + ] + }, + { + "cell_type": "markdown", + "id": "0cff2c8b-bb51-4a75-8e2c-dcb3af51ad1f", + "metadata": {}, + "source": [ + "type of a is str, \n", + "type of b is float, \n", + "type of c is int, \n", + "type of d is list, \n", + "type of e is dict, " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "13c9c23d-8c5c-4ea8-a2ce-18e68e8cf3d7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "String metod result: HELLO\n", + "List metod result: [1, 2, 3, 4]\n", + "Dictionary metod result: ['a', 'b']\n" + ] + } + ], + "source": [ + "my_string = \"hello\"\n", + "my_list = [1, 2, 3]\n", + "my_dict = {\"a\": 1, \"b\": 2}\n", + "\n", + "string_result = my_string.upper() \n", + "print(\"String metod result:\", string_result)\n", + "\n", + "my_list.append(4) \n", + "print(\"List metod result:\", my_list)\n", + "\n", + "dict_keys = my_dict.keys() \n", + "print(\"Dictionary metod result:\", list(dict_keys))\n" + ] + }, + { + "cell_type": "markdown", + "id": "bed7a3a7-00b3-4f32-b5b5-5159ba37db07", + "metadata": {}, + "source": [ + "String (my_string.upper()):\n", + "This method converted all letters in the string to uppercase.\n", + "Example: \"hello\" -> \"HELLO\"\n", + "\n", + "List (my_list.append(4)):\n", + "This method added a new element to the list.\n", + "Example: [1, 2, 3] -> [1, 2, 3, 4]\n", + "\n", + "Dictionary (my_dict.keys()):\n", + "This method returned all the keys in the dictionary.\n", + "Example: {\"a\": 1, \"b\": 2} -> [\"a\", \"b\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "f77a5946-1556-4420-9d89-868528b6c534", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function get:\n", + "\n", + "get(key, default=None, /) method of builtins.dict instance\n", + " Return the value for key if key is in the dictionary, else default.\n", + "\n" + ] + } + ], + "source": [ + "#Using dot + Tab\n", + "\n", + "\n", + "help(my_dict.get)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "8b30add0-f75a-4edc-821a-196b65203191", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class dict in module builtins:\n", + "\n", + "class dict(object)\n", + " | dict() -> new empty dictionary\n", + " | dict(mapping) -> new dictionary initialized from a mapping object's\n", + " | (key, value) pairs\n", + " | dict(iterable) -> new dictionary initialized as if via:\n", + " | d = {}\n", + " | for k, v in iterable:\n", + " | d[k] = v\n", + " | dict(**kwargs) -> new dictionary initialized with the name=value pairs\n", + " | in the keyword argument list. For example: dict(one=1, two=2)\n", + " | \n", + " | Built-in subclasses:\n", + " | StgDict\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __contains__(self, key, /)\n", + " | True if the dictionary has the specified key, else False.\n", + " | \n", + " | __delitem__(self, key, /)\n", + " | Delete self[key].\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(...)\n", + " | x.__getitem__(y) <==> x[y]\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | __ior__(self, value, /)\n", + " | Return self|=value.\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self size of D in memory, in bytes\n", + " | \n", + " | clear(...)\n", + " | D.clear() -> None. Remove all items from D.\n", + " | \n", + " | copy(...)\n", + " | D.copy() -> a shallow copy of D\n", + " | \n", + " | get(self, key, default=None, /)\n", + " | Return the value for key if key is in the dictionary, else default.\n", + " | \n", + " | items(...)\n", + " | D.items() -> a set-like object providing a view on D's items\n", + " | \n", + " | keys(...)\n", + " | D.keys() -> a set-like object providing a view on D's keys\n", + " | \n", + " | pop(...)\n", + " | D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n", + " | \n", + " | If the key is not found, return the default if given; otherwise,\n", + " | raise a KeyError.\n", + " | \n", + " | popitem(self, /)\n", + " | Remove and return a (key, value) pair as a 2-tuple.\n", + " | \n", + " | Pairs are returned in LIFO (last-in, first-out) order.\n", + " | Raises KeyError if the dict is empty.\n", + " | \n", + " | setdefault(self, key, default=None, /)\n", + " | Insert key with a value of default if key is not in the dictionary.\n", + " | \n", + " | Return the value for key if key is in the dictionary, else default.\n", + " | \n", + " | update(...)\n", + " | D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n", + " | If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n", + " | If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n", + " | In either case, this is followed by: for k in F: D[k] = F[k]\n", + " | \n", + " | values(...)\n", + " | D.values() -> an object providing a view on D's values\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Class methods defined here:\n", + " | \n", + " | __class_getitem__(...)\n", + " | See PEP 585\n", + " | \n", + " | fromkeys(iterable, value=None, /)\n", + " | Create a new dictionary with keys from iterable and values set to value.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs)\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data and other attributes defined here:\n", + " | \n", + " | __hash__ = None\n", + "\n" + ] + } + ], + "source": [ + "help(dict)" + ] + }, + { + "cell_type": "markdown", + "id": "5bee1f06-7f89-4085-9b43-b63522a24666", + "metadata": {}, + "source": [ + "List object methods: .append(), .pop(), .sort(), etc.\n", + "\n", + "String object methods: .upper(), .lower(), .split(), etc.\n", + "\n", + "Dictionary object methods: .keys(), .values(), .get(), etc." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "ff4a6043-f2ab-455e-a966-f94bfa9d09a9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Task 2\n", + "\n", + "values = [1,2,3]\n", + "alias = values\n", + "alias" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "71f5b697-16a0-4e2e-a19e-6eddfec69653", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 5]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alias.append(5)\n", + "alias" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f7b11ddf-11af-4079-8201-466a727fe2cf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 5, 11]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alias.append(11)\n", + "alias" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "af4523f4-f12b-4d24-a92d-3b4792a86598", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 5, 11]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "values" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "9672b616-cf5f-4be1-bdc1-55d4c7e959ea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 5, 11]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "copy_values = values.copy()\n", + "copy_values" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "cc4c1dec-5f35-4c66-9122-53a897f941d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "values list: [1, 2, 3, 5, 11]\n", + "copy_values list: [1, 2, 3, 5, 11, 20, 111, 20, 111, 20, 111, 20, 111]\n" + ] + } + ], + "source": [ + "copy_values.append(20)\n", + "copy_values.append(111)\n", + "print(\"values list: \" , values)\n", + "print(\"copy_values list: \" , copy_values)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "f96d57ad-39ac-4998-a3bb-58fa48fbb75a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'Shamil', 'age': 20}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "record = {\"name\": \"Shamil\", \"age\": 20, \"city\": \"Baku\"}\n", + "record_alias = record\n", + "record_alias" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "902ab4e0-dd4e-4a1c-b335-712eae0d2121", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'name': 'Shamil', 'age': 20, 'city': 'Berlin'}\n", + "{'name': 'Shamil', 'age': 20, 'city': 'Berlin'}\n" + ] + } + ], + "source": [ + "record_alias[\"city\"] = \"Berlin\"\n", + "print(record)\n", + "print(record_alias)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "6784b584-f7a5-4761-adc0-0960649ca359", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'name': 'Shamil', 'age': 20, 'city': 'Berlin'}\n", + "{'name': 'Shamil', 'age': 30, 'city': 'Berlin'}\n" + ] + } + ], + "source": [ + "record_copy = record.copy()\n", + "record_copy[\"age\"] = 30\n", + "print(record)\n", + "print(record_copy)" + ] + }, + { + "cell_type": "markdown", + "id": "7c627932-7124-4085-a74e-f1886bf443b4", + "metadata": {}, + "source": [ + "Aliasing: If you assign one variable to another (e.g., alias = values), both names point to the same object. Changing it through one name also changes the other.\n", + "\n", + "Copying: If you make a copy (e.g., values.copy()), it creates a new object. Changes to the copy do not affect the original.\n", + "\n", + "For dictionaries: The same rules apply. An alias shares the object, a copy creates a new one.\n", + "\n", + "Why it matters: When passing mutable objects into functions, using an alias can modify the original object, but a copy keeps it safe." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "4830795d-2daa-409f-981e-c0bdb947af6e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of cleaned events: 10\n", + "Keys in first cleaned record: dict_keys(['user_id', 'event_type', 'duration_seconds', 'duration_minutes'])\n", + "First cleaned record: {'user_id': 1, 'event_type': 'login', 'duration_seconds': 120.0, 'duration_minutes': 2.0}\n", + "Second cleaned record: {'user_id': 2, 'event_type': 'logout', 'duration_seconds': 300.0, 'duration_minutes': 5.0}\n" + ] + } + ], + "source": [ + "# Task 4\n", + "events = [\n", + " {\"user_id\": 1, \"event_type\": \"login\", \"duration_seconds\": 120},\n", + " {\"user_id\": 2, \"event_type\": \"logout\", \"duration_seconds\": 300},\n", + " {\"user_id\": 3, \"event_type\": \"purchase\", \"duration_seconds\": 180},\n", + " {\"user_id\": 4, \"event_type\": \"login\", \"duration_seconds\": -50},\n", + " {\"user_id\": 5, \"event_type\": \"logout\", \"duration_seconds\": 90},\n", + " {\"user_id\": 6, \"event_type\": \"purchase\", \"duration_seconds\": \"abc\"},\n", + " {\"user_id\": 7, \"event_type\": \"login\", \"duration_seconds\": 240},\n", + " {\"user_id\": 8, \"event_type\": \"logout\", \"duration_seconds\": 60},\n", + " {\"user_id\": 9, \"event_type\": \"purchase\", \"duration_seconds\": 150},\n", + " {\"user_id\": 10, \"event_type\": \"login\", \"duration_seconds\": 200},\n", + " {\"user_id\": 11, \"event_type\": \"logout\", \"duration_seconds\": 180},\n", + " {\"user_id\": 12, \"event_type\": \"purchase\", \"duration_seconds\": 210}\n", + "]\n", + "\n", + "cleaned_events = []\n", + "\n", + "for event in events:\n", + " duration = event.get(\"duration_seconds\")\n", + " \n", + " try:\n", + " duration = float(duration)\n", + " if duration > 0:\n", + " \n", + " new_event = event.copy()\n", + " new_event[\"duration_seconds\"] = duration\n", + " new_event[\"duration_minutes\"] = duration / 60\n", + " cleaned_events.append(new_event)\n", + " except (ValueError, TypeError):\n", + " pass \n", + "\n", + "\n", + "print(\"Number of cleaned events:\", len(cleaned_events))\n", + "\n", + "print(\"Keys in first cleaned record:\", cleaned_events[0].keys())\n", + "\n", + "\n", + "print(\"First cleaned record:\", cleaned_events[0])\n", + "print(\"Second cleaned record:\", cleaned_events[1])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "ba21c27d-4e20-4c0e-9104-4dbcb75648ef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "metric,key,value\n", + "count,login,3\n", + "count,logout,4\n", + "count,purchase,3\n", + "average_duration,login,3.11\n", + "average_duration,logout,2.62\n", + "average_duration,purchase,3.00\n", + "unique_users,,10\n" + ] + } + ], + "source": [ + "# Task 5\n", + "event_counts = {}\n", + "for event in cleaned_events:\n", + " etype = event[\"event_type\"]\n", + " event_counts[etype] = event_counts.get(etype, 0) + 1\n", + "\n", + "duration_totals = {}\n", + "duration_counts = {}\n", + "\n", + "for event in cleaned_events:\n", + " etype = event[\"event_type\"]\n", + " duration = event[\"duration_minutes\"]\n", + " duration_totals[etype] = duration_totals.get(etype, 0) + duration\n", + " duration_counts[etype] = duration_counts.get(etype, 0) + 1\n", + "\n", + "avg_duration = {etype: duration_totals[etype]/duration_counts[etype] for etype in duration_totals}\n", + "\n", + "\n", + "unique_users = set()\n", + "for event in cleaned_events:\n", + " unique_users.add(event[\"user_id\"])\n", + "unique_user_count = len(unique_users)\n", + "\n", + "assert sum(event_counts.values()) == len(cleaned_events), \"Event count mismatch!\"\n", + "\n", + "csv_lines = [\"metric,key,value\"]\n", + "\n", + "for etype, count in event_counts.items():\n", + " csv_lines.append(f\"count,{etype},{count}\")\n", + "\n", + "for etype, avg in avg_duration.items():\n", + " csv_lines.append(f\"average_duration,{etype},{avg:.2f}\")\n", + "\n", + "\n", + "csv_lines.append(f\"unique_users,,{unique_user_count}\")\n", + "\n", + "\n", + "csv_string = \"\\n\".join(csv_lines)\n", + "\n", + "print(csv_string)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "688e668a-c27c-4816-9d43-9768aa998044", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Venv(DSML)", + "language": "python", + "name": "venv_dsml" + }, + "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.11.13" + } + }, + "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..1da55fd --- /dev/null +++ b/m1-01-task-3-functions.py @@ -0,0 +1,18 @@ +def string_to_float(text): + try: + return float(text) + except ValueError: + return None + +def clean_str(text): + return text.strip().upper() + + +test_inputs = ["hello", " 3.14 ", "", "abc", " 7 ", "4.5"] + +float_results = [string_to_float(x) for x in test_inputs] + +cleaned_results = [clean_str(x) for x in test_inputs] + +print("string_to_float results:", float_results) +print("clean_str results:", cleaned_results) \ No newline at end of file