Getting Started =============== There are many different ways to use Python. For now we will use an iPython notebook. Create a directory of your choice (where you want to locate your python files). Open a command shell in this directory and start the iPython notebook server by typing \`ipython notebook'. This should open your web browser or at least show you the URL that can be opened by the web browser of your choice. Usually http://localhost:8888 In your browser window you can now open or create notebook files (.ipynb) in your directory. Once you opened a notebook you can type 'h' to get help on some useful shortcuts. This text is a static version of such a notebook. You can download it (see start page of this documentation) or type it on your own. Using Python as a Calculator ---------------------------- .. code:: python 2+2 .. parsed-literal:: 4 .. code:: python 50-5*6 .. parsed-literal:: 20 .. code:: python (50-5*6) / 4 .. parsed-literal:: 5.0 .. code:: python 8/5 .. parsed-literal:: 1.6 .. code:: python 8//5 .. parsed-literal:: 1 .. code:: python 8%5 .. parsed-literal:: 3 Variables and Types ------------------- Naming ~~~~~~ Do not use reserved names: and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield Use a-z, A-Z, 0-9, \_ Start with a letter .. code:: python myvar = 12 .. code:: python myvar .. parsed-literal:: 12 .. code:: python myvar + 10 .. parsed-literal:: 22 .. code:: python myvar = myvar + 10 .. code:: python myvar .. parsed-literal:: 22 .. code:: python type(myvar) .. parsed-literal:: int .. code:: python myvar = 22.0 .. code:: python type(myvar) .. parsed-literal:: float .. code:: python mybool = True .. code:: python type( mybool) .. parsed-literal:: bool .. code:: python x = 1.0 + 1.0j .. code:: python type( x) .. parsed-literal:: complex .. code:: python x.real .. parsed-literal:: 1.0 .. code:: python myvar**0.5 .. parsed-literal:: 4.69041575982343 type casting ~~~~~~~~~~~~ .. code:: python type(myvar) .. parsed-literal:: float .. code:: python myvar=22.9 .. code:: python int(myvar) .. parsed-literal:: 22 Comparision ~~~~~~~~~~~ .. code:: python 1 > 0 .. parsed-literal:: True .. code:: python type(0 > 1) .. parsed-literal:: bool .. code:: python 2 >= 2 .. parsed-literal:: True .. code:: python not myvar != 1 .. parsed-literal:: False .. code:: python (1==1) | (1==2) .. parsed-literal:: True Composite types: Strings, List, Tuple, Set and Dictionaries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Strings ^^^^^^^ .. code:: python s = "Hello Everybody" .. code:: python type(s) .. parsed-literal:: str .. code:: python s .. parsed-literal:: 'Hello Everybody' .. code:: python len(s) .. parsed-literal:: 15 .. code:: python s.replace('Hello', 'Goodbye') .. parsed-literal:: 'Goodbye Everybody' .. code:: python s[0] .. parsed-literal:: 'H' .. code:: python s[6:] .. parsed-literal:: 'Everybody' .. code:: python s[0:1] .. parsed-literal:: 'H' .. code:: python s[:-3] .. parsed-literal:: 'Hello Everyb' .. code:: python s[0:10:2] .. parsed-literal:: 'HloEe' .. code:: python s[::-2] .. parsed-literal:: 'yoyeEolH' .. code:: python s2 = ' and Nobody' .. code:: python s + s2 .. parsed-literal:: 'Hello Everybody and Nobody' .. code:: python s[0:5] + s2[-7:] .. parsed-literal:: 'Hello Nobody' .. code:: python print( "The number {} is great".format(myvar)) .. parsed-literal:: The number 22.9 is great Lists ^^^^^ .. code:: python l = [1,2,3,4] .. code:: python l .. parsed-literal:: [1, 2, 3, 4] .. code:: python type(l) .. parsed-literal:: list .. code:: python l2 = list(range(10)) .. code:: python l2 .. parsed-literal:: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] .. code:: python l5 = list(range(-10,11,2)) .. code:: python l5 .. parsed-literal:: [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10] .. code:: python l3 = [1.1, 2, 'Hallo', 1+1j] .. code:: python l3 .. parsed-literal:: [1.1, 2, 'Hallo', (1+1j)] .. code:: python l3[1] .. parsed-literal:: 2 .. code:: python l4 = [1,2,3,[1,2,3,[5,6,7]]] .. code:: python l4 .. parsed-literal:: [1, 2, 3, [1, 2, 3, [5, 6, 7]]] .. code:: python l4[3][3] .. parsed-literal:: [5, 6, 7] .. code:: python l4[3][3][2] .. parsed-literal:: 7 .. code:: python ls = list(s) .. code:: python ls .. parsed-literal:: ['H', 'e', 'l', 'l', 'o', ' ', 'E', 'v', 'e', 'r', 'y', 'b', 'o', 'd', 'y'] .. code:: python ls.sort() .. code:: python ls .. parsed-literal:: [' ', 'E', 'H', 'b', 'd', 'e', 'e', 'l', 'l', 'o', 'o', 'r', 'v', 'y', 'y'] .. code:: python ls.append( 'X') .. code:: python l6 = [] .. code:: python l6 .. parsed-literal:: [] .. code:: python l6.append( 'H') .. code:: python l6.append('a') .. code:: python l6 .. parsed-literal:: ['H', 'a'] .. code:: python l6.insert( 0, 'X') .. code:: python l6 .. parsed-literal:: ['X', 'H', 'a'] .. code:: python l6.insert( 2, 'A') .. code:: python l6 .. parsed-literal:: ['X', 'H', 'A', 'a'] .. code:: python l6[0] = 'x' .. code:: python l6 .. parsed-literal:: ['x', 'H', 'A', 'a'] Tuple ^^^^^ similiar to list but it is impossible to modify .. code:: python t1 = (1,2,3,4) .. code:: python t1 .. parsed-literal:: (1, 2, 3, 4) .. code:: python # t1[0] = 3 Dictionaries ^^^^^^^^^^^^ .. code:: python d1 = {'par1': 5, 'par2': 10} .. code:: python d1 .. parsed-literal:: {'par1': 5, 'par2': 10} .. code:: python type(d1) .. parsed-literal:: dict .. code:: python d1['par1'] .. parsed-literal:: 5 .. code:: python d1['par1']=1 .. code:: python d1['par3']=22 .. code:: python d1 .. parsed-literal:: {'par1': 1, 'par2': 10, 'par3': 22} Set ^^^ .. code:: python basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} .. code:: python print(basket) .. parsed-literal:: {'banana', 'apple', 'pear', 'orange'} .. code:: python 'orange' in basket .. parsed-literal:: True .. code:: python 'lemon' in basket .. parsed-literal:: False .. code:: python a = set('abracadabra') b = set('alacazam') .. code:: python a, b .. parsed-literal:: ({'a', 'b', 'c', 'd', 'r'}, {'a', 'c', 'l', 'm', 'z'}) .. code:: python a-b .. parsed-literal:: {'b', 'd', 'r'} .. code:: python a & b .. parsed-literal:: {'a', 'c'} .. code:: python a | b .. parsed-literal:: {'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'}