{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Dnes se naučíte používat základní typy v Pythonu:\n", "* numerické typy jako je `int` nebo `float`,\n", "* řetězce (`string`).\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numerické typy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python má zabudované čtyři základní numerické typy:\n", "\n", "1. celá čísla: `int` (Python 2 měl ještě `long`)\n", "2. desetinná čísla: `float` (odpovídá double v C)\n", "3. komplexní čísla: `complex`\n", "4. logické (boolean): `bool`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 - 3 # int - celá čísla" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10, 2, 16, 8\n", "To samé binárně\n", "0b1010, 0b10, 0b10000, 0b1000\n" ] } ], "source": [ "int1 = 10 # desítková soustava\n", "int2 = 0b10 # binární\n", "int3 = 0x10 # hexadecimální\n", "int4 = 0o10 # osmičková\n", "print(\"%i, %i, %i, %i\" % (int1, int2, int3, int4))\n", "print(\"To samé binárně\")\n", "print(\"%s, %s, %s, %s\" % (bin(int1), bin(int2), bin(int3), bin(int4)))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.6666666666666666\n", "0\n", "0.0\n" ] } ], "source": [ "print(2 / 3) # dělení s desetinnou čárkou (vždy v Pythonu 3)\n", "print(2 // 3) # operátor pro celočíselné dělení\n", "print(2.0 // 3.0) # a pro desetinná čísla" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "type(1) = \n", "type(10000000000000000000) = \n", "999^999 = 368063488259223267894700840060521865838338232037353204655959621437025609300472231530103873614505175218691345257589896391130393189447969771645832382192366076536631132001776175977932178658703660778465765811830827876982014124022948671975678131724958064427949902810498973271030787716781467419524180040734398996952930832508934116945966120176735120823151959779536852290090377452502236990839453416790640456116471139751546750048602189291028640970574762600185950226138244530187489211615864021135312077912018844630780307462205252807737757672094320692373101032517459518497524015120165166724189816766397247824175394802028228160027100623998873667435799073054618906855460488351426611310634023489044291860510352301912426608488807462312126590206830413782664554260411266378866626653755763627796569082931785645600816236891168141774993267488171702172191072731069216881668294625679492696148976999868715671440874206427212056717373099639711168901197440416590226524192782842896415414611688187391232048327738965820265934093108172054875188246591760877131657895633586576611857277011782497943522945011248430439201297015119468730712364007639373910811953430309476832453230123996750235710787086641070310288725389595138936784715274150426495416196669832679980253436807864187160054589045664027158817958549374490512399055448819148487049363674611664609890030088549591992466360050042566270348330911795487647045949301286614658650071299695652245266080672989921799342509291635330827874264789587306974472327718704306352445925996155619153783913237212716010410294999877569745287353422903443387562746452522860420416689019732913798073773281533570910205207767157128174184873357050830752777900041943256738499067821488421053870869022738698816059810579221002560882999884763252161747566893835178558961142349304466506402373556318707175710866983035313122068321102457824112014969387225476259342872866363550383840720010832906695360553556647545295849966279980830561242960013654529514995113584909050813015198928283202189194615501403435553060147713139766323195743324848047347575473228198492343231496580885057330510949058490527738662697480293583612233134502078182014347192522391449087738579081585795613547198599661273567662441490401862839817822686573112998663038868314974259766039340894024308383451039874674061160538242392803580758232755749310843694194787991556647907091849600704712003371103926967137408125713631396699343733288014254084819379380555174777020843568689927348949484201042595271932630685747613835385434424807024615161848223715989797178155169951121052285149157137697718850449708843330475301440373094611119631361702936342263219382793996895988331701890693689862459020775599439506870005130750427949747071390095256759203426671803377068109744629909769176319526837824364926844730545524646494321826241925107158040561607706364484910978348669388142016838792902926158979355432483611517588605967745393958061959024834251565197963477521095821435651996730128376734574843289089682710350244222290017891280419782767803785277960834729869249991658417000499998999\n", "999 ^ 999 má 2997 cifer.\n" ] } ], "source": [ "print(\"type(1) = %s\" % type(1))\n", "# Pro velká celá čísla máme long\n", "print(\"type(10000000000000000000) = %s\" % type(10000000000000000000)) \n", "# Velká celá čísla mohou být faaakt veliká.\n", "velke_cislo = 999**999 \n", "print(\"999^999 = %d\" % velke_cislo)\n", "print(\"999 ^ 999 má {} cifer.\".format(len(str(velke_cislo))))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Výpočty s desetinnými čísly jsou standardní, pozor na konečnou přesnost" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.031000000000000007" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.1 * (0.2 - 0.1)**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python umí komplexní čísla" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(1 - 1j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "S boolovskými hodnotami se pracuje snadno" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "b = True # boolean\n", "print(not b) # logický operátor not" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Řetezce -- string a unicode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python obsahuje typy `str` a `unicode` (Python 3 je sjednocuje do `builtins.str`) a bohatou sadu funkcí pro práci s nimi. Ty jsou implementovány jednak jako metody (o třídách a metodách více později), jednak v modulech `string`, `StringIO`, `re` a dalších." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Abc3Abc3Abc3!\n" ] } ], "source": [ "a = 3 * (\"Abc%i\" % 3) + \"!\" # string lze vytvořit například takto\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Můžeme přistupovat k jednotlivým částem řetězce pomocí indexů" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n", "Abc3Abc3Abc3\n", "!\n" ] } ], "source": [ "print(a[0]) # první znak řetězce\n", "print(a[0:-1]) # všechny znaky kromě posledního\n", "print(a[-1:None]) # poslední znak (None lze vynechat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Metoda [`format`](https://docs.python.org/2/library/stdtypes.html#str.format) umožňuje pokročilé formátování textu." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abracadabra\n", "ABRACADABRA\n", "2\n", "['', 'BR', 'C', 'D', 'BR', '']\n" ] } ], "source": [ "a = '{0}{1}{0}'.format('abra', 'cad') # metoda format\n", "print(a)\n", "print(a.upper()) # metoda upper\n", "print(a.find(\"ra\")) # jednoduché hledání\n", "print(a.upper().split('A')) # rozdělení dělícím znakem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modul [`string`](https://docs.python.org/2/library/string.html) obsahuje další funkce." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']\n" ] } ], "source": [ "import string # načtení modulu string\n", "print(dir(string)) # výpis obsahu modulu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "vyzkoušíme `capwords`" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Titles In English Use Capital First Letters.\n" ] } ], "source": [ "print(string.capwords(\"titles in english use capital first letters.\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Konverze typů\n", "\n", "Python je silně typovaný, pamatujete? Typy lze ale převádět." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str -> int\n", "int(\"2\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str -> float\n", "float(\"2\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# float -> str\n", "str(3.14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Projekt - oko bere\n", "(kredit https://naucse.python.cz/course/pyladies/beginners/while/)\n", "\n", "Doplňte cybějící části označené ??? a opravte chyby, aby fungoval program na hru oko bere: " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 4)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m while ???:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "import random\n", "\n", "soucet = 0\n", "while ???:\n", " print('Máš', soucet, 'bodů')\n", " odpoved = input('Otočit kartu? ')\n", " if odpoved == 'ano':\n", " ??? = random.randrange(2, 11)\n", " print('Otočil/a jsi', karta)\n", " soucet = ???\n", " elif ???:\n", " break\n", " else:\n", " ???\n", "\n", "if soucet == 21:\n", " print('Gratuluji! Vyhrál/a jsi!')\n", "???\n", " print('Smůla!', soucet, 'bodů je moc!')\n", "else:\n", " print('Chybělo jen', 21 - soucet, 'bodů!')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vylepšete předchozí kód, aby \n", "1. se zeptal na počet otáčených karet\n", "2. hráč vyhrál, pokud má součet méně nebo rovno 21 a soupoeř (počítač) má méně bodů než hráč. Skóre počítače generujte pomocí `random.normalvariate`.\n", "\n", "*Nápověda - použijte konverzi *str* na `int`.*" ] }, { "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.10" } }, "nbformat": 4, "nbformat_minor": 2 }