{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# **Podstawowe elementy języka Python**\n",
        "*W tym rozdziale przedstawimy podstawowe elementy składni języka Python*\n",
        "\n",
        "[Dokumentacja Python](https://www.python.org/)\n",
        "\n",
        "# Omawiane elementy\n",
        "\n",
        "---\n",
        "\n",
        "1.   Zmienne\n",
        "\n",
        "---\n",
        "\n",
        "2.   Listy\n",
        "\n",
        "---\n",
        "\n",
        "3.   Pętle\n",
        "\n",
        "---\n",
        "\n",
        "4.   Słowniki i krotki\n",
        "\n",
        "---\n",
        "5.   Instrukcje warunkowe\n",
        "\n",
        "---\n",
        "6.   Funkcje\n",
        "\n",
        "---\n",
        "7.   Obsługa plików\n",
        "\n",
        "---\n",
        "8.   Obsługa wyjątków\n",
        "\n",
        "---\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "1LUfojVsGPxl"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Obsługa Git"
      ],
      "metadata": {
        "id": "rHjrzftnKSvk"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!git init\n",
        "!git config --global user.name \"nazwa uzytkownika\"\n",
        "!git config --global user.email \"email uzytkownika\""
      ],
      "metadata": {
        "id": "BEP1Vl45KgSr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!git add ."
      ],
      "metadata": {
        "id": "W9YuiAifKSpH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!git commit -m \"inicjalizacja projektu\""
      ],
      "metadata": {
        "id": "aOiNFjGbKnO3",
        "collapsed": true
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!git branch -M main"
      ],
      "metadata": {
        "id": "temjvYa7LBNV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!git remote add origin https://<PAT>@github.com/<nazwa uzytkownika>/<nazwa repozytorium>.git"
      ],
      "metadata": {
        "id": "WN35wouz_I38"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!git push -u origin main\n"
      ],
      "metadata": {
        "id": "jS1FVYATLImc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "! git pull origin main"
      ],
      "metadata": {
        "id": "Yekb4UhNZEdq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Zmienne\n"
      ],
      "metadata": {
        "id": "b1ZVYLAoe0Qq"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "FNv4tixm80Kc"
      },
      "outputs": [],
      "source": [
        "liczba = 6\n",
        "zmiennoprzecinkowa = 8.9\n",
        "napis = \"Tekst\"\n",
        "logiczna = False\n",
        "znak = 'f'\n",
        "\n",
        "print(f\"liczba: {liczba}, liczba zmiennoprzecinkowa: {zmiennoprzecinkowa}, napis: {napis}, zmienna logiczna {logiczna}, zmienna znakowa {znak}\")\n",
        "print(f\"Zmienna liczba jest typu {type(liczba)}\")"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "liczba = \"szesc\"\n",
        "print(f\"Zmienna liczba jest typu {type(liczba)}\")"
      ],
      "metadata": {
        "id": "LF219nZ4evsF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Listy"
      ],
      "metadata": {
        "id": "4Vj6Sb1hkABM"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#tworzymy listy\n",
        "gry = ['fortnite', 'minecraft', 'roblox']\n",
        "lista_mieszana = [1, 'minecraft', 2, 'roblox']\n",
        "\n",
        "#wyświetlamy listy\n",
        "print(gry)\n",
        "print(lista_mieszana)\n",
        "\n",
        "#odwołanie do pierwszego elementu listy\n",
        "print(f\"pierwszyy element listy gry {gry[0]}\")\n",
        "\n",
        "#dodanie elementu do listy\n",
        "gry.append('cs')\n",
        "print(gry)"
      ],
      "metadata": {
        "id": "siqKGkTtkgfb"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Pętle"
      ],
      "metadata": {
        "id": "Dl6fq0uakitN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#pętla while\n",
        "licznik = 0\n",
        "\n",
        "print(\"Pętla while licząca do 5\")\n",
        "while licznik <= 5:\n",
        "  print(licznik)\n",
        "  licznik += 1\n",
        "\n",
        "print(\"Pętla for licząca do 5\")\n",
        "for licznik in range(6):\n",
        "  print(licznik)\n",
        "\n",
        "print(\"Pętla for licząca od 5 do 10\")\n",
        "for licznik in range(5,11):\n",
        "  print(licznik)\n",
        "\n",
        "print(\"Pętla for licząca do 10 tylko liczbami parzystymi\")\n",
        "for licznik in range(0,11,2):\n",
        "  print(licznik)\n",
        "\n",
        "print(\"Pętla przechodząca przez elementy listy gry\")\n",
        "for gra in gry:\n",
        "  print(gra)"
      ],
      "metadata": {
        "id": "09f6lN62kmh_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Słowniki i krotki"
      ],
      "metadata": {
        "id": "Q_E8sUAHknAz"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "slownik = {'kot' : 'cat',\n",
        "           'pies': 'dog',\n",
        "           'waz': 'snake'}\n",
        "\n",
        "print(slownik['kot'])\n",
        "\n",
        "print(\"Klucze słownika\")\n",
        "for key in slownik.keys():\n",
        "  print(key)\n",
        "\n",
        "print(\"Wartości słownika\")\n",
        "for value in slownik.values():\n",
        "  print(value)\n",
        "\n",
        "print(\"Krotka\")\n",
        "wspolrzedne = (2,7)\n",
        "print(f'Pierwsza wspolrzedna {wspolrzedne[0]}, druga wspolrzedna {wspolrzedne[1]}')"
      ],
      "metadata": {
        "id": "GRvyaOTelUo0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Instrukcje warunkowe\n"
      ],
      "metadata": {
        "id": "d72vS7MglU8b"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#Inteligenty grzejnik\n",
        "temperatura = float(input(\"Podaj temperaturę: \"))\n",
        "\n",
        "if temperatura < 20:\n",
        "  print(\"Podwyższam temperaturę\")\n",
        "elif temperatura >25:\n",
        "  print(\"Obniżam temperaturę\")\n",
        "else:\n",
        "  print(\"Temperatura w normie\")"
      ],
      "metadata": {
        "id": "oONZ6-4RlegD"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Funkcje"
      ],
      "metadata": {
        "id": "EnVLQOiUlezP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#definicja funkcji\n",
        "def dodawanie(a, b):\n",
        "  return a + b\n",
        "print(dodawanie (2,3))\n",
        "\n",
        "\n",
        "#definicja funkcji z argumentem domyslnym\n",
        "def dodawanie(a, b = 5):\n",
        "  return a + b\n",
        "print(dodawanie (2,3))\n",
        "print(dodawanie (2))\n",
        "\n",
        "#wywołanie funkcji z argumentem nazwanym\n",
        "def dodawanie(a, b):\n",
        "  return 2*a + b\n",
        "print(dodawanie (a=2, b = 3))\n",
        "print(dodawanie (b = 2, a = 3))"
      ],
      "metadata": {
        "id": "IeWwQocjlj68"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Obsługa plików"
      ],
      "metadata": {
        "id": "r4t_nQUxllG_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "#odczyt z pliku\n",
        "with open('dane.txt','r') as file:\n",
        "  for line in file:\n",
        "    print(line)\n",
        "\n",
        "#dodawanie do pliku\n",
        "with open('dane.txt','a') as file:\n",
        "  file.write('\\nLUA')\n",
        "\n",
        "#zapis do pliku (zapisanie do nowego pliku)\n",
        "with open('dane2.txt','w') as file:\n",
        "  file.write('Lubie placki')"
      ],
      "metadata": {
        "id": "6iA7hCc6lo-_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Obsługa wyjątków"
      ],
      "metadata": {
        "id": "AYemQA1rlpRl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "try:\n",
        "  dzielna = float(input(\"Podaj dzielną: \"))\n",
        "  dzielnik = float(input(\"Podaj dzielniki: \"))\n",
        "  suma = dzielna / dzielnik\n",
        "except Exception as e:\n",
        "  print(f\"Blad o tresci {e}\")\n",
        "else:\n",
        "  print(f\"{dzielna}/{dzielnik} to {suma}\")\n",
        "finally:\n",
        "  print(\"Koniec programu\")\n"
      ],
      "metadata": {
        "id": "vtTwivEMl0qI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!git add .\n",
        "!git commit -m \"Komentarz co zostało zmienione w projekcie\"\n",
        "! git push origin main\n"
      ],
      "metadata": {
        "id": "5GJ0niA-dYnh"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#Zadanie dodatkowe 1\n",
        "def czy_prawidlowy_trojkat(a,b,c):\n",
        "  if min(a,b,c) <= 0:\n",
        "    raise Exception (\"Długości boków trójkąta muszą być niezerowe\")\n",
        "\n",
        "  if 2*max(a,b,c) >= sum([a,b,c]):\n",
        "    print(\"Nie można zbudować trójkąta\")\n",
        "  else:\n",
        "    print(\"Można zbudować trójkąt\")\n",
        "\n",
        "def oneline_prawidlowy_trojkat(a,b,c):\n",
        "  print(\"Można zbudować trójkąt\" if min(a,b,c) > 0 and 2*max(a,b,c) < sum([a,b,c]) else \"Nie można zbudować trójkąta\")\n",
        "\n",
        "a = float(input(\"Podaj długość boku a: \"))\n",
        "b = float(input(\"Podaj długość boku b: \"))\n",
        "c = float(input(\"Podaj długość boku c: \"))\n",
        "oneline_prawidlowy_trojkat(a,b,c)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wyOoHbiq_FdP",
        "outputId": "ec0b4d4c-d18e-499b-eca2-314e6f4434f4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Podaj długość boku a: 10\n",
            "Podaj długość boku b: 5\n",
            "Podaj długość boku c: 5\n",
            "Nie można zbudować trójkąta\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#Zadanie dodatkowe 2\n",
        "lista = [x **2 for x in range(2,11,2)] + ([x **(1/2) for x in range(11,20,2)])\n",
        "print(lista)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "STbCPkE5CHX6",
        "outputId": "2a3cbc61-8ad5-43b1-d13d-3b267d0c25d3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[4, 16, 36, 64, 100, 3.3166247903554, 3.605551275463989, 3.872983346207417, 4.123105625617661, 4.358898943540674]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#Zadanie dodatkowe 3\n",
        "lista = [1,1,1,1,2,2,3,4,4,4,4,4,4,4,5,5,6]\n",
        "unikalne_wartości = set(lista)\n",
        "slownik = {}\n",
        "\n",
        "for unikalna_wartosc in unikalne_wartości:\n",
        "  liczba_wystapien = lista.count(unikalna_wartosc)\n",
        "  slownik.setdefault(unikalna_wartosc, liczba_wystapien)\n",
        "\n",
        "print(slownik)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-WjzeIGED-Px",
        "outputId": "600d6a8f-a092-4e49-899d-ed3216e0dd4f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{1: 4, 2: 2, 3: 1, 4: 7, 5: 2, 6: 1}\n"
          ]
        }
      ]
    }
  ]
}