add postawy 3

This commit is contained in:
Tomasz Dwojak 2019-02-09 10:09:44 +01:00
parent 12a770ac59
commit e6ab48cc18

View File

@ -0,0 +1,719 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Python: podstawy, cz. 3 (ostatnia)\n",
"\n",
" \n",
"## 9 lutego 2019"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Kilka uwag do zadania domowego:"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def even_elements(list):\n",
" output = []\n",
" for element in range(len(list)):\n",
" if element % 2 == 0:\n",
" output.append(list[element])\n",
" return output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def even_elements(elems):\n",
" output = []\n",
" for element in range(len(elems)):\n",
" if element % 2 == 0:\n",
" output.append(elems[element])\n",
" return output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def leet_speak(text):\n",
" text2 = \"\"\n",
" i = 0\n",
" suma = 0\n",
" for e in text:\n",
" if text[i] == \"e\":\n",
" text2 = text2 + \"3\"\n",
" i = i + 1\n",
" else:\n",
" if text[i] == \"l\":\n",
" text2 = text2 + \"1\"\n",
" i = i + 1\n",
" else:\n",
" if text[i] == \"o\":\n",
" text2 = text2 + \"0\"\n",
" i = i + 1\n",
" else:\n",
" if text[i] == \"t\":\n",
" text2 = text2 + \"7\"\n",
" i = i + 1\n",
" else:\n",
" text2 = text2 + text[i]\n",
" i = i + 1\n",
" return (text2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def leet_speak(text):\n",
" text2 = \"\"\n",
" i = 0\n",
" suma = 0\n",
" for e in text:\n",
" if text[i] == \"e\":\n",
" text2 = text2 + \"3\"\n",
" elif text[i] == \"l\":\n",
" text2 = text2 + \"1\"\n",
" elif text[i] == \"o\":\n",
" text2 = text2 + \"0\"\n",
" elif text[i] == \"t\":\n",
" text2 = text2 + \"7\"\n",
" else:\n",
" text2 = text2 + text[i]\n",
" i = i + 1\n",
" return (text2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Do tej pory:\n",
" * podstawowe typy danych\n",
" * pętla `for` i wyrażenie warunkowe `if`\n",
" * listy, słowniki\n",
" * funkcje"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Wbudowane funkcje w Pythona"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 18 36\n"
]
}
],
"source": [
"elems = [1, 5, 10, 18, 2]\n",
"print(min(elems), max(elems), sum(elems))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index: 0 wartość: 1\n",
"Index: 1 wartość: 5\n",
"Index: 2 wartość: 10\n",
"Index: 3 wartość: 18\n",
"Index: 4 wartość: 2\n",
"Index: 0 wartość: 1\n",
"Index: 1 wartość: 5\n",
"Index: 2 wartość: 10\n",
"Index: 3 wartość: 18\n",
"Index: 4 wartość: 2\n"
]
}
],
"source": [
"elems = [1, 5, 10, 18, 2]\n",
"for idx, elem in enumerate(elems):\n",
" print(\"Index:\", idx, \"wartość:\", elem)\n",
"\n",
"for idx in range(len(elems)):\n",
" elem = elems[idx]\n",
" print(\"Index:\", idx, \"wartość:\", elem)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alicja lat 1 .\n",
"Beata lat 5 .\n",
"Cecylia lat 10 .\n",
"Danuta lat 18 .\n",
"Eryka lat 2 .\n"
]
}
],
"source": [
"ages = [1, 5, 10, 18, 2, 1]\n",
"names = [\"Alicja\", \"Beata\", \"Cecylia\", \"Danuta\", \"Eryka\"]\n",
"for age, name in zip(ages, names):\n",
" print(name, \"lat\", age, \".\")"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[18.84, 25.12, 31.400000000000002, 37.68]\n"
]
}
],
"source": [
"def circle_length(radius):\n",
" return 2 * 3.14 * radius\n",
"radii = [3, 4, 5, 6]\n",
"\n",
"results = []\n",
"for r in radii:\n",
" results.append(circle_length(r))\n",
"\n",
"results = list(map(circle_length, radii))\n",
"print(results)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12.56\n",
"[18.84, 25.12, 31.400000000000002, 37.68]\n"
]
}
],
"source": [
"radii = [3, 4, 5, 6]\n",
"\n",
"f = lambda radius: 2 * 3.14 * radius\n",
"print(f(2))\n",
"results = map(f, radii)\n",
"print(list(results))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Funkcje lambda\n",
"\n",
" * składnia: `lambda nazwa_zmiennej: zwracana_wartość`"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Co mam zrobić?\n",
"print(\"ala ma kota.\")\n",
"ala ma kota.\n"
]
}
],
"source": [
"code = input(\"Co mam zrobić?\\n\")\n",
"eval(code)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Listy składane"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 2, 2, 2, 3, 5]\n"
]
}
],
"source": [
"elems = [10, 50, 40, 30, 100, 19990]\n",
"lens = []\n",
"for elem in elems:\n",
" lens.append(len(str(elem)))\n",
"print(lens)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 2, 2, 2, 3, 5]\n"
]
}
],
"source": [
"elems = [10, 50, 40, 30, 100, 19990]\n",
"lens = [len(str(elem)) for elem in elems]\n",
"print(lens)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 5]\n"
]
}
],
"source": [
"elems = [10, 50, 40, 30, 100, 19990]\n",
"lens = [len(str(elem)) for elem in elems \n",
" if str(elem)[0] == '1']\n",
"print(lens)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 0, 0, 0, 3, 5]\n"
]
}
],
"source": [
"elems = [10, 50, 40, 30, 100, 19990]\n",
"lens = [len(str(elem)) \n",
" if str(elem)[0] == '1' else 0\n",
" for elem in elems]\n",
"print(lens)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 14, [16, 18.0]] 2\n"
]
}
],
"source": [
"def times(elem, n=2):\n",
" if isinstance(elem, list):\n",
" return [times(item, n) for item in elem]\n",
" elif isinstance(elem, float) or isinstance(elem, int):\n",
" return elem * n\n",
" else:\n",
" raise ValueError(\"błąd\")\n",
"\n",
"print(times([5, 7, [8, 9.0]]), 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Formatowanie napisów\n",
"\n",
" * procentowe formatowanie z Pythona 2\n",
" * metoda format\n",
" * f-stringi (Python 3.6)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Jan Kowalski, lat 34\n"
]
}
],
"source": [
"imie = \"Jan\"\n",
"nazwisko = \"Kowalski\"\n",
"wiek = 34\n",
"\n",
"full_name = imie + \" \" + nazwisko + \", lat \" + str(wiek)\n",
"full_name = \"%s %s, lat %d\" % (imie, nazwisko, wiek)\n",
"print(full_name)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Jan Kowalski, lat 34\n",
"Jan Kowalski, lat 34\n"
]
}
],
"source": [
"imie = \"Jan\"\n",
"nazwisko = \"Kowalski\"\n",
"wiek = 34\n",
"\n",
"full_name = \"{0} {0}, lat {2}\".format(imie, nazwisko, wiek)\n",
"print(full_name)\n",
"full_name = f\"{imie} {nazwisko}, lat {wiek}\"\n",
"print(full_name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## OOP: Programowanie obiektowe"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class Sentence:\n",
" pass\n",
"sentence = Sentence()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class Sentence:\n",
" def __init__(self, text):\n",
" self.text = text\n",
"sentence = Sentence(\"This is a little test.\")"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"text: This is a little test.\n"
]
}
],
"source": [
"sent_1 = Sentence(\"This is a little test.\")\n",
"sent_2 = Sentence(\"This is a little test.\")\n",
"print(sent_1 == sent_2)\n",
"print(\"text: {}\".format(sent_1.text))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Liczba słów: 4\n"
]
}
],
"source": [
"class Sentence:\n",
" def __init__(self, text):\n",
" self.text = text\n",
" def word_number(self):\n",
" return len(self.text.split(' '))\n",
" \n",
" def is_question(self):\n",
" return self.text[-1] == \"?\"\n",
" \n",
"sent = Sentence(\"To jest test .\")\n",
"print(\"Liczba słów: {}\".format(sent.word_number()))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6500\n"
]
}
],
"source": [
"class Employee:\n",
" def __init__(self, name, salary):\n",
" self.name = name\n",
" self.salary = salary\n",
" def raise_salary(self, amount):\n",
" self.salary += amount\n",
" \n",
"employee = Employee(\"Franek\", 6000)\n",
"employee.raise_salary(500)\n",
"print(employee.salary)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class Manager(Employee):\n",
" def __init__(self, name, salary, team):\n",
" super().__init__(name, salary)\n",
" self.team = team\n",
"\n",
"man = Manager(\"Joanna\", 6000, \"super-team\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}