{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Numbers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "2**31" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "2147483648" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "2**63" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "9223372036854775808L" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "2**64" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "18446744073709551616L" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "2**128" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "340282366920938463463374607431768211456L" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "2**4000" ], "language": "python", "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "13182040934309431001038897942365913631840191610932727690928034502417569281128344551079752123172122033140940756480716823038446817694240581281731062452512184038544674444386888956328970642771993930036586552924249514488832183389415832375620009284922608946111038578754077913265440918583125586050431647284603636490823850007826811672468900210689104488089485347192152708820119765006125944858397761874669301278745233504796586994514054435217053803732703240283400815926169348364799472716094576894007243168662568886603065832486830606125017643356469732407252874567217733694824236675323341755681839221954693820456072020253884371226826844858636194212875139566587445390068014747975813971748114770439248826688667129237954128555841874460665729630492658600179338272579110020881228767361200603478973120168893997574353727653998969223092798255701666067972698906236921628764772837915526086464389161570534616956703744840502975279094087587298968423516531626090898389351449020056851221079048966718878943309232071978575639877208621237040940126912767610658141079378758043403611425454744180577150855204937163460902512732551260539639221457005977247266676344018155647509515396711351487546062479444592779055555421362722504575706910949376L" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "len(str(2**4000))" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "1205" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Data types" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,1,2,3]\n", "arr" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "[0, 1, 2, 3]" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [1,2,'cos', [10,11,12], 3.14]\n", "arr" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "[1, 2, 'cos', [10, 11, 12], 3.14]" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,1,2,3]\n", "arr" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "[0, 1, 2, 3]" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "len(arr)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "4" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "1" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "3" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[1:3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "[1, 2]" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[:3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "[0, 1, 2]" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[-2:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "[2, 3]" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[-3:-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "[1, 2]" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,1,2,3,4,5,6]\n", "arr[0:5]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "[0, 1, 2, 3, 4]" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[0:5:2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "[0, 2, 4]" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[::2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "[0, 2, 4, 6]" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "arr[::-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "[6, 5, 4, 3, 2, 1, 0]" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "a=[0,1,2,3]\n", "b=['a','b','c','d']\n", "a+b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "[0, 1, 2, 3, 'a', 'b', 'c', 'd']" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "a=[10,20,30,40]\n", "a.append(100)\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "[10, 20, 30, 40, 100]" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "a=[10,20,20,30,40]\n", "a.remove(20)\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "[10, 20, 30, 40]" ] } ], "prompt_number": 27 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Tuples" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = (1,2,3)\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "(1, 2, 3)" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "a[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "1" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "a[1]=1" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "prompt_number": 30 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Dictionaries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "oceny = {'polski': 3.5, 'matematyka': 5, 'fizyka': 4, \n", " 'informatyka': 6}\n", "oceny" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "{'fizyka': 4, 'informatyka': 6, 'matematyka': 5, 'polski': 3.5}" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "oceny['informatyka']" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "6" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "oceny['nowy przedmiot'] = 2.0\n", "oceny" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "{'fizyka': 4,\n", " 'informatyka': 6,\n", " 'matematyka': 5,\n", " 'nowy przedmiot': 2.0,\n", " 'polski': 3.5}" ] } ], "prompt_number": 33 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Sets" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ulubione_przedmioty = set(['polski', 'muzyka', 'geografia', 'historia'])\n", "ulubione_przedmioty" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "{'geografia', 'historia', 'muzyka', 'polski'}" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "'informatyka' in ulubione_przedmioty" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "False" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "if 'polski' in ulubione_przedmioty:\n", " print('Kocham j. polski!')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Kocham j. polski!\n" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "a = set(['a','b','c','d'])\n", "b = set(['c','d','e','f'])\n", "a.union(b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "{'a', 'b', 'c', 'd', 'e', 'f'}" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "a.intersection(b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "{'c', 'd'}" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "a.difference(b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "{'a', 'b'}" ] } ], "prompt_number": 39 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Strings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print('Imi\u0119: {}'.format('Wojciech'))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from math import pi\n", "print(' Imi\u0119: {},\\n Nazwisko: {},\\n PESEL: {},\\n Ulubiona: {}\\n'\n", " .format('Wojciech', 'Ja\u015bkowski', '8212...', pi))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Imi\u0119: Wojciech,\n", " Nazwisko: Ja\u015bkowski,\n", " PESEL: 8212...,\n", " Ulubiona: 3.14159265359\n", "\n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "from math import pi\n", "print('''\n", "Imi\u0119: {imie},\n", "Nazwisko: {nazw},\n", "PESEL: {pesel},\n", "Ulubiona: {ulub}\n", "'''.format(\n", " imie='Wojciech', \n", " nazw='Ja\u015bkowski', \n", " pesel='8212...', \n", " ulub=pi))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Imi\u0119: Wojciech,\n", "Nazwisko: Ja\u015bkowski,\n", "PESEL: 8212...,\n", "Ulubiona: 3.14159265359\n", "\n" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "print('\u0119\u015b\u0107\u0144\u017c\u00f3\u0142')\n", "print('\u05d9\u05d4\u05d5\u05d4')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\u0119\u015b\u0107\u0144\u017c\u00f3\u0142\n", "\u05d9\u05d4\u05d5\u05d4\n" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'ala'\n", "print(s[0:2])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "al\n" ] } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "print('ala')\n", "print(\"ala\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ala\n", "ala\n" ] } ], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "print('ala ma \"kota\"')\n", "print(\"ala ma 'kota'\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ala ma \"kota\"\n", "ala ma 'kota'\n" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "str(12)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "'12'" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "int('12')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "12" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "float('12.1')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "12.1" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "text = 'Python is easy;really?'\n", "text.split()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "['Python', 'is', 'easy;really?']" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "text.split(';')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": [ "['Python is easy', 'really?']" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "import re\n", "re.split(' |;', text)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 51, "text": [ "['Python', 'is', 'easy', 'really?']" ] } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = ['a','b','c','d']\n", "str(arr)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": [ "\"['a', 'b', 'c', 'd']\"" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "'; '.join(arr)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": [ "'a; b; c; d'" ] } ], "prompt_number": 53 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Flow control" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arr = ['a', 'b', 'c', 'd']\n", "for a in arr:\n", " print(a)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "b\n", "c\n", "d\n" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "range(4)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "[0, 1, 2, 3]" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(len(arr)):\n", " print(i, arr[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(0, 'a')\n", "(1, 'b')\n", "(2, 'c')\n", "(3, 'd')\n" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "for i, a in enumerate(arr):\n", " print(i, a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(0, 'a')\n", "(1, 'b')\n", "(2, 'c')\n", "(3, 'd')\n" ] } ], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0,1,2,3]\n", "b = ['a','b','c','d','e']\n", "n = len(a)\n", "for i in range(n):\n", " print(a[i], b[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(0, 'a')\n", "(1, 'b')\n", "(2, 'c')\n", "(3, 'd')\n" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0,1,2,3]\n", "b = ['a','b','c']\n", "for x, y in zip(a, b):\n", " print(x, y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(0, 'a')\n", "(1, 'b')\n", "(2, 'c')\n" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "zip(a,b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "[(0, 'a'), (1, 'b'), (2, 'c')]" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "c=['ala','ma','kota','!']\n", "zipped = zip(a,b,c)\n", "zipped" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 62, "text": [ "[(0, 'a', 'ala'), (1, 'b', 'ma'), (2, 'c', 'kota')]" ] } ], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "a, b, c = zip(*zipped)\n", "print(a)\n", "print(b)\n", "print(c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(0, 1, 2)\n", "('a', 'b', 'c')\n", "('ala', 'ma', 'kota')\n" ] } ], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,1,2,3]\n", "for a in arr:\n", " if a % 3 == 0:\n", " print('{} = 0 (mod 3)'.format(a))\n", " elif a % 3 == 1:\n", " print('{} = 1 (mod 3)'.format(a))\n", " else:\n", " print('{} = 2 (mod 3)'.format(a))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'python'\n", "while s[0] != 'h':\n", " s = s[1:]\n", "print(s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hon\n" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 0\n", "if -1 <= a and a <= 1:\n", " print 'A is in range'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A is in range\n" ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "if -1 <= a <= 1:\n", " print 'A is in range'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A is in range\n" ] } ], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1, 2, 3]\n", "if not 4 in a:\n", " print '4 is missing'\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 is missing\n" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "# or, continue, break\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Iterating over dictionaries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'a': 1, 'b': 2, 12: 'mama'}\n", "for v in d:\n", " print(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "b\n", "12\n" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "for k, v in d.items():\n", " print(str(k) + ' => ' + str(v))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a => 1\n", "b => 2\n", "12 => mama\n" ] } ], "prompt_number": 71 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def suma(a, b): \n", " return a + b\n", " \n", "suma(1,3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 75, "text": [ "4" ] } ], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "def suma(a, b, wa=1, wb=1):\n", " return wa * a + wb * b\n", "\n", "print(suma(1, 3))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] } ], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "print(suma(1, 3, 2, 1))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] } ], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "print(suma(1, 3, wa=2, wb=1))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] } ], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "print(suma(1, 3, wb=2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] } ], "prompt_number": 78 }, { "cell_type": "code", "collapsed": false, "input": [ "def f(arr):\n", " n = x = arr[0]\n", " for a in arr:\n", " if n > a:\n", " n = a\n", " if x < a:\n", " x = a\n", " return n, x\n", "\n", "print(f([2,3,1,4]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(1, 4)\n" ] } ], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "a, b = f([2,3,1,4])\n", "print(a)\n", "print(b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "4\n" ] } ], "prompt_number": 81 }, { "cell_type": "code", "collapsed": false, "input": [ "x = f([2,3,1,4])\n", "x" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 82, "text": [ "(1, 4)" ] } ], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "b = 2\n", "a, b = b, a\n", "print('a = ' + str(a))\n", "print('b = ' + str(b))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a = 2\n", "b = 1\n" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "x = ['ala','ma','kota']\n", "a, b, c = x\n", "print('a = ' + str(a))\n", "print('b = ' + str(b))\n", "print('c = ' + str(c))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a = ala\n", "b = ma\n", "c = kota\n" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "def f(wa=1, wb=1): \n", " def g(a, b):\n", " return a * wa + b * wb;\n", " return g\n", "\n", "f(1,3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 85, "text": [ "" ] } ], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "wazona = f(4, 1)\n", "wazona(1, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 86, "text": [ "6" ] } ], "prompt_number": 86 }, { "cell_type": "code", "collapsed": false, "input": [ "wazona(1, 3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 87, "text": [ "7" ] } ], "prompt_number": 87 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [-3, 2, 1, 4]\n", "arr.sort()\n", "arr" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 88, "text": [ "[-3, 1, 2, 4]" ] } ], "prompt_number": 88 }, { "cell_type": "code", "collapsed": false, "input": [ "arr.sort(reverse=True)\n", "arr" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 89, "text": [ "[4, 2, 1, -3]" ] } ], "prompt_number": 89 }, { "cell_type": "code", "collapsed": false, "input": [ "def mykey(x):\n", " return abs(x)\n", "arr.sort(key=mykey)\n", "arr" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 90, "text": [ "[1, 2, -3, 4]" ] } ], "prompt_number": 90 }, { "cell_type": "code", "collapsed": false, "input": [ "arr.sort(key=lambda x: abs(x))\n", "arr" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 91, "text": [ "[1, 2, -3, 4]" ] } ], "prompt_number": 91 }, { "cell_type": "code", "collapsed": false, "input": [ "f = lambda a, b: a - b\n", "print(f(1,2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-1\n" ] } ], "prompt_number": 92 }, { "cell_type": "code", "collapsed": false, "input": [ "g = lambda a, b: a + b\n", "print(g(1,2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 93 }, { "cell_type": "code", "collapsed": false, "input": [ "f, g = g, f\n", "print(f(1,2))\n", "print(g(1,2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n", "-1\n" ] } ], "prompt_number": 96 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Various" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Complex numbers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "1+3j" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 72, "text": [ "(1+3j)" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "abs(1+3j)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 73, "text": [ "3.1622776601683795" ] } ], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "(1+3j)*(2-1j)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 74, "text": [ "(5+5j)" ] } ], "prompt_number": 74 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Generators (yield)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def whatdoido():\n", " a, b = 0, 1\n", " while True:\n", " yield a\n", " a, b = b, a + b\n", " \n", "print(whatdoido())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 97 }, { "cell_type": "code", "collapsed": false, "input": [ "g = whatdoido()\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] } ], "prompt_number": 98 }, { "cell_type": "code", "collapsed": false, "input": [ "print(next(g))\n", "print(next(g))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "233\n", "377\n", "610\n" ] } ], "prompt_number": 103 }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(10):\n", " print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "987\n", "1597\n", "2584\n", "4181\n", "6765\n", "10946\n", "17711\n", "28657\n", "46368\n", "75025\n" ] } ], "prompt_number": 104 }, { "cell_type": "code", "collapsed": false, "input": [ "for v in whatdoido():\n", " if v > 100:\n", " break\n", " print(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n", "55\n", "89\n" ] } ], "prompt_number": 105 }, { "cell_type": "code", "collapsed": false, "input": [ "def combinations(iterable, r):\n", " # combinations('ABCD', 2) --> AB AC AD BC BD CD\n", " # combinations(range(4), 3) --> 012 013 023 123\n", " pool = tuple(iterable)\n", " n = len(pool)\n", " if r > n:\n", " return\n", " indices = range(r)\n", " yield tuple(pool[i] for i in indices)\n", " while True:\n", " for i in reversed(range(r)):\n", " if indices[i] != i + n - r:\n", " break\n", " else:\n", " return\n", " indices[i] += 1\n", " for j in range(i+1, r):\n", " indices[j] = indices[j-1] + 1\n", " yield tuple(pool[i] for i in indices)\n", " \n", "pairs = combinations('ABCD', 2)\n", "print(pairs)\n", "for pair in pairs:\n", " print(pair)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "('A', 'B')\n", "('A', 'C')\n", "('A', 'D')\n", "('B', 'C')\n", "('B', 'D')\n", "('C', 'D')\n" ] } ], "prompt_number": 106 }, { "cell_type": "code", "collapsed": false, "input": [ "# Pierwsze trzy pary, kt\u00f3re maj\u0105 A i G w \u015brodku\n", "pairs = combinations('ABCDEFGH', 3)\n", "found = 0\n", "for pair in pairs:\n", " if 'A' in pair and 'G' in pair and found < 3:\n", " found += 1\n", " print(pair)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Build-in functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=14\n", "eval('max([x,2,-2,3])')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 107, "text": [ "14" ] } ], "prompt_number": 107 }, { "cell_type": "code", "collapsed": false, "input": [ "exec('import math; print(math.sin(2*math.pi))')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-2.44929359829e-16\n" ] } ], "prompt_number": 108 }, { "cell_type": "code", "collapsed": false, "input": [ "print(all([True, False, True]))\n", "print(any([True, False, True]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "False\n", "True\n" ] } ], "prompt_number": 109 }, { "cell_type": "code", "collapsed": false, "input": [ "print(sum([1,2,3]))\n", "print(min([1,2,3]))\n", "print(max([1,2,3]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n", "1\n", "3\n" ] } ], "prompt_number": 110 }, { "cell_type": "code", "collapsed": false, "input": [ "print(abs(-12))\n", "print(round(3.14159, 3))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12\n", "3.142\n" ] } ], "prompt_number": 111 }, { "cell_type": "code", "collapsed": false, "input": [ "print(float(12))\n", "print(int(12.7))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12.0\n", "12\n" ] } ], "prompt_number": 112 }, { "cell_type": "code", "collapsed": false, "input": [ "print(str(123))\n", "print(str([1,2,3]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "123\n", "[1, 2, 3]\n" ] } ], "prompt_number": 113 }, { "cell_type": "code", "collapsed": false, "input": [ "print(ord('a'))\n", "print(chr(98))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "97\n", "b\n" ] } ], "prompt_number": 114 }, { "cell_type": "code", "collapsed": false, "input": [ "print(type('ala ma kota'))\n", "print(type([1,2,3]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n" ] } ], "prompt_number": 115 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Global" ] }, { "cell_type": "code", "collapsed": false, "input": [ "text = 'ala ma kota'\n", "\n", "def a():\n", " print('a: ' + text) \n", "def b():\n", " print('b: ' + text)\n", " text = 'ala ma psa' " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 116 }, { "cell_type": "code", "collapsed": false, "input": [ "a()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a: ala ma kota\n" ] } ], "prompt_number": 117 }, { "cell_type": "code", "collapsed": false, "input": [ "b()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "UnboundLocalError", "evalue": "local variable 'text' referenced before assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mb\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mb\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a: '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mtext\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'b: '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mtext\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mtext\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'ala ma psa'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'text' referenced before assignment" ] } ], "prompt_number": 118 }, { "cell_type": "code", "collapsed": false, "input": [ "text = 'ala ma kota'\n", "def c():\n", " global text\n", " print('c: ' + text)\n", " text = 'ala ma psa'\n", "c()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "c: ala ma kota\n" ] } ], "prompt_number": 119 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Comprehensions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,1,2,3,4,5]\n", "new = []\n", "for a in arr:\n", " new.append('A' + str(a))\n", "new" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 120, "text": [ "['A0', 'A1', 'A2', 'A3', 'A4', 'A5']" ] } ], "prompt_number": 120 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,1,2,3,4,5]\n", "new = ['A' + str(a) for a in arr]\n", "new" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 121, "text": [ "['A0', 'A1', 'A2', 'A3', 'A4', 'A5']" ] } ], "prompt_number": 121 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,1,2,3,4,5]\n", "new = ['A' + str(a) for a in arr if a % 2 == 0]\n", "new" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 123, "text": [ "[0, 2, 4]" ] } ], "prompt_number": 123 }, { "cell_type": "code", "collapsed": false, "input": [ "arr = [0,13,2,4,11]\n", "sum([1 for a in arr if a % 2 == 0])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 125, "text": [ "3" ] } ], "prompt_number": 125 }, { "cell_type": "code", "collapsed": false, "input": [ "# Chcemy wszystkie pierwsze litery wyraz\u00f3w zamieni\u0107 na wielkie\n", "text = 'to jest kr\u00f3tki text nie na temat'\n", "splitted = text.split()\n", "splitted" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 126, "text": [ "['to', 'jest', 'kr\\xc3\\xb3tki', 'text', 'nie', 'na', 'temat']" ] } ], "prompt_number": 126 }, { "cell_type": "code", "collapsed": false, "input": [ "big = [x[0].upper() + x[1:] for x in splitted]\n", "big" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 127, "text": [ "['To', 'Jest', 'Kr\\xc3\\xb3tki', 'Text', 'Nie', 'Na', 'Temat']" ] } ], "prompt_number": 127 }, { "cell_type": "code", "collapsed": false, "input": [ "\" \".join(big)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 131, "text": [ "'To Jest Kr\\xc3\\xb3tki Text Nie Na Temat'" ] } ], "prompt_number": 131 }, { "cell_type": "code", "collapsed": false, "input": [ "text = 'to jest kr\u00f3tki text nie na temat'\n", "print(\" \".join([x[0].upper() + x[1:] for x in text.split()]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "To Jest Kr\u00f3tki Text Nie Na Temat\n" ] } ], "prompt_number": 129 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Dictionaries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "przedmioty = ('matematyka', 'fizyka', 'chemia')\n", "slownik = {p: len(p) for p in przedmioty}\n", "slownik" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 132, "text": [ "{'chemia': 6, 'fizyka': 6, 'matematyka': 10}" ] } ], "prompt_number": 132 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Classes" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Circle:\n", " def __init__(self, radius):\n", " self.radius = radius \n", " \n", " def area(self):\n", " from math import pi\n", " return pi * self.radius**2\n", " \n", " def set_radius(self, radius):\n", " self.radius = radius \n", " \n", "c = Circle(3)\n", "print(c.area())\n", "c.set_radius(5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "28.2743338823\n" ] } ], "prompt_number": 133 }, { "cell_type": "code", "collapsed": false, "input": [ "print(c.radius)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] } ], "prompt_number": 134 }, { "cell_type": "code", "collapsed": false, "input": [ "class Container:\n", " pass\n", "\n", "c = Container()\n", "c.size = 12\n", "c.value = 10000\n", "\n", "def compute(container):\n", " return container.size ** 2\n", "\n", "compute(c)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 135, "text": [ "144" ] } ], "prompt_number": 135 }, { "cell_type": "code", "collapsed": false, "input": [ "class Przedmiot:\n", " acoto = 12\n", " def __init__(self, nazwa):\n", " self.nazwa = nazwa\n", " \n", "p = Przedmiot('kck')\n", "p.nazwa" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(p.acoto)\n", "print(Przedmiot.acoto)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(p)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "class Przedmiot: \n", " def __init__(self, nazwa):\n", " self.nazwa = nazwa\n", " \n", " def __repr__(self):\n", " return 'Przedmiot o nazwie ' + self.nazwa\n", " \n", "p = Przedmiot('KCK')\n", "print(p)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Duck typing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Circle:\n", " def __init__(self, radius):\n", " self.radius = radius \n", " def area(self):\n", " from math import pi\n", " return pi * self.radius**2\n", "\n", "class Square:\n", " def __init__(self, w):\n", " self.w = w\n", " def area(self):\n", " return self.w**2\n", " \n", "def get_shape():\n", " from random import choice\n", " return choice([Square(4), Circle(4)])\n", " \n", "shape = get_shape()\n", "shape.area()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 139, "text": [ "16" ] } ], "prompt_number": 139 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Files" ] }, { "cell_type": "code", "collapsed": false, "input": [ "f = open('test.txt','w')\n", "for i in range(6):\n", " f.write('To jest linia {}\\n'.format(i))\n", "f.close()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 140 }, { "cell_type": "code", "collapsed": false, "input": [ "cat test.txt" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "To jest linia 0\r\n", "To jest linia 1\r\n", "To jest linia 2\r\n", "To jest linia 3\r\n", "To jest linia 4\r\n", "To jest linia 5\r\n" ] } ], "prompt_number": 141 }, { "cell_type": "code", "collapsed": false, "input": [ "f = open('test.txt')\n", "for line in f:\n", " print(line.strip())\n", "f.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "To jest linia 0\n", "To jest linia 1\n", "To jest linia 2\n", "To jest linia 3\n", "To jest linia 4\n", "To jest linia 5\n" ] } ], "prompt_number": 142 }, { "cell_type": "code", "collapsed": false, "input": [ "with open('test.txt') as f:\n", " for line in f:\n", " print(line.strip())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "To jest linia 0\n", "To jest linia 1\n", "To jest linia 2\n", "To jest linia 3\n", "To jest linia 4\n", "To jest linia 5\n" ] } ], "prompt_number": 143 }, { "cell_type": "code", "collapsed": false, "input": [ "with open('test.txt') as f:\n", " lines = f.readlines()\n", "lines[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 144, "text": [ "'To jest linia 0\\n'" ] } ], "prompt_number": 144 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }