Last active
August 29, 2015 14:17
-
-
Save p2004a/7efe2fea338704f13ed9 to your computer and use it in GitHub Desktop.
Python tutorial for programmers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:705f9a4986a12c758e0e3c34f8a36be0f86e0122f35f70816838c794a75f2d54" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Python cz\u0119\u015b\u0107 2" | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"List/Set/Dict comprehensions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[x*x for x in range(5)]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 1, | |
"text": [ | |
"[0, 1, 4, 9, 16]" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"{x for x in range(5)}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 2, | |
"text": [ | |
"{0, 1, 2, 3, 4}" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"{2 for x in range(5)}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"{2}" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"{x: x*x for x in range(5)}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"{i: x*x for x in range(5) for i in range(5)}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"{0: 16, 1: 16, 2: 16, 3: 16, 4: 16}" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"Formatowanie string\u00f3w w starym stylu" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"asd\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 6, | |
"text": [ | |
"'asd'" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"asd %d\" % 5" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"'asd 5'" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"asd %04d %s\" % (1,\"asd\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": [ | |
"'asd 0001 asd'" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"asd %(alfa)s %(beta)s\" % {'alfa': \"31\", 'beta': \"123\"}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 9, | |
"text": [ | |
"'asd 31 123'" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Formatowanie string\u00f3w w nowym stylu\n", | |
"\n", | |
"Opis format string\u00f3w https://docs.python.org/3.4/library/string.html#format-string-syntax" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"{} ma {}\".format(\"ala\", \"kota\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"'ala ma kota'" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"{kto} ma {co}\".format(kto=\"ala\", co=\"kota\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 11, | |
"text": [ | |
"'ala ma kota'" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"{0} ma {1}\".format(\"kota\", \"ala\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": [ | |
"'kota ma ala'" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"'{0:<10}' '{0:>10}' '{0:^10}'\".format(\"asd\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 13, | |
"text": [ | |
"\"'asd ' ' asd' ' asd '\"" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"{0[a]} {0[a][0]}\".format({'a': [1,2,3]})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 14, | |
"text": [ | |
"'[1, 2, 3] 1'" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import string\n", | |
"\"{0.ascii_letters}\".format(string)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": [ | |
"'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"{0[a]:{1}10}\".format({'a': \"asd\"}, '>')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 16, | |
"text": [ | |
"' asd'" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"\"Prawdziwo\u015b\u0107\" obiekt\u00f3w" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"if [1,2,2]:\n", | |
" print(\"TAK\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"TAK\n" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"if []:\n", | |
" print(\"TAK\")\n", | |
"else:\n", | |
" print(\"NIE\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"NIE\n" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Kolekcje\n", | |
"\n", | |
"Opr\u00f3cz standardowych set\u00f3w, list i dict\u00f3w wiele przydatnych znajduje si\u0119 z module `collections` https://docs.python.org/3.4/library/collections.html.\n", | |
"Przede wszystim polecam spojrze\u0107 na `namedtuple`." | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"Klasy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class F(object):\n", | |
" def dodaj(self, a, b):\n", | |
" return a + b\n", | |
"\n", | |
"a = F()\n", | |
"print(a.dodaj(1,2))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"3\n" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Dodawacz(object):\n", | |
" def __init__(self, a):\n", | |
" self.a = a\n", | |
" \n", | |
" def dodaj(self, b):\n", | |
" return self.a + b\n", | |
" \n", | |
"a = Dodawacz(5)\n", | |
"print(a.dodaj(10))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"15\n" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"Iteratory" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def urange(n):\n", | |
" while n > 0:\n", | |
" yield n\n", | |
" n -= 1\n", | |
" \n", | |
"print(type(urange(10)))\n", | |
"for i in urange(10):\n", | |
" print(i)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<class 'generator'>\n", | |
"10\n", | |
"9\n", | |
"8\n", | |
"7\n", | |
"6\n", | |
"5\n", | |
"4\n", | |
"3\n", | |
"2\n", | |
"1\n" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def rrange(n):\n", | |
" class RRange(object):\n", | |
" def __init__(self, n):\n", | |
" self.n = n\n", | |
" \n", | |
" def __iter__(self):\n", | |
" return self\n", | |
" #return range(10).__iter__()\n", | |
" \n", | |
" def __next__(self):\n", | |
" if self.n <= 0:\n", | |
" raise StopIteration\n", | |
" n = self.n\n", | |
" self.n -= 1\n", | |
" return n\n", | |
" \n", | |
" return RRange(n)\n", | |
"\n", | |
"print(type(rrange(10)))\n", | |
"\n", | |
"for i in rrange(10):\n", | |
" print(i)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<class '__main__.rrange.<locals>.RRange'>\n", | |
"10\n", | |
"9\n", | |
"8\n", | |
"7\n", | |
"6\n", | |
"5\n", | |
"4\n", | |
"3\n", | |
"2\n", | |
"1\n" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Obiekt `locals`\n", | |
"\n", | |
"Do obiektu `locals` analogiczny jest obiekt `globals`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f():\n", | |
" a = 10\n", | |
" b = 5\n", | |
" return locals()\n", | |
"\n", | |
"x = f()\n", | |
"print(x['a'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"Metody instancji, klasy i statyczne" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class PPP(object):\n", | |
" def public(self):\n", | |
" pass\n", | |
" \n", | |
" def _protected(self):\n", | |
" pass\n", | |
" \n", | |
" def __pivate(self):\n", | |
" pass" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class StaticMethods(object):\n", | |
" @staticmethod\n", | |
" def func(a, b):\n", | |
" \"dokumentcja funkcji\"\n", | |
" return a + b\n", | |
" \n", | |
"print(StaticMethods.func(2,4))\n", | |
"print(StaticMethods.func.__doc__)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"6\n", | |
"dokumentcja funkcji\n" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class CM(object):\n", | |
" a = {'a': 10}\n", | |
"\n", | |
" @classmethod\n", | |
" def f(cls):\n", | |
" print(cls, cls.a)\n", | |
"\n", | |
" def inc_a(self):\n", | |
" self.a['a'] += 1\n", | |
" return self.a['a']\n", | |
"\n", | |
"CM.f()\n", | |
"\n", | |
"a = CM()\n", | |
"b = CM()\n", | |
"\n", | |
"print(a.inc_a())\n", | |
"print(b.inc_a())" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<class '__main__.CM'> {'a': 10}\n", | |
"11\n", | |
"12\n" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Przeci\u0105\u017canie operator\u00f3w\n", | |
"\n", | |
"Najbardziej og\u00f3lnie: https://docs.python.org/3.4/reference/datamodel.html#special-method-names\n", | |
"\n", | |
"Co do samych operator\u00f3w arytmetycznych: https://docs.python.org/3.4/reference/datamodel.html#emulating-numeric-types" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Liczba(object):\n", | |
" def __init__(self, n):\n", | |
" super().__init__()\n", | |
" self.n = n\n", | |
"\n", | |
" def __add__(self, rhs):\n", | |
" if type(rhs) is not Liczba:\n", | |
" rhs = Liczba(rhs)\n", | |
" return Liczba(self.n + rhs.n)\n", | |
" \n", | |
" def __radd__(self, lhs):\n", | |
" return self + lhs\n", | |
"\n", | |
" def __str__(self):\n", | |
" return str(self.n)\n", | |
" \n", | |
" def __iadd__(self, n):\n", | |
" print(\"asd\")\n", | |
" self.n += n\n", | |
" return self\n", | |
" \n", | |
" def __getitem__(self, n):\n", | |
" return (self.n & (1 << n)) >> n\n", | |
"\n", | |
" def __iter__(self):\n", | |
" for i in range(10):\n", | |
" yield self[i]\n", | |
" \n", | |
"a = Liczba(2)\n", | |
"b = Liczba(3)\n", | |
"\n", | |
"print(a)\n", | |
"a += 2\n", | |
"print(a)\n", | |
"\n", | |
"print(a + b)\n", | |
"print(a + 9)\n", | |
"print(9 + a)\n", | |
"\n", | |
"print(a[0], a[1])\n", | |
"\n", | |
"print(list(a))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"2\n", | |
"asd\n", | |
"4\n", | |
"7\n", | |
"13\n", | |
"13\n", | |
"0 0\n", | |
"[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]\n" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 27 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment