{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e4768a42",
   "metadata": {},
   "source": [
    "# 09-17 · Short-circuit\n",
    "\n",
    "Praktyka do sekcji [„Short-circuit: Python jest czasem leniwy”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb88134e",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Zobacz, jak short-circuit chroni przed IndexError."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "197af1c5",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8e722bd2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T17:23:08.389682Z",
     "iopub.status.busy": "2026-08-16T17:23:08.389571Z",
     "iopub.status.idle": "2026-08-16T17:23:08.405655Z",
     "shell.execute_reply": "2026-08-16T17:23:08.405122Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "name = \"Anna\"\n",
    "print(name and name[0] == \"A\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea20c079",
   "metadata": {},
   "source": [
    "## Typowy błąd\n",
    "\n",
    "Bez short-circuit kolejność operandów może powodować IndexError."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f3a2067f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T17:23:08.406652Z",
     "iopub.status.busy": "2026-08-16T17:23:08.406486Z",
     "iopub.status.idle": "2026-08-16T17:23:08.434600Z",
     "shell.execute_reply": "2026-08-16T17:23:08.434139Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mIndexError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m name = \u001b[33m\"\"\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m print(name[\u001b[32m0\u001b[39m] == \u001b[33m\"A\"\u001b[39m \u001b[38;5;28;01mand\u001b[39;00m name)\n",
      "\u001b[31mIndexError\u001b[39m: string index out of range"
     ]
    }
   ],
   "source": [
    "name = \"\"\n",
    "print(name[0] == \"A\" and name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e14954f",
   "metadata": {},
   "source": [
    "## Poprawka"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3824d3c5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T17:23:08.435698Z",
     "iopub.status.busy": "2026-08-16T17:23:08.435592Z",
     "iopub.status.idle": "2026-08-16T17:23:08.438088Z",
     "shell.execute_reply": "2026-08-16T17:23:08.437533Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "name = \"\"\n",
    "safe_check = name and name[0] == \"A\"\n",
    "print(bool(safe_check))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d670fd4d",
   "metadata": {},
   "source": [
    "## Podstawowa praktyka zadań ★\n",
    "\n",
    "Ponownie spróbuj bezpiecznego sprawdzenia pustego łańcuch znaków – upewnij się, że NIE powoduje on błędu."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2a482882",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T17:23:08.439485Z",
     "iopub.status.busy": "2026-08-16T17:23:08.439309Z",
     "iopub.status.idle": "2026-08-16T17:23:08.442236Z",
     "shell.execute_reply": "2026-08-16T17:23:08.441660Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "name = \"\"\n",
    "safe_check = name and name[0] == \"A\"\n",
    "print(bool(safe_check))"
   ]
  }
 ],
 "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
}
