{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Uporządkowane type danych\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Kodowanie znaków i napisy\n",
    "- kodowanie znaków: **ASCII** i **UNICODE**. System **UTF-8**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65\n",
      "243\n",
      "9728\n"
     ]
    }
   ],
   "source": [
    "# funkcja ord() zwraca wartość Unicode dla podanego znaku\n",
    "print(ord('A'))\n",
    "print(ord('ó'))\n",
    "print(ord('☀'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "a\n",
      "ਨ\n",
      "☀\n",
      "☀\n"
     ]
    }
   ],
   "source": [
    "print(chr(97)) # mała litera 'a' (U+0061)\n",
    "print(chr(0x0061)) # argument w systemie szesnastkowym\n",
    "print(chr(2600)) # Unicode Character 'GURMUKHI LETTER NA' (U+0A28)\n",
    "print(chr(9728)) # Unicode Character 'BLACK SUN WITH RAYS' (U+2600)\n",
    "print(chr(0x2600)) # w systemie szesnastkowym"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "早安\n",
      "早安\n"
     ]
    }
   ],
   "source": [
    "napis = b'\\xe6\\x97\\xa9\\xe5\\xae\\x89' #reprezentacja bajtowa napisu\n",
    "print(napis.decode('utf8'))\n",
    "print(str(napis, encoding='utf8'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " * Napisy (stringi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Witaj na 'spotkaniu'\n",
      "ZajeciaZajeciaZajecia\n"
     ]
    }
   ],
   "source": [
    "a = 'Witaj'\n",
    "b = \"na\"\n",
    "c = \"'spotkaniu'\"\n",
    "spacja = \" \"\n",
    "print(a + spacja + b + spacja + c)\n",
    "print(\"Zajecia\"*3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print('a' > 'c')\n",
    "print('ą' < 'c') # UWAGA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-e89eb3f59573>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "print(a + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ala ma 1 'kota'\n"
     ]
    }
   ],
   "source": [
    "print(a + \" \" + b + \" \" + str(1) + \" \" + c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ala ma\n",
      "'kota'\n"
     ]
    }
   ],
   "source": [
    "print(a + \" \" + b + \"\\n\" + c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W naszej grupie jest 15 studentów\n"
     ]
    }
   ],
   "source": [
    "print('W naszej grupie jest {0} studentów'.format(15))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jak masz na imię? Mateusz\n",
      "Użytkownik ma na imię Mateusz\n"
     ]
    }
   ],
   "source": [
    "a = input('Jak masz na imię? ')\n",
    "print('Użytkownik ma na imię {0}'.format(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "znak nowej \n",
      " lini\n"
     ]
    }
   ],
   "source": [
    "print(\"znak nowej \\n lini\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "znak tabulacji \t.\n"
     ]
    }
   ],
   "source": [
    "print(\"znak tabulacji \\t.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Napisy\n",
      "    wielo\n",
      "    linijkowe\n"
     ]
    }
   ],
   "source": [
    "print(\"\"\"Napisy\n",
    "    wielo\n",
    "    linijkowe\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello W\n",
      "o World\n",
      "dlroW olleH\n",
      "Długość napisu to 11\n"
     ]
    }
   ],
   "source": [
    "napis = \"Hello World\"\n",
    "\n",
    "print(napis[:7])\n",
    "print(napis[-7:])\n",
    "print(napis[::-1])\n",
    "print(\"Długość napisu to {0}.\".format(len(napis)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Kilka najważniejszych metod dla napisów (Więcej można znaleźć w dokumentacji)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n",
      "6\n",
      "HELLO WORLD\n",
      "hehho worhd\n",
      "['hello', 'world']\n",
      "hello<->world\n",
      "hello world\n",
      "hELLO wORLD\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world\".capitalize())\n",
    "print(\"hello world\".index(\"w\"))\n",
    "print(\"hello world\".upper())\n",
    "print(\"hello world\".replace(\"l\",\"h\"))\n",
    "print(\"hello world\".split())\n",
    "print(\"<->\".join(['hello', 'world']))\n",
    "print(\" \\n      hello world      \".strip())\n",
    "print(\"Hello World\".swapcase())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - **Listy** i **krotki**. Listy należa do typów mutowalnych, natomiast krotki są niemutowalne, ale ich wartości mogą się zmieniać."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "długi\n",
      "2\n",
      "('a', 'b', 'c') <class 'tuple'>\n",
      "[0, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "lista1 = [1, 2, 3]\n",
    "lista2 = [\"Bardzo\", \"długi\", \"tekst\"]\n",
    "\n",
    "krotka1 = (1,2)\n",
    "krotka2 = (2,)\n",
    "krotka3 = 'a','b','c'\n",
    "\n",
    "print(len(lista1))\n",
    "print(lista2[1])\n",
    "print(krotka1[1])\n",
    "print(krotka3, type(krotka3))\n",
    "\n",
    "lista1[0] = 0\n",
    "print(lista1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, [1, 2, 3]) 2094504618600\n",
      "(1, 2, [121, 2, 3]) 2094504618600\n",
      "(1, 2, [121, 2, 3], 144) 2094505684088\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-befbf45620cd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnowa_krotka\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnowa_krotka\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mkrotka\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m11\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "krotka = (1, 2, [1, 2, 3])\n",
    "print(krotka, id(krotka))\n",
    "\n",
    "krotka[2][0] = 121\n",
    "print(krotka, id(krotka))\n",
    "\n",
    "nowa_krotka = krotka + (144,)\n",
    "print(nowa_krotka, id(nowa_krotka))\n",
    "\n",
    "krotka[2] = 11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# listy są mutowalne\n",
    "id_przed_zmiana = id(lista1)\n",
    "lista1[1] = 6\n",
    "id_po_zmianie = id(lista1) \n",
    "\n",
    "print(id_przed_zmiana == id_po_zmianie)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n",
      "<class 'range'>\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[5, 6, 7, 8, 9]\n",
      "[1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "print(range(10))\n",
    "print(type(range(10)))\n",
    "print(list(range(10)))\n",
    "print(list(range(5,10)))\n",
    "print(list(range(1,10,2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "lista = list(range(10))\n",
    "\n",
    "lista[3:7] = []\n",
    "print(lista)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "# dołączanie do listy\n",
    "lista1 = [1, 2, 3, 4]\n",
    "print(lista1)\n",
    "lista1.append(5)\n",
    "lista1 += [6]\n",
    "print(lista1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "# rozszerzanie listy\n",
    "lista1 = [1, 2, 3]\n",
    "lista2 = [4, 5]\n",
    "print(lista1)\n",
    "lista1.extend(lista2)\n",
    "print(lista1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# kopiowanie list\n",
    "\n",
    "lista1 = [1, 2, 3, 11]\n",
    "id_lista1 = id(lista1)\n",
    "\n",
    "lista2 = lista1\n",
    "id_lista2 = id(lista2)\n",
    "\n",
    "lista3 = list(lista1) # to samo działnie można uzyskać poprzez: lista1[:] lub lista1.copy()\n",
    "id_lista3 = id(lista3)\n",
    "\n",
    "print(id_lista1 == id_lista2)\n",
    "print(id_lista1 == id_lista3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 7, 5, 17, 11, 11, 3, 6, 5, 8]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(list(np.random.randint(1,20, 10 )))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 9, 11, 17, 16, 4, 8, 6, 15, 19]\n",
      "[3, 5, 5, 6, 7, 8, 10, 11, 11, 17]\n",
      "[4, 6, 6, 8, 9, 11, 15, 16, 17, 19]\n"
     ]
    }
   ],
   "source": [
    "# sortowanie list\n",
    "\n",
    "lista1 = [6, 9, 11, 17, 16, 4, 8, 6, 15, 19]\n",
    "lista2 = [10, 7, 5, 17, 11, 11, 3, 6, 5, 8]\n",
    "\n",
    "lista3 = sorted(lista1)\n",
    "lista2.sort() # metoda sort() sortuje orginalną listę\n",
    "\n",
    "print(lista1)\n",
    "print(lista2)\n",
    "print(lista3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] \n",
      "id listy: 2094489267848\n",
      "\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8] \n",
      "id listy: 2094489267848\n",
      "\n",
      "8\n",
      "[0, 1, 2, 3, 4, 5, 6, 7] \n",
      "id listy: 2094489267848\n",
      "\n",
      "[0, 1, 2, 3, 4, 5] \n",
      "id listy: 2094489267848\n",
      "\n",
      "[0, 1, 2, 3, 4] \n",
      "id listy: 2094489267848\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# usuwanie elemetów z listy\n",
    "lista = list(range(10))\n",
    "print(lista, \"\\nid listy: {0}\".format(id(lista)),end='\\n\\n')\n",
    "\n",
    "lista.remove(9)\n",
    "print(lista, \"\\nid listy: {0}\".format(id(lista)),end='\\n\\n')\n",
    "\n",
    "print(lista.pop())\n",
    "print(lista, \"\\nid listy: {0}\".format(id(lista)),end='\\n\\n')\n",
    "\n",
    "lista[6:] = []\n",
    "print(lista, \"\\nid listy: {0}\".format(id(lista)),end='\\n\\n')\n",
    "\n",
    "del lista[-1]\n",
    "print(lista, \"\\nid listy: {0}\".format(id(lista)),end='\\n\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - **Pętle** *for* i *while*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i = 0, j = 0\n",
      "i = 0, j = 1\n",
      "i = 0, j = 2\n",
      "i = 0, j = 3\n",
      "i = 0, j = 4\n",
      "i = 1, j = 0\n",
      "i = 1, j = 1\n",
      "i = 1, j = 2\n",
      "i = 1, j = 3\n",
      "i = 1, j = 4\n",
      "i = 2, j = 0\n",
      "i = 2, j = 1\n",
      "i = 2, j = 2\n",
      "i = 2, j = 3\n",
      "i = 2, j = 4\n",
      "i = 3, j = 0\n",
      "i = 3, j = 1\n",
      "i = 3, j = 2\n",
      "i = 3, j = 3\n",
      "i = 3, j = 4\n",
      "i = 4, j = 0\n",
      "i = 4, j = 1\n",
      "i = 4, j = 2\n",
      "i = 4, j = 3\n",
      "i = 4, j = 4\n"
     ]
    }
   ],
   "source": [
    "# pętla w pętli\n",
    "for i in range(5):\n",
    "    for j in range(5):\n",
    "        print(\"i = {0}, j = {1}\".format(i,j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < 5: # dopóki warunek 'i < 10' jest prawdziwy\n",
    "    print(i)\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Porównanie czasu działania zwykłej pętli *for* i produktowania list "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.52 ms ± 125 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "\n",
    "N = 10**2\n",
    "suma_liczb = []\n",
    "for i in range(N):\n",
    "    for j in range(N):\n",
    "        suma_liczb.append(i + j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 10**2\n",
    "suma_liczb = [0]*(N*N) # tworzymy na początku całą listę wypełnioną zerami"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.46 ms ± 59.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "for i in range(N):\n",
    "    for j in range(N):\n",
    "        suma_liczb[i*N+j] = i + j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "549 µs ± 35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "\n",
    "N = 10**2\n",
    "sumy_liczb = [i + j for i in range(N) for j in range(N)]"
   ]
  },
  {
   "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
}
