forked from tdwojak/Python2018
809 lines
14 KiB
Plaintext
809 lines
14 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"# Ogólne wprowadzenie do Pythona\n",
|
|
"\n",
|
|
" \n",
|
|
"\n",
|
|
"## 19 Listopada 2017"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Hello Python!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"adflakjfsldkfjasd\n",
|
|
"r\n",
|
|
"0.6666666666666666\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"r = \"adflakjfsldkfj\" + \"asd\"\n",
|
|
"print(r)\n",
|
|
"print('r')\n",
|
|
"print(2 / 3.0)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print('Hello Python')\n",
|
|
"print(10) \n",
|
|
"print(3.14)\n",
|
|
"print('123456789 * 987654321')\n",
|
|
"print(10 ** 20)\n",
|
|
"print(12 ** (3 + 4 *(567 % 8) / 9))\n",
|
|
"print(\"PI\", \"jest równe\", 3.1415)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Komentarze\n",
|
|
" * Komentarze nie są interpretowane.\n",
|
|
" * Komentarze w Pythonie zaczynają się od znaku '#'\n",
|
|
" * Istnieją komentarze wielolinijkowe tagowane potrójnym \", czyli \"\"\" \"\"\"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"Bardzo ważna wiadomość\") # A to jest komentarz\n",
|
|
"\"\"\"\n",
|
|
"Komentarz\n",
|
|
"wielo-\n",
|
|
"linijkowy\n",
|
|
"\"\"\"\n",
|
|
"# print(\"Nie chcę być wydrukowanym\")\n",
|
|
"print(\"A teraz chcę\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Zmienne\n",
|
|
" * case sensitive,\n",
|
|
" * brak deklaracji typu,\n",
|
|
" * do zmiennej można pezypisać wszystko."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 36,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"alaalaalaalaalaalaalaala\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 38,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<class 'int'>\n",
|
|
"<class 'float'>\n",
|
|
"<class 'str'>\n",
|
|
"<class 'str'>\n",
|
|
"<class 'NoneType'>\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(type(ocena_z_pythona))\n",
|
|
"print(type(pi))\n",
|
|
"print(type(imie_prowadzacego))\n",
|
|
"print(type(nazwisko_prowadzego))\n",
|
|
"print(type(None))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Listy\n",
|
|
" * W Pythonie nie ma tablic, są listy;\n",
|
|
" * Listy mogą przechowywać elementy różnych typów;\n",
|
|
" * Indeksowanie zaczyna się od 0."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"3\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"pusta_lista = [] # albo równoważnie pusta_lista = list()\n",
|
|
"oceny = [5, 4, 3, 5, 5]\n",
|
|
"roznosci = [3.14, \"pi\", [\"pi\"], 3]\n",
|
|
"#print(oceny[1])\n",
|
|
"ciag_10_elem = range(10)\n",
|
|
"\n",
|
|
"print(oceny[int(12/ 5)])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Dodawanie i usuwanie elementów z listy"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"11\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[1, 3, 2, 3, 1, 2, 4]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"#oceny.pop(0) # Domyślnie usuwa ostatni element z listy\n",
|
|
"oceny = [1,2,3,2,3,1,2, 4]\n",
|
|
"oceny.remove(2)\n",
|
|
"print(oceny)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Indeksowanie"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[1, 3, 2, 3, 1, 2, 4]\n",
|
|
"pierwszy element: 1\n",
|
|
"ostatni element: 4\n",
|
|
"5 pierwszych: [1, 3, 2, 3, 1]\n",
|
|
"5 ostatnich [2, 3, 1, 2, 4]\n",
|
|
"od drugiego, do piątego [3, 2, 3, 1]\n",
|
|
"parzyste: [3, 3, 2]\n",
|
|
"od tyłu [4, 2, 1, 3, 2, 3, 1]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(oceny)\n",
|
|
"print('pierwszy element:', oceny[0])\n",
|
|
"print('ostatni element:', oceny[-1])\n",
|
|
"print('5 pierwszych:', oceny[:5])\n",
|
|
"print('5 ostatnich', oceny[-5:])\n",
|
|
"print('od drugiego, do piątego', oceny[1:5])\n",
|
|
"print('parzyste:', oceny[1:6:2])\n",
|
|
"print('od tyłu', oceny[::-1])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Co możemy zrobić z listą?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 30,
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[1, 1, 2, 2, 3, 3, 4]\n",
|
|
"liczba piątek 0\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"oceny.sort()\n",
|
|
"print(oceny)\n",
|
|
"oceny.reverse()\n",
|
|
"print(\"liczba piątek\", oceny.count(5))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Pętla typu ```for```\n",
|
|
" * W Pythonie pętla *for* działa jak pętla *for each* w innych językach;\n",
|
|
" * W Pythonie nie ma klamr, ani *begin* i *end*; jako ciało pętli musi być wcięte (najcześciej 4 spacje)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for i in range(10):# range[5] = [0,1,2,3,4]\n",
|
|
" pass\n",
|
|
" \n",
|
|
"print('element:', i) # Uwaga wcięcie!\n",
|
|
"print('sqr:', i **2) # Uwaga wcięcie!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"source": [
|
|
"Schemat:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for zmienna in lista:\n",
|
|
" # jakiś kod"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for ocena in oceny:\n",
|
|
" print(\"Ocena:\", ocena)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Lista kwadratów"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"kwadraty = []\n",
|
|
"for i in range(10):\n",
|
|
" kwadraty.append(i ** 2)\n",
|
|
"print(kwadraty)\n",
|
|
"print(kwadraty[5])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Słowniki, czyli mapy"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"slownik = {}\n",
|
|
"s_oceny = {\"Justyna\" : [5,5,5], \"Bartek\" : [3,4,5], \"Ola\": [3,3,3]}\n",
|
|
"print(s_oceny)\n",
|
|
"s_oceny[1234567890] = [3,4,5]\n",
|
|
"#print(\"ADAM:\", s_oceny[\"Adam\"])\n",
|
|
"print(s_oceny)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"s_oceny['Ala'] = [2,2,2,4,4,4,3,3,3]\n",
|
|
"print(s_oceny)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Iterowanie po słowniku"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for osoba in s_oceny:\n",
|
|
" print(osoba,':', s_oceny[osoba])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for osoba, oceny in s_oceny.items():\n",
|
|
" print(osoba,':', oceny)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Instrukcja sterująca ```if ... else```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"source": [
|
|
" * działa tak samo, jak w innych języka;\n",
|
|
" * jest dodatkowa instrukcja następnego warunku *elif...*;"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"zmienna = 7\n",
|
|
"if zmienna < 0:\n",
|
|
" print(\"Ujemna!\")\n",
|
|
"elif zmienna == 0:\n",
|
|
" print(\"Zero!\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"if \"Ala\" in s_oceny:\n",
|
|
" print(\"Ala jest w grupie!\")\n",
|
|
"else:\n",
|
|
" print(\"Niestety Ali nie ma grupie!\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Funkcje"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def is_greater_than_5(x):\n",
|
|
" if x > 5:\n",
|
|
" return True\n",
|
|
" else:\n",
|
|
" return False"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(is_greater_than_5(5))\n",
|
|
"print(is_greater_than_5(-100))\n",
|
|
"print(is_greater_than_5(500))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"# Łańcuchy znakowe"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"napis = \"Wiadomość\"\n",
|
|
"print(napis)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for znak in napis:\n",
|
|
" print(znak, ord(znak))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"if 'dom' in napis:\n",
|
|
" print(True)\n",
|
|
"else:\n",
|
|
" print(False)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"slowa = ['Bardzo' , 'ważna', 'wiadomość']\n",
|
|
"print(' '.join(slowa))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"text = \"Bardzo ważna wiadomość\"\n",
|
|
"print(text.split(' '))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"slideshow": {
|
|
"slide_type": "fragment"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"text = \"Nie wszyscy lubią spacje na końcu linii. \"\n",
|
|
"print(text)\n",
|
|
"print(text.strip(' '))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "slide"
|
|
}
|
|
},
|
|
"source": [
|
|
"# Pytania?"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"celltoolbar": "Slideshow",
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3.0
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.6.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
} |