From ca263283643258a25df9b963d86c4325027a4a46 Mon Sep 17 00:00:00 2001 From: Vladimir <87763796+moiseevv@users.noreply.github.com> Date: Mon, 24 Oct 2022 15:48:47 +0700 Subject: [PATCH 1/6] python1 --- Untitled_1.ipynb | 499 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 499 insertions(+) create mode 100644 Untitled_1.ipynb diff --git a/Untitled_1.ipynb b/Untitled_1.ipynb new file mode 100644 index 0000000..3c4e2b2 --- /dev/null +++ b/Untitled_1.ipynb @@ -0,0 +1,499 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 15, + "id": "87b4e236-58fe-4534-8104-cafbd0bcb2ce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current Version of Python interpreter we are using- 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)]\n" + ] + } + ], + "source": [ + "import sys \n", + "# Getting interpreter version as a result \n", + "print(\"Current Version of Python interpreter we are using-\", sys.version)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1028fb6d-7d7c-4069-9534-bdf74f40cc01", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10**2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "798f186d-4cf0-4aff-856d-2d2a39637b06", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(9**(1/2))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d8748644-eab9-4e0e-a229-4858f975e5c8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5//2" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "e6305a25-99cc-45a9-81cd-530ef5fe86a7", + "metadata": {}, + "outputs": [], + "source": [ + "def av(*ar):\n", + " sum_ar = sum(ar)\n", + " len_ar = len(ar)\n", + " result = sum_ar/len_ar\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "3ab92fcf-3dfc-4a3e-84f6-9ba58d26fe91", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = [1,2,3]\n", + "av(1,2,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "18a88c13-a445-406d-8bfb-cb03f2554048", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "dcea747d-8c32-4832-9b24-dd7732fee8f6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[0+2]" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "e1a02b36-88dc-48c2-a50e-4cf8489586c3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.9" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + " df_2 = [1,2,2,2,2,2,2]\n", + "av(*df_2, *df)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "8452f63e-2139-4c21-b4ad-96e966fd774a", + "metadata": {}, + "outputs": [], + "source": [ + "i = 6" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "a56310df-a913-49c1-8de1-5c7a751f0492", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2111452807568" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "id(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "9b1ca221-b724-4666-a634-7878648ca92a", + "metadata": {}, + "outputs": [], + "source": [ + "y = 9" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "64c093c8-e018-4c91-b768-f87267f8f932", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2111452807568" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "id(i)\n", + "id(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "86b3db87-e827-4534-a43d-1b352c0ffcbb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "number is x: False\n" + ] + } + ], + "source": [ + "print('number is x:', i is y)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "90886ec4-0779-407b-87bd-a955c5711a2c", + "metadata": {}, + "outputs": [], + "source": [ + "fact = 1\n", + "def fl(n):\n", + " if n <=1:\n", + " return 1\n", + " else: r = n*fl(n-1)\n", + " \n", + " return r" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "9097a50a-9f54-4708-9153-1ef3372f2b15", + "metadata": {}, + "outputs": [], + "source": [ + "g = fl(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "96657577-2fd4-4174-b545-2dd8f0089620", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "g" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "8052d8a4-46a1-4321-ac49-fb2262f3e3e7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "1\n", + "2\n", + "6\n", + "24\n", + "120\n", + "720\n", + "5040\n", + "40320\n", + "362880\n", + "3628800\n", + "39916800\n", + "479001600\n", + "6227020800\n" + ] + } + ], + "source": [ + "for i in range(0,14):\n", + " print(fl(i))" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "090c66e9-8edf-43a7-b6f0-f2b875070a63", + "metadata": {}, + "outputs": [], + "source": [ + "tumpl1 = (10,20,10)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "1dcaa050-8f0d-439d-acc9-9e55237a26da", + "metadata": {}, + "outputs": [], + "source": [ + "tumple2 = (10,1,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "66e387e4-5c3e-4105-8f1b-d5b38e83746c", + "metadata": {}, + "outputs": [], + "source": [ + "tumpl3 = tumple2+tumpl1" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "dc4c33f0-e853-40d9-8021-b9c20e923944", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 1, 2, 10, 20, 10)\n" + ] + } + ], + "source": [ + "print(tumpl3)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "d6506ef6-3a6b-4ca9-aa01-23cbbbf9e108", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(0, 'red'), (1, 'orange'), (2, 'yellow')]" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colors = ['red', 'orange', 'yellow']\n", + "list(enumerate(colors))" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "a9ee3d53-a596-4ac8-b08e-a99e4fac9608", + "metadata": {}, + "outputs": [], + "source": [ + "sample_list = []\n", + "s = 'abc'\n", + "sample_list.extend(s)" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "44108327-34bb-47a0-b00e-26ae5913f618", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c', 1, 2, 3]" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sample_list" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "178c30be-d83d-4dc6-a500-5a83be6313ac", + "metadata": {}, + "outputs": [], + "source": [ + "t = (1, 2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "225baa50-fca0-422e-8b29-b723bc2786fd", + "metadata": {}, + "outputs": [], + "source": [ + "sample_list.extend(t)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "58d95999-17d9-4e08-b6c9-77bc6b5c0834", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.10.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 34ec1670837b587c4e681c03dff539a40a5695c0 Mon Sep 17 00:00:00 2001 From: Vladimir <87763796+moiseevv@users.noreply.github.com> Date: Tue, 25 Oct 2022 08:33:39 +0700 Subject: [PATCH 2/6] lambda --- Untitled_2.ipynb | 350 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 350 insertions(+) create mode 100644 Untitled_2.ipynb diff --git a/Untitled_2.ipynb b/Untitled_2.ipynb new file mode 100644 index 0000000..d287c32 --- /dev/null +++ b/Untitled_2.ipynb @@ -0,0 +1,350 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "e03c8ebf-3444-48bd-a90b-e19d9e558116", + "metadata": {}, + "outputs": [], + "source": [ + "color_names = [1,2,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "12078d2b-5dfb-4ca6-8714-c8c03ad3c151", + "metadata": {}, + "outputs": [], + "source": [ + "copied_list = color_names.copy()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "3a89ed70-b217-497d-8a3e-e484581eeb5f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l1 is copied_list" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9e275b98-a866-47ce-9f0a-19a763432ef2", + "metadata": {}, + "outputs": [], + "source": [ + "l1 = copied_list" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "a92906d1-4da6-4178-b6a5-bfd6265e211c", + "metadata": {}, + "outputs": [], + "source": [ + "list2 = [item for item in range(1, 6+1)]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "14f206b3-2a1a-4398-9257-e6e4e5c8f8a1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list2" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "89a930e6-8f17-49e2-9bec-1d320df20743", + "metadata": {}, + "outputs": [], + "source": [ + "def is_odd(x):\n", + " return x*2" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "836c0de9-e8a5-4d70-a19d-fd9c28ad5309", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 3, 7, 1, 9, 4, 2, 8, 5, 6]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[item for item in numbers if is_odd(item)]" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "67c70f0b-e800-45f6-8910-0901c14cc585", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 5]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(lambda x: x % 2 != 0, list2))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c129f974-830d-4ff6-8f4d-4709d0a46f77", + "metadata": {}, + "outputs": [], + "source": [ + "numbers = [10, 3, 7, 1, 9, 4, 2, 8, 5, 6]" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "04aa966d-bdab-4864-be72-a93be8f307f8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[100, 9, 49, 1, 81, 16, 4, 64, 25, 36]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(lambda x: x ** 2, numbers))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "8dc445c0-e4c8-47d7-8dad-3db71881e3a7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[100, 9, 49, 1, 81, 16, 4, 64, 25, 36]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[item ** 2 for item in numbers]" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "4df731b9-6760-4fcd-b3db-55e89c0b1e76", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[9, 49, 1, 81, 25]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(lambda x: x ** 2, filter(lambda x: x % 2 != 0, numbers)))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "009512e2-a2ad-4b1b-b669-1ca474ca5629", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[9, 49, 1, 81, 25]" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x ** 2 for x in numbers if x % 2 != 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "f8962103-dc68-4a08-9fdd-e6d6b5fab363", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Red' < 'orange'" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "ef4d098d-458b-41f3-ae4f-96a5740247f9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "82" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ord('R')" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "3311a245-b013-410c-b144-39087f6cd4d2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "111" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ord('o')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "96bf8dd7-258c-40b3-bcd4-ff302b97d34e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Name=ABob; GPA=3.5\n", + "Name=Sue; GPA=5.0\n", + "Name=Amanda; GPA=3.75\n" + ] + } + ], + "source": [ + "names = ['ABob', 'Sue', 'Amanda']\n", + "grade_point_averages = [3.5, 5.0, 3.75]\n", + "for name, gpa in zip(names, grade_point_averages):\n", + " print(f'Name={name}; GPA={gpa}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f684b13b-9fbc-4d2a-8fc3-c65a1dc69a19", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.10.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 1f568eedcf6bd2bf94ee6bf9a50b5b3065ee982c Mon Sep 17 00:00:00 2001 From: Vladimir <87763796+moiseevv@users.noreply.github.com> Date: Tue, 25 Oct 2022 20:34:22 +0700 Subject: [PATCH 3/6] use arg --- use_arg.ipynb | 403 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 403 insertions(+) create mode 100644 use_arg.ipynb diff --git a/use_arg.ipynb b/use_arg.ipynb new file mode 100644 index 0000000..c14db80 --- /dev/null +++ b/use_arg.ipynb @@ -0,0 +1,403 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "639371c1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome\n", + "to\n", + "\n", + "Python!\n" + ] + } + ], + "source": [ + "print('Welcome\\nto\\n\\nPython!')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7b704a41", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Display \"hi\" and 'bye' in quotes\n" + ] + } + ], + "source": [ + "print(\"\"\"Display \"hi\" and 'bye' in quotes\"\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "35633e34", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a=10,5\n", + "type(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "30e4fa59", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10, 5)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "93ee8a5e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "9298c79d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(36, 27, 12)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "38215670", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "0505c100", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 5) < > 20 < > 30\n" + ] + } + ], + "source": [ + "print(a, 20, 30, sep=' < > ')" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "56cbc069", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "source": [ + "for counter in range(5,10+1):\n", + " print(counter)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "d3d8e984", + "metadata": {}, + "outputs": [], + "source": [ + "from decimal import Decimal" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "5bafaea2", + "metadata": {}, + "outputs": [], + "source": [ + "principal = Decimal('1000.00')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "ab1eaace", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Decimal('1000.00')" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "principal" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "af5d6933", + "metadata": {}, + "outputs": [], + "source": [ + "principal = principal+1" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "6dacd450", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Decimal('1001.00')" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "principal" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "66a06789", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 1 30.00\n", + " 2 180.00\n", + " 3 1080.00\n", + " 4 6480.00\n", + " 5 38880.00\n", + " 6 233280.00\n", + " 7 1399680.00\n", + " 8 8398080.00\n", + " 9 50388480.00\n", + "10 302330880.00\n" + ] + } + ], + "source": [ + "for year in range(1, 11):\n", + " principal = 5\n", + " rate = 5\n", + " amount = principal * (1 + rate) ** year\n", + " print(f'{year:>2}{amount:>20.2f}')" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "1de14f2a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "397\n", + "5\n" + ] + } + ], + "source": [ + "grades = [85, 93, 45, 89, 85]\n", + "print(sum(grades))\n", + "print(len(grades))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "a471d64e", + "metadata": {}, + "outputs": [], + "source": [ + "def aver(*ar):\n", + " '''Определение среднего'''\n", + " sum_a = sum(*ar)\n", + " len_a = len(*ar)\n", + " h = sum_a/len_a\n", + " return h" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "bf7c34e0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13.333333333333334" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lis = [10,20,10]\n", + "aver(lis)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "8fd6921f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j = (10,10)\n", + "aver(j)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ac42681", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 197c797756485022740ec7dfcdd2e41961748d4d Mon Sep 17 00:00:00 2001 From: Vladimir <87763796+moiseevv@users.noreply.github.com> Date: Wed, 26 Oct 2022 05:57:23 +0700 Subject: [PATCH 4/6] nampy & pandas --- np&pd.ipynb | 2163 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2163 insertions(+) create mode 100644 np&pd.ipynb diff --git a/np&pd.ipynb b/np&pd.ipynb new file mode 100644 index 0000000..1027761 --- /dev/null +++ b/np&pd.ipynb @@ -0,0 +1,2163 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "d6ebb10f", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7e4ffdff", + "metadata": {}, + "outputs": [], + "source": [ + "numbers = np.array([2, 3, 5, 7, 11])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ba601940", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(numbers)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2a70a3f1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 2, 3, 5, 7, 11])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5a402af5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "7b3e3449", + "metadata": {}, + "outputs": [], + "source": [ + "y = np.int_([1,2,4])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "f956ece3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 4])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9f35c784", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y.size" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f9c72b1b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers.size" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "d4f0b1b7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers.itemsize" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "047d0825", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "5\n", + "7\n", + "11\n" + ] + } + ], + "source": [ + "for i in numbers.flat:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "3410ff91", + "metadata": {}, + "outputs": [], + "source": [ + "integers = np.array([[1, 2, 3], [4, 5, 6]])\n", + "floats = np.array([0.0, 0.1, 0.2, 0.3, 0.4])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "eb677832", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "integers.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "66f1961b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "integers.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "3e8908fa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "6e2efc4c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3,)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "11d195f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "[4 5 6]\n" + ] + } + ], + "source": [ + "for i in integers:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "f20098ba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n" + ] + } + ], + "source": [ + "for i in integers.flat:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "33d95819", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[13, 13, 13, 13, 13],\n", + " [13, 13, 13, 13, 13],\n", + " [13, 13, 13, 13, 13]])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.full((3, 5), 13)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "d5fd9c50", + "metadata": {}, + "outputs": [], + "source": [ + "li = np.ones((20, 4), dtype=int)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "07ac8a61", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1]])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "li" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "c00fa46c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "li.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "c06bebb9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(20, 4)" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "li.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "2be511f5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([5, 6, 7, 8, 9])" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(5, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "dca8ff67", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([10, 8, 6, 4, 2])" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(10, 1, -2)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "c9fab483", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0. , 0.25, 0.5 , 0.75, 1. ])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.linspace(0.0, 1.0, num=5)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "317bfe24", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1, 2, 3, 4, 5],\n", + " [ 6, 7, 8, 9, 10],\n", + " [11, 12, 13, 14, 15],\n", + " [16, 17, 18, 19, 20]])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(1, 21).reshape(4, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "269e310a", + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "952bf4e6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9.66 s ± 1.07 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" + ] + } + ], + "source": [ + "%timeit rolls_list = [random.randrange(1, 7) for i in range(0, 6_000_000)]" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "a89c635e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "126 ms ± 32.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" + ] + } + ], + "source": [ + "%timeit rolls_array = np.random.randint(1, 7, 6_000_000)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "40dd0bd0", + "metadata": {}, + "outputs": [], + "source": [ + "numbers2 = np.linspace(1.1, 5.5, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "148ddf6e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1.1, 2.2, 3.3, 4.4, 5.5])" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers2" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "2049b83d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([False, False, False, False, False])" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers >= 13" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "190ff68d", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "0f253f1a", + "metadata": {}, + "outputs": [], + "source": [ + "grades = pd.Series([87, 100, 94])" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "88f80b11", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 87\n", + "1 100\n", + "2 94\n", + "dtype: int64" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "8bdf704f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 98.6\n", + "1 98.6\n", + "2 98.6\n", + "dtype: float64" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(98.6, range(3))" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "b7f2e636", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.count()" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "e24a1bea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "87" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.min()" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "16c1d0b9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6.506407098647712" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.std()" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "723f8cec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "count 3.000000\n", + "mean 93.666667\n", + "std 6.506407\n", + "min 87.000000\n", + "25% 90.500000\n", + "50% 94.000000\n", + "75% 97.000000\n", + "max 100.000000\n", + "dtype: float64" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "09c493b7", + "metadata": {}, + "outputs": [], + "source": [ + "grades = pd.Series([87, 100, 94], index=['Wally', 'Eva', 'Sam'])" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "a6397b3c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Wally 87\n", + "Eva 101\n", + "Sam 94\n", + "dtype: int64" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "91eae6d5", + "metadata": {}, + "outputs": [], + "source": [ + "grades = pd.Series({'Wally': 87, 'Eva': 101, 'Sam': 94})" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "78e32498", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "101" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades['Eva']" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "e4cf6e19", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "87" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.Wally" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "3d04d8e2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 87, 101, 94], dtype=int64)" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.values" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "5a3e575f", + "metadata": {}, + "outputs": [], + "source": [ + "hardware = pd.Series(['Hammer', 'Saw', 'Wrench'])" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "d9f96adf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 True\n", + "1 True\n", + "2 False\n", + "dtype: bool" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hardware.str.contains('a')" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "f7780e29", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 HAMMER\n", + "1 SAW\n", + "2 WRENCH\n", + "dtype: object" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hardware.str.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "7c2d3366", + "metadata": {}, + "outputs": [], + "source": [ + "grades_dict = {'Wally': [87, 96, 70], 'Eva': [100, 87, 90],'Sam': [94, 77, 90], 'Katie': [100, 81, 82], 'Bob': [83, 65, 85]}" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "de590745", + "metadata": {}, + "outputs": [], + "source": [ + "grades = pd.DataFrame(grades_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "1339559b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSamKatieBob
0871009410083
19687778165
27090908285
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam Katie Bob\n", + "0 87 100 94 100 83\n", + "1 96 87 77 81 65\n", + "2 70 90 90 82 85" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "36631a82", + "metadata": {}, + "outputs": [], + "source": [ + "grades = pd.DataFrame(grades_dict, index=['Test1', 'Test2', 'Test3'])" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "53cb348b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSamKatieBob
Test1871009410083
Test29687778165
Test37090908285
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam Katie Bob\n", + "Test1 87 100 94 100 83\n", + "Test2 96 87 77 81 65\n", + "Test3 70 90 90 82 85" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "142741a6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Test1 100\n", + "Test2 87\n", + "Test3 90\n", + "Name: Eva, dtype: int64" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades['Eva']" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "5a4f11cb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 94\n", + "1 77\n", + "2 90\n", + "Name: Sam, dtype: int64" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.Sam" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "e10b2cf1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Wally 87\n", + "Eva 100\n", + "Sam 94\n", + "Katie 100\n", + "Bob 83\n", + "Name: Test1, dtype: int64" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.loc['Test1']" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "a49495b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Wally 96\n", + "Eva 87\n", + "Sam 77\n", + "Katie 81\n", + "Bob 65\n", + "Name: Test2, dtype: int64" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.iloc[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "26e3902b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSamKatieBob
Test1871009410083
Test37090908285
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam Katie Bob\n", + "Test1 87 100 94 100 83\n", + "Test3 70 90 90 82 85" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.loc[['Test1', 'Test3']]" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "cd8e9604", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSamKatieBob
Test1871009410083
Test37090908285
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam Katie Bob\n", + "Test1 87 100 94 100 83\n", + "Test3 70 90 90 82 85" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.iloc[[0, 2]]" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "c99628ff", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
EvaKatie
Test1100100
Test28781
\n", + "
" + ], + "text/plain": [ + " Eva Katie\n", + "Test1 100 100\n", + "Test2 87 81" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.loc['Test1':'Test2', ['Eva', 'Katie']]" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "65624a1e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSam
Test18710094
Test3709090
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam\n", + "Test1 87 100 94\n", + "Test3 70 90 90" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.iloc[[0, 2], 0:3]" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "id": "151dc860", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Wally
Test187
Test370
\n", + "
" + ], + "text/plain": [ + " Wally\n", + "Test1 87\n", + "Test3 70" + ] + }, + "execution_count": 106, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.iloc[[0, 2], 0:1]" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "f1666c3c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSamKatieBob
Test187.010094.010083.0
Test296.087NaN81NaN
Test3NaN9090.08285.0
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam Katie Bob\n", + "Test1 87.0 100 94.0 100 83.0\n", + "Test2 96.0 87 NaN 81 NaN\n", + "Test3 NaN 90 90.0 82 85.0" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades[grades >= 80]" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "db29e56a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "87" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.iat[2, 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "283e9cbf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "70" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.iat[2, 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "id": "82bc9f81", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "90" + ] + }, + "execution_count": 112, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.iat[2, 1]" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "id": "12c40b35", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSamKatieBob
count3.0000003.0000003.0000003.0000003.000000
mean84.33333392.33333387.00000087.66666777.666667
std13.2035356.8068598.88819410.69267711.015141
min70.00000087.00000077.00000081.00000065.000000
25%78.50000088.50000083.50000081.50000074.000000
50%87.00000090.00000090.00000082.00000083.000000
75%91.50000095.00000092.00000091.00000084.000000
max96.000000100.00000094.000000100.00000085.000000
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam Katie Bob\n", + "count 3.000000 3.000000 3.000000 3.000000 3.000000\n", + "mean 84.333333 92.333333 87.000000 87.666667 77.666667\n", + "std 13.203535 6.806859 8.888194 10.692677 11.015141\n", + "min 70.000000 87.000000 77.000000 81.000000 65.000000\n", + "25% 78.500000 88.500000 83.500000 81.500000 74.000000\n", + "50% 87.000000 90.000000 90.000000 82.000000 83.000000\n", + "75% 91.500000 95.000000 92.000000 91.000000 84.000000\n", + "max 96.000000 100.000000 94.000000 100.000000 85.000000" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "947f5291", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Wally 84.333333\n", + "Eva 92.333333\n", + "Sam 87.000000\n", + "Katie 87.666667\n", + "Bob 77.666667\n", + "dtype: float64" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.mean()" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "7ff84bdf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
BobEvaKatieSamWally
Test1831001009487
Test26587817796
Test38590829070
\n", + "
" + ], + "text/plain": [ + " Bob Eva Katie Sam Wally\n", + "Test1 83 100 100 94 87\n", + "Test2 65 87 81 77 96\n", + "Test3 85 90 82 90 70" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.sort_index(axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "bf7d5cdf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WallyEvaSamKatieBob
Test1871009410083
Test29687778165
Test37090908285
\n", + "
" + ], + "text/plain": [ + " Wally Eva Sam Katie Bob\n", + "Test1 87 100 94 100 83\n", + "Test2 96 87 77 81 65\n", + "Test3 70 90 90 82 85" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "id": "8ecdde56", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
EvaKatieSamWallyBob
Test1100100948783
Test28781779665
Test39082907085
\n", + "
" + ], + "text/plain": [ + " Eva Katie Sam Wally Bob\n", + "Test1 100 100 94 87 83\n", + "Test2 87 81 77 96 65\n", + "Test3 90 82 90 70 85" + ] + }, + "execution_count": 117, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.sort_values(by='Test1', axis=1, ascending=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "8c35d5d5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Eva 100\n", + "Katie 100\n", + "Sam 94\n", + "Wally 87\n", + "Bob 83\n", + "Name: Test1, dtype: int64" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades.loc['Test1'].sort_values(ascending=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e01e3b77", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 5f770dad91974decdc9c28ba21ab6e4d50b14e2b Mon Sep 17 00:00:00 2001 From: Vladimir <87763796+moiseevv@users.noreply.github.com> Date: Wed, 26 Oct 2022 07:04:29 +0700 Subject: [PATCH 5/6] str & re & pd --- str & re & pd.ipynb | 963 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 963 insertions(+) create mode 100644 str & re & pd.ipynb diff --git a/str & re & pd.ipynb b/str & re & pd.ipynb new file mode 100644 index 0000000..86f0d62 --- /dev/null +++ b/str & re & pd.ipynb @@ -0,0 +1,963 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "97ca073e", + "metadata": {}, + "outputs": [], + "source": [ + "sentence = '\\t \\n This is a test string. \\t\\t \\n'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9af5f267", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\t \\n This is a test string. \\t\\t \\n'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f62e14c3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'This is a test string.'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence.strip()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9d2f893d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\t \\n This is a test string.'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence.rstrip()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "584635e6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\t \\n this is a test string. \\t\\t \\n'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence.capitalize()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a218620a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\t \\n This Is A Test String. \\t\\t \\n'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence.title()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e6d0d75b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A: 65; a: 97;\" \": 32\n" + ] + } + ], + "source": [ + "print(f'A: {ord(\"A\")}; a: {ord(\"a\")};\" \": {ord(\" \")}')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "fe660ffc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence.count('in')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "3379b7f9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence.count('i', 12, 25)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "104883ec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['\\t', '\\n', 'This is a test string. \\t\\t \\n']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence.split(' ', 2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "bd1bb501", + "metadata": {}, + "outputs": [], + "source": [ + "ll = sentence.split(' ', 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "edaf7c0b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\t,\\n,This is a test string. \\t\\t \\n'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "','.join(ll)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "93cfe6e0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0,1,2,3,4,5,6,7,8,9'" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "','.join([str(i) for i in range(10)])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "b7f7ea62", + "metadata": {}, + "outputs": [], + "source": [ + "mm=','.join([str(i) for i in range(10)])" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "b59e51f6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('0', ',', '1,2,3,4,5,6,7,8,9')" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mm.partition(',')" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "4838d1f9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('0,1,2,3,4,5,6,7,8', ',', '9')" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mm.rpartition(',')" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "09e1705b", + "metadata": {}, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "abb71c50", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Valid'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Valid' if re.fullmatch('[A-Z][a-z]*', 'Wally') else 'Invalid'" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "661d4bae", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Invalid'" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Valid' if re.fullmatch('[A-Z][a-z]*', 'eva') else 'Invalid'" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "285aa3e2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Match'" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch('[^a-z]', 'A') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "119ea7a2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'No match'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch('^[a-z]', 'A') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "f1ff0963", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Match'" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch('^[a-z]', 'a') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "93671f97", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Match'" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch('[A-Z][a-z]', 'Aa') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "77b646a0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Valid'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Valid' if re.fullmatch('[A-Z][a-z]+', 'Wally') else 'Invalid'" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "90de3a6f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Invalid'" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Valid' if re.fullmatch('[A-Z][a-z]+', 'E') else 'Invalid'" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "1168fb84", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Match'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch('labell?ed', 'labelled') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "d5e7d029", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'No match'" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch('labell?ed', 'labellled') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "7e979ea8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Match'" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch(r'\\d{3}', '123') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "76e9227b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Match'" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch(r'\\d{3,}', '123') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "7e516b1b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'No match'" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch(r'\\D{3,}', '123') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "4c5886d2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Match'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Match' if re.fullmatch(r'\\d{3,6}', '123456') else 'No match'" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "2a31adc7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "re.search('Python', 'Python is fun Python')" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "97b5af3e", + "metadata": {}, + "outputs": [], + "source": [ + "result=re.search('Python', 'Python is fun Python')" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "9ae42be0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Python'" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result.group() if result else 'not found'" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "b1818555", + "metadata": {}, + "outputs": [], + "source": [ + "result3 = re.search('Sam', 'SAM WHITE', flags=re.IGNORECASE)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "97406b35", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'SAM'" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result3.group() if result3 else 'not found'" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "7d48f488", + "metadata": {}, + "outputs": [], + "source": [ + "result = re.search('^Python', 'Python is fun')" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "ebf6db40", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Python'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result.group() if result else 'not found'" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "b015c624", + "metadata": {}, + "outputs": [], + "source": [ + "result = re.search('Python$', 'Python is fun')" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "38f06694", + "metadata": {}, + "outputs": [], + "source": [ + "result = re.search('fun$', 'Python is fun')" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "8bc64f11", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'fun'" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result.group() if result else 'not found'" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "624459e6", + "metadata": {}, + "outputs": [], + "source": [ + "contact = 'Wally White, Home: 555-555-1234, Work: 555-555-4321'" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "439a2c33", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['555-555-1234', '555-555-4321']" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "re.findall(r'\\d{3}-\\d{3}-\\d{4}', contact)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "ab5f4876", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "555-555-1234\n", + "555-555-4321\n" + ] + } + ], + "source": [ + "for phone in re.finditer(r'\\d{3}-\\d{3}-\\d{4}', contact):\n", + " print(phone.group())" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "0ea3447c", + "metadata": {}, + "outputs": [], + "source": [ + "text = 'Charlie Cyan, e-mail: demo1@deitel.com'" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "c65d9b80", + "metadata": {}, + "outputs": [], + "source": [ + "pattern = r'([A-Z][a-z]+ [A-Z][a-z]+), e-mail: (\\w+@\\w+\\.\\w{3})'" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "4fcebf11", + "metadata": {}, + "outputs": [], + "source": [ + "result = re.search(pattern, text)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "3b77c879", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "zips = pd.Series({'Boston': '02215', 'Miami': '3310'})" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "1b89b7b6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Boston True\n", + "Miami False\n", + "dtype: bool" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "zips.str.match(r'\\d{5}')" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "a1de4f3a", + "metadata": {}, + "outputs": [], + "source": [ + "cities = pd.Series(['Boston, MA 02215', 'Miami, FL 33101'])" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "d8292f90", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 True\n", + "1 True\n", + "dtype: bool" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cities.str.contains(r' [A-Z]{2} ')" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "58eb6934", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 False\n", + "1 False\n", + "dtype: bool" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cities.str.match(r' [A-Z]{2} ')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d340ef45", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From e7a5c6510f060867f4b5cf02c0b1fa249c85e308 Mon Sep 17 00:00:00 2001 From: Vladimir <87763796+moiseevv@users.noreply.github.com> Date: Wed, 26 Oct 2022 22:07:47 +0700 Subject: [PATCH 6/6] json --- json.ipynb | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 json.ipynb diff --git a/json.ipynb b/json.ipynb new file mode 100644 index 0000000..b882808 --- /dev/null +++ b/json.ipynb @@ -0,0 +1,139 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "5060c301", + "metadata": {}, + "outputs": [], + "source": [ + "import json" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6605ba06", + "metadata": {}, + "outputs": [], + "source": [ + "accounts_dict = [{'account': 100, 'name': 'Jones', 'balance': 24.98},{'account': 200, 'name': 'Doe', 'balance': 345.67}]" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "08444f1f", + "metadata": {}, + "outputs": [], + "source": [ + "with open('mal.json','w') as file:\n", + " json.dump(accounts_dict, file)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "165b9114", + "metadata": {}, + "outputs": [], + "source": [ + "with open('mal.json','r') as file2:\n", + " kl = json.load(file2)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3a8b8102", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'account': 100, 'name': 'Jones', 'balance': 24.98},\n", + " {'account': 200, 'name': 'Doe', 'balance': 345.67}]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kl" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "09c00810", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(kl)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "05b277e5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(kl[0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "34ecf5d6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}