Skip to content

Commit

Permalink
hello world; list comprehension; higher order functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ariefrahmansyah committed Aug 12, 2024
1 parent ed2ddd4 commit cc3f912
Show file tree
Hide file tree
Showing 9 changed files with 862 additions and 23 deletions.
4 changes: 3 additions & 1 deletion docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ parts:
chapters:
- file: python/python_fundamentals_1/index
sections:
- file: python/python_fundamentals_1/hello_world
- file: python/python_fundamentals_1/variables
- file: python/python_fundamentals_1/data_types
sections:
Expand All @@ -36,7 +37,8 @@ parts:
- file: python/python_fundamentals_1/modules_packages
- file: python/python_fundamentals_2/index
sections:
- file: python/python_fundamentals_2/list_comprehensions
- file: python/python_fundamentals_2/list_comprehension
- file: python/python_fundamentals_2/higher_order_functions
- file: python/oop/index
sections:
- file: python/oop/classes
Expand Down
Binary file added docs/images/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
201 changes: 180 additions & 21 deletions docs/python/python_fundamentals_1/functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"id": "4f7ff307-e254-433d-ac3d-0533c2515bd3",
"metadata": {},
"outputs": [
Expand All @@ -347,15 +347,15 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"id": "9fb7913b-edbd-47b4-be82-025026f248f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum of two numbers: 10\n"
"Sum of two numbers: 10\n"
]
}
],
Expand All @@ -370,15 +370,15 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 12,
"id": "ff631115-e80a-4858-b34c-3cde3b6fd639",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Age: 20\n"
"Age: 20\n"
]
}
],
Expand All @@ -393,7 +393,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 13,
"id": "9b0d6091-1de9-426d-89ab-06b65d7648d3",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -436,7 +436,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"id": "140dc909-43f7-4fec-bf28-ee29ed61550e",
"metadata": {},
"outputs": [
Expand All @@ -459,7 +459,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 15,
"id": "5f38fb72-2a22-4395-b296-d7af538c5bba",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -494,7 +494,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 16,
"id": "ef091fc0-66ef-4992-ba9c-d35564d5ba43",
"metadata": {},
"outputs": [
Expand All @@ -504,7 +504,7 @@
"'Aurora Luna'"
]
},
"execution_count": 21,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -529,7 +529,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 17,
"id": "2cf06818-b4b9-4e9c-aeb8-ed84d47224cb",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -560,7 +560,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 18,
"id": "e804c860-c7f6-471a-b5ef-e8efa42e6297",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -592,7 +592,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 19,
"id": "7483d320-0db0-4d86-bb1b-dd87c8e8f263",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -639,7 +639,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 20,
"id": "02d30759-c089-4b83-a6c0-d2c14660644c",
"metadata": {},
"outputs": [
Expand All @@ -664,7 +664,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 21,
"id": "61f7e7e1-472f-4090-9366-3fb8ee194e42",
"metadata": {},
"outputs": [
Expand All @@ -690,7 +690,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 22,
"id": "7e13ffbc-fa4b-498f-b27b-84b6256864f8",
"metadata": {},
"outputs": [
Expand All @@ -713,7 +713,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 23,
"id": "aeb734ac-4a50-4939-8281-169ce669a31b",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -753,7 +753,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 24,
"id": "a792361e-256e-4983-b79b-aea2cdc573fa",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -802,7 +802,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 25,
"id": "3ca2a1c0-1352-4906-80d7-0879ca705b29",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -846,7 +846,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 26,
"id": "00706906-f108-47fb-b497-7de24731b769",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -923,7 +923,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 27,
"id": "900748c0-c413-465d-9a3b-ff9a7e62a90b",
"metadata": {},
"outputs": [
Expand All @@ -949,6 +949,165 @@
"\n",
"print(do_something(square_number, 3)) # 27"
]
},
{
"cell_type": "markdown",
"id": "cab7e9e2-3380-47ca-aa7b-416590b2a857",
"metadata": {},
"source": [
"## Lambda Function\n",
"\n",
"Lambda function is a small anonymous function without a name. It can take any number of arguments, but can only have one expression. Lambda function is similar to anonymous functions in JavaScript. We need it when we want to write an anonymous function inside another function.\n",
"\n",
"To create a lambda function we use lambda keyword followed by a parameter(s), followed by an expression. See the syntax and the example below. Lambda function does not use return but it explicitly returns the expression.\n",
"\n",
"```\n",
"x = lambda param1, param2, param3: param1 + param2 + param2\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "3f5425b4-4619-4ba3-8758-1a208d4d7435",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"5\n"
]
}
],
"source": [
"# Named function\n",
"def add_two_nums(a, b):\n",
" return a + b\n",
"\n",
"\n",
"print(add_two_nums(2, 3))\n",
"\n",
"# Lets change the above function to a lambda function\n",
"add_two_nums = lambda a, b: a + b\n",
"print(add_two_nums(2, 3)) # 5"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "0c42d51d-e83d-4968-8654-801ee40d1e6c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Self invoking lambda function\n",
"(lambda a, b: a + b)(2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "85950214-44ba-43c8-8cfa-c5599e86ebd9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n"
]
}
],
"source": [
"square = lambda x: x**2\n",
"print(square(3))"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "10732191-0fbe-44fb-b8c6-51993e40d064",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27\n"
]
}
],
"source": [
"cube = lambda x: x**3\n",
"print(cube(3))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "2f1761e2-daa6-4c91-904d-3322b736a24f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22\n"
]
}
],
"source": [
"multiple_variable = lambda a, b, c: a**2 - 3 * b + 4 * c\n",
"print(multiple_variable(5, 5, 3))"
]
},
{
"cell_type": "markdown",
"id": "446c7b17-577b-4e5f-ab96-d012bb9587d2",
"metadata": {},
"source": [
"### Lambda Function Inside Another Function"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "2b23841f-1d3e-41b4-9884-56d091ec7d7f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"32\n"
]
}
],
"source": [
"def power(x):\n",
" return lambda n: x**n\n",
"\n",
"\n",
"# function power now need 2 arguments to run, in separate rounded brackets\n",
"cube = power(2)(3)\n",
"print(cube)\n",
"\n",
"two_power_of_five = power(2)(5)\n",
"print(two_power_of_five)"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit cc3f912

Please sign in to comment.