{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# główne zasady pisania 'czystego' kodu w języku Python\n",
    "# kod więcej razy będzie czytany niż pisany!\n",
    "import this"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python jako kalkulator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "?abs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "?print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1,2,sep='\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Liczbowe typy danych:\n",
    "- **int**: $1, 2, 3, 101$  \n",
    "- **float**: $0.12, 3.14, .5$  (zauważ, że w ostatniej liczbie nie ma cyfry $0$)  \n",
    "\n",
    "Python używa standardu IEEE 754 z podwójną precyzją:\n",
    "\\begin{equation}\n",
    "(-1)^s (1.b_{51}b_{50}\\ldots b_{0})_2 2^{E-1023}\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "W Python 3 zakres liczb całkowitych jest nieograniczony. Posługiwanie się bardzo dużymi liczbami może spowolnić pracę programu. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math as mt # o importowaniu pakietów w Pythonie jeszcze powiemy\n",
    "\n",
    "mt.factorial(10**5) # Uwaga! Dla dużego argumentu może wyskoczyć wyjątek OverflowError "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Istnieje liczba, która jest większa od każdej innej liczby!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(10**100 < float('inf'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Operatory arytmetyczne:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1 + 2)\n",
    "print(3.14 * 2)\n",
    "print(5/7)\n",
    "print(7 % 5) # reszta z dzielenia 7 przez 5\n",
    "print(2**10) # operator potęgowania"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(3//4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abs(2 - 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(0.3 - 0.2 - 0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1 == 2) # operator porównania\n",
    "print(3 >= 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(int(3.14)) # konwersja na typ int\n",
    "print(float(2)) # konwersja na float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bin(1024))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(int('0b10000000000',base=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(int(\"12\") + 9) # konwersja napisu \"12\" na typ int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(float(2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **liczby zespolone**  \n",
    "$1+2j$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print((1+2j).real)\n",
    "print((1+2j).imag)\n",
    "print((1+2j).conjugate())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Typ logiczny 'bool'\n",
    "- True, False ale też $1,0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# operatory logiczne\n",
    "print(True and False)\n",
    "print(True or False)\n",
    "print(not (True and True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = (2 < 3)\n",
    "print(x, type(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Epsilon maszynowy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys \n",
    "\n",
    "print(1 + sys.float_info.epsilon/2 == 1)\n",
    "print(sys.float_info.epsilon)\n",
    "print(0.3 - 0.2 - 0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Zmienne"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2 # funkcja type()\n",
    "b = 3\n",
    "\n",
    "print(a + b)\n",
    "print(a >= b) # operator porównania"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3.1455\n",
    "b = 3\n",
    "\n",
    "print(a == b) # porównujemy wartości tych zmiennych"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Zmienna jest niejako referencją do miejsca w pamięci gdzie przechowywana jest pewna wartość. Zmienne typu liczbowego są niemutowalne."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = 3 \n",
    "print(c is b) # 'is' operator sprawdza czy dwie zmienne wskasują na ten sam obiekt w pamięci"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 7\n",
    "print(\"Zmienna jeszcze istnieje: \",x)\n",
    "\n",
    "# usuwanie zmiennej\n",
    "del x\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Funkcja **round(x,n)** zaokrągla liczbę $x$  do najbliższej liczby z dokładnością do $n$ miejsc po przecinku.\n",
    "<u>Nie jest to klasyczne zaogrąglanie matematyczne.</u>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "round(a,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "round(a,-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Zaokrąglanie za pmocą funkcji round() jest intuincyjne dla prawie wszystkich przykładów. Rozważmy poniższe zaokrąglenia:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(round(0.125,2))\n",
    "print(round(0.135,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Dlaczego funkcja round() w pierwszym przykładzie zaokrągliła liczbę w górę, a w drugim w dół? Dokładne informacje można znaleźć w dokumentacji Pythona, w dziale funkcji wbudowanych."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
