{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# asynchronous code in python"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A\n",
"!\n",
"B\n",
"@\n",
"Hey\n"
]
}
],
"source": [
"import asyncio\n",
"\n",
"async def main():\n",
" task= asyncio.create_task(other())\n",
" print(\"A\")\n",
" await asyncio.sleep(1)\n",
" print(\"B\")\n",
" return_val = await task\n",
" print(return_val)\n",
"\n",
"async def other():\n",
" print(\"!\")\n",
" await asyncio.sleep(2)\n",
" print(\"@\")\n",
" return \"Hey\"\n",
"# asyncio.run(main()) --> this is for python >= 3.7\n",
"await main()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Counter in collection in python"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counter({'x': 4, 'y': 2, 'z': 2})\n",
"Counter({'a': 3, 'A': 3, 'r': 3, 'e': 2, 'c': 1, 'd': 1})\n",
"Counter({'x': 4, 'y': 2, 'z': 2})\n",
"Counter : Counter({'x': 4, 'y': 2, 'z': 2})\n",
"\n",
"\n",
"u : 0\n",
"\n",
"\n",
"G : 1\n",
"n : 2\n",
"a : 1\n",
"n : 2\n",
"i : 1\n",
"Counter({'e': 2, 'o': 2, ' ': 2, 'n': 2, 'W': 1, 'l': 1, 'c': 1, 'm': 1, 't': 1, 'a': 1, 'i': 1})\n"
]
}
],
"source": [
"from collections import Counter\n",
"\n",
"# Counter with list -------> \n",
"list1 = ['x','y','z','x','x','x','y', 'z']\n",
"print(Counter(list1))\n",
"\n",
"# Counter with String ------->\n",
"s = \"aaaAAAcdeerrr\"\n",
"print(Counter(s))\n",
"\n",
"# Counter with tuple ------->\n",
"tuple1 = ('x','y','z','x','x','x','y','z')\n",
"print(Counter(tuple1))\n",
"\n",
"# initialize empty counter and assign value to it ------>\n",
"_count = Counter()\n",
"\n",
"_count.update(list1)\n",
"print(f\"Counter : {_count}\")\n",
"\n",
"\n",
"\n",
"# Accessing Counter -------->\n",
"print(\"\\n\")\n",
"_count = Counter()\n",
"_count.update('Welcome to Gnani')\n",
"print('%s : %d' % ('u', _count['u']))\n",
"print(\"\\n\")\n",
"for char in 'Gnani':\n",
" print('%s : %d' % (char, _count[char]))\n",
"\n",
"\n",
"\n",
"# Deleting an Element from Counter ----->\n",
"\n",
"del _count[\"G\"]\n",
"print(_count)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Arithmatic operation on counter"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counter({'y': 7, 'z': 2})\n",
"Counter({'x': 16})\n",
"Counter({'y': 2})\n",
"Counter({'y': 5, 'x': 4, 'z': 4})\n"
]
}
],
"source": [
"from collections import Counter\n",
"\n",
"counter1 = Counter({'x': 4, 'y': 2, 'z': -2})\n",
"counter2 = Counter({'x': -12, 'y': 5, 'z':4 })\n",
"\n",
"# Addition\n",
"counter3 = counter1 + counter2 # only the values that are positive will be returned.\n",
"print(counter3)\n",
"\n",
"# Substarction\n",
"counter4 = counter1 - counter2 # all -ve numbers are excluded.For example z will be z = -2-4=-6, since it is -ve value it is not shown in the output\n",
"print(counter4)\n",
"\n",
"#Intersection\n",
"counter5 = counter1 & counter2 # it will give all common positive minimum values from counter1 and counter2\n",
"print(counter5)\n",
"\n",
"#Union\n",
"counter6 = counter1 | counter2 # it will give positive max values from counter1 and counter2\n",
"print(counter6)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Methods\n",
"- elements() : This method will return you all the elements with count >0. Elements with 0 or -1 count will not be returned.\n",
"- most_common(value): This method will return you the most common elements from Counter list.\n",
"- subtract(): This method is used to deduct the elements from another Counter.\n",
"- update(): This method is used to update the elements from another Counter.\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\n",
"x\n",
"x\n",
"x\n",
"x\n",
"x\n",
"y\n",
"y\n",
"\n",
"\n",
"\n",
"Most_common with given n(n=2) : [('x', 5), ('y', 2)]\n",
"Most commen : [('x', 5), ('y', 2), ('x1', 0), ('z', -2)]\n",
"\n",
"\n",
"\n",
"Subtract : Counter({'y': 7, 'x': 3, 'x1': 0, 'z': -2})\n",
"\n",
"\n",
"\n",
"Update : Counter({'y': 17, 'x': 7, 'x1': 0, 'z': -2})\n",
"\n",
"\n",
"\n",
"We can update the value of any key:\n",
"Counter({'x': 50, 'y': 17, 'x1': 0, 'z': -2})\n"
]
}
],
"source": [
"counter1 = Counter({'x': 5, 'y': 2, 'z': -2, 'x1':0})\n",
"print(\"\\n\\n\")\n",
"\n",
"for a in counter1.elements():\n",
" print(a)\n",
"\n",
"print(\"\\n\\n\")\n",
"\n",
"most_2_common_element = counter1.most_common(2)\n",
"print(\"Most_common with given n(n=2) :\",most_2_common_element)\n",
"print(\"Most commen :\",counter1.most_common())\n",
"\n",
"print(\"\\n\\n\")\n",
"\n",
"counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})\n",
"counter2 = Counter({'x': 2, 'y':5})\n",
"counter1.subtract(counter2)\n",
"print(\"Subtract :\",counter1)\n",
"\n",
"print(\"\\n\\n\")\n",
"\n",
"counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})\n",
"counter2 = Counter({'x': 2, 'y':5})\n",
"counter1.update(counter2)\n",
"print(\"Update :\",counter1)\n",
"\n",
"print(\"\\n\\n\")\n",
"\n",
"print(\"We can update the value of any key:\")\n",
"counter1['x'] = 50\n",
"print(counter1)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Map fucntion\n",
"- Create simple itereator for function, instead of writing loop"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"