Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python functions #936

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
328 changes: 328 additions & 0 deletions Project/builtin_functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# builtin methods <a href='https://docs.python.org/3/library/functions.html'>[doc]</a>\n",
"- statistics\n",
" - len()\n",
" - max()\n",
" - min()\n",
" - sum()\n",
"- utility\n",
" - sorted()\n",
" - reversed()\n",
" - eval()\n",
" - exec()\n",
"- operate on iterable\n",
" - all()\n",
" - any()\n",
" - enumerate()\n",
" - zip()\n",
" - filter()\n",
" - map()\n",
"- type conversion\n",
" - int()\n",
" - float()\n",
" - str()\n",
" - list()\n",
" - tuple()\n",
" - dict()\n",
" - set()\n",
" - bool()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = ['A','N','B','A','C','Z','D','E']\n",
"y = \"this is an example string\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sorted(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(sorted(y))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sorted(x, reverse=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sorted(y.split(), reverse=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z = 'we are working on an Encyclopedia in which there will be stuff'\n",
"sorted(z.split(), key=len)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reversed(z) # lazy object"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\".join(list(reversed(z))) # not the best way to reverse a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z[::-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = [1,1,1,11,1]\n",
"print(all(x))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z =['this','is', '', 'example', 'string']\n",
"print(all(z))\n",
"print(any(z))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = [0,0,1,0,0]\n",
"print(any(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- math functions\n",
"\n",
"`f(x) = x**2 + x + 10`\n",
"\n",
"`f(5)`\n",
"\n",
"`g(x, y) = x**2 + y**2 + x*y + 10`\n",
"\n",
"`g(2,3)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = lambda i : i**2\n",
"print(f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f(25)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = lambda x: x**2 + x + 10\n",
"print(f(10))\n",
"print(f(15))\n",
"print(f(25))\n",
"print(f(11))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = [12,45,5,17,89,124,32,15,16]\n",
"for val in data:\n",
" print(f(val))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# map(function, iterable)\n",
"list(map(f, data)) # lazy object"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"nums =['1', '100', '300', '599']\n",
"clean_list = map(lambda i: int(i), nums)\n",
"print(list(clean_list))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"clean_list = map(int, nums)\n",
"print(list(clean_list))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"nums = [1,2,0,1,0,2,2,2,2,0,3,3,0,3,1,0,0,2,2,3,3]\n",
"list(filter(lambda i: i != 0, nums))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"isinstance(x, list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"isinstance(x, (int, float))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"isinstance(x, (list, tuple))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 14, 24, 36, 50]"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = [1,2,3,4,5]\n",
"y = [6,7,8,9,10]\n",
"list(map(lambda i, j: i*j, x, y))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.11.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading