Skip to content
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
141 changes: 141 additions & 0 deletions MyWork/LambdaFunctionTutorial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4cd33944-a665-4b67-abf1-de228d9c99e9",
"metadata": {},
"source": [
"This code has been prouced as a teaching resource for the UKSA space software, data and AI course run by the Space South Central Universities.\n",
"\n",
"Contributors to this code includes: M. Chandar"
]
},
{
"cell_type": "markdown",
"id": "75e5aa33-5261-4e5d-b894-309031fea1b6",
"metadata": {},
"source": [
"## Learning Outcome"
]
},
{
"cell_type": "markdown",
"id": "e4d50917-7bff-4dac-8a44-19ab5a36c127",
"metadata": {},
"source": [
"\n",
"<div class=\"alert alert-block alert-info\"> <b> NOTE </b>\n",
"This notebook aims to explain what the lambda function is in Python through examples. \n",
"</div>\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "dbc81d61-fa78-49ae-a014-8b3fe5a507c8",
"metadata": {},
"outputs": [],
"source": [
"# A lambda function is known as an anonymous function as it is not bound to an identifier,\n",
"# A regular function (defined with def) can be reused many times. A lambda function are single-use functions that can be inlined. \n",
"# Since you only use them once, \n",
"# they don't need an identifier\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0e86f85e-40cc-4acd-8d4f-ef62ff661eaf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"# If we wanted to write a reusable function to double a number:\n",
"\n",
"def doubleNum(num):\n",
" return 2 * num\n",
"\n",
"# As a single use lambda expression\n",
"\n",
"# This is known as an antipattern as lambda functions are not supposed to be assigned to an identifier - it defeats the point of an anonymous function!\n",
"doubleNum = lambda num : 2 * num # This takes the identifier doubleNum from the namespace which is also bad practice with lambda functions.\n",
"\n",
"\n",
"print(doubleNum(5))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6b5cb119-7d2d-41ee-8c95-0ab6f29d74de",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"With a function: [['Daniel', 12], ['Beth', 15], ['Aaron', 56], ['Carrie', 80]]\n",
"With a lambda function [['Daniel', 12], ['Beth', 15], ['Aaron', 56], ['Carrie', 80]]\n"
]
}
],
"source": [
"# You can create custom sorts\n",
"# With a selection sort function\n",
"def sortByGrade(studentGradeList):\n",
" for i in range(len(studentGradeList)):\n",
" smallestGradeStudent = i\n",
" for j in range(i,len(studentGradeList)):\n",
" if studentGradeList[j][1] < studentGradeList[smallestGradeStudent][1]:\n",
" smallestGradeStudent = j\n",
" studentGradeList[smallestGradeStudent], studentGradeList[i] = studentGradeList[i], studentGradeList[smallestGradeStudent]\n",
" \n",
" return studentGradeList \n",
" \n",
"studentGradeList = [[\"Aaron\",56],[\"Beth\",15],[\"Carrie\",80], [\"Daniel\",12]]\n",
"\n",
"print(f\"With a function: {sortByGrade(studentGradeList)}\")\n",
"print(f\"With a lambda function {sorted(studentGradeList, key=lambda grade: grade[1])}\")\n"
]
},
{
"cell_type": "markdown",
"id": "af3b3085-6de4-4d6a-a28d-24c60e9158fe",
"metadata": {},
"source": [
"As we can see, using the lamda function to make this custom sort was much easier, quicker, and it will be more efficient. "
]
}
],
"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.9.21"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading