{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "49987e10",
   "metadata": {},
   "source": [
    "# 13-06 · Лямбда-функции\n",
    "\n",
    "Практика к разделу [«Лямбда-функции»](../../site/chapters/glava-13/13-06-lambda.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae35d6b9",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить lambda и сравнить с def."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79f575b3",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c6a72a49",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:39.332867Z",
     "iopub.status.busy": "2026-08-14T20:58:39.332689Z",
     "iopub.status.idle": "2026-08-14T20:58:39.362534Z",
     "shell.execute_reply": "2026-08-14T20:58:39.362044Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "kvadrat = lambda x: x ** 2\n",
    "print(kvadrat(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8767c264",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — lambda в sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0c1f9c51",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:39.363948Z",
     "iopub.status.busy": "2026-08-14T20:58:39.363772Z",
     "iopub.status.idle": "2026-08-14T20:58:39.368131Z",
     "shell.execute_reply": "2026-08-14T20:58:39.367429Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['я', 'python', 'программирование']\n"
     ]
    }
   ],
   "source": [
    "slova = [\"python\", \"я\", \"программирование\"]\n",
    "slova.sort(key=lambda word: len(word))\n",
    "print(slova)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c69fb8ef",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Напишите lambda, которая проверяет, чётно ли число, и используйте её в filter()."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf375f94",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:39.369524Z",
     "iopub.status.busy": "2026-08-14T20:58:39.369401Z",
     "iopub.status.idle": "2026-08-14T20:58:39.371795Z",
     "shell.execute_reply": "2026-08-14T20:58:39.371348Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "is_even = lambda n: n % 2 == 0\n",
    "chisla = [1, 2, 3, 4, 5, 6, 7, 8]\n",
    "chetnye = list(filter(is_even, chisla))\n",
    "print(chetnye)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "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.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
