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

2+2
4
50-5*6
20
(50-5*6) / 4
5.0
8/5
1.6
8//5
1
8%5
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

myvar = 12
myvar
12
myvar + 10
22
myvar = myvar + 10
myvar
22
type(myvar)
int
myvar = 22.0
type(myvar)
float
mybool = True
type( mybool)
bool
x = 1.0 + 1.0j
type( x)
complex
x.real
1.0
myvar**0.5
4.69041575982343

type casting

type(myvar)
float
myvar=22.9
int(myvar)
22

Comparision

1 > 0
True
type(0 > 1)
bool
2 >= 2
True
not myvar != 1
False
(1==1) | (1==2)
True

Composite types: Strings, List, Tuple, Set and Dictionaries

Strings

s = "Hello Everybody"
type(s)
str
s
'Hello Everybody'
len(s)
15
s.replace('Hello', 'Goodbye')
'Goodbye Everybody'
s[0]
'H'
s[6:]
'Everybody'
s[0:1]
'H'
s[:-3]
'Hello Everyb'
s[0:10:2]
'HloEe'
s[::-2]
'yoyeEolH'
s2 = ' and Nobody'
s + s2
'Hello Everybody and Nobody'
s[0:5] + s2[-7:]
'Hello Nobody'
print( "The number {} is great".format(myvar))
The number 22.9 is great

Lists

l = [1,2,3,4]
l
[1, 2, 3, 4]
type(l)
list
l2 = list(range(10))
l2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l5 = list(range(-10,11,2))
l5
[-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
l3 = [1.1, 2, 'Hallo', 1+1j]
l3
[1.1, 2, 'Hallo', (1+1j)]
l3[1]
2
l4 = [1,2,3,[1,2,3,[5,6,7]]]
l4
[1, 2, 3, [1, 2, 3, [5, 6, 7]]]
l4[3][3]
[5, 6, 7]
l4[3][3][2]
7
ls = list(s)
ls
['H', 'e', 'l', 'l', 'o', ' ', 'E', 'v', 'e', 'r', 'y', 'b', 'o', 'd', 'y']
ls.sort()
ls
[' ', 'E', 'H', 'b', 'd', 'e', 'e', 'l', 'l', 'o', 'o', 'r', 'v', 'y', 'y']
ls.append( 'X')
l6 = []
l6
[]
l6.append( 'H')
l6.append('a')
l6
['H', 'a']
l6.insert( 0, 'X')
l6
['X', 'H', 'a']
l6.insert( 2, 'A')
l6
['X', 'H', 'A', 'a']
l6[0] = 'x'
l6
['x', 'H', 'A', 'a']

Tuple

similiar to list but it is impossible to modify

t1 = (1,2,3,4)
t1
(1, 2, 3, 4)
# t1[0] = 3

Dictionaries

d1 = {'par1': 5, 'par2': 10}
d1
{'par1': 5, 'par2': 10}
type(d1)
dict
d1['par1']
5
d1['par1']=1
d1['par3']=22
d1
{'par1': 1, 'par2': 10, 'par3': 22}

Set

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)
{'banana', 'apple', 'pear', 'orange'}
'orange' in basket
True
'lemon' in basket
False
a = set('abracadabra')
b = set('alacazam')
a, b
({'a', 'b', 'c', 'd', 'r'}, {'a', 'c', 'l', 'm', 'z'})
a-b
{'b', 'd', 'r'}
a & b
{'a', 'c'}
a | b
{'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'}