Basics

Jupyter Notebook

Das Notebook tut aus Markdown HTML machen, und aus Python Code auch

[1]:
print('hello world')

Syntax

[2]:
for c in 'hello':
    print(c)

Batteries

[3]:
import random
random.randrange(3, 7)

Tuple Unpacking

[4]:
a, b = 1, 2
[5]:
a
[5]:
1
[6]:
b
[6]:
2
[7]:
a, b = b, a
[8]:
a
[8]:
2
[9]:
b
[9]:
1

Ist das gleiche wie …

[10]:
(a, b) = (b, a)

for

[11]:
liste = [1, 'eins', 1.0]
for element in liste:
    print(element)
1
eins
1.0

In C: for (i=0; i<len(liste); i++) ...

Key Value Map

[12]:
lookup_table = {
    'eins': 1,
    'zwei': 2,
    'drei': 3,
    # ...
}
[13]:
lookup_table['eins']
[13]:
1
[14]:
lookup_table['vier'] = 4
[15]:
lookup_table['vier']
[15]:
4
[16]:
try:
    lookup_table['fuenf']
except Exception as e:
    print(e)
'fuenf'

Syntax

Comments vs Documentation

[17]:
# das ist ein kommentar
[18]:
a = 1  # das ist auch ein kommentar

Docstrings

[19]:
def do_something(some_number):
    "Das ist die Dokumentation von dieser Funktion do_something. Sie macht genau nichts, ausser deppert auf stdout auszuspucken"
    print('doing something with that number:', some_number)
[20]:
do_something(42)
doing something with that number: 42
[21]:
do_something.__doc__
[21]:
'Das ist die Dokumentation von dieser Funktion do_something. Sie macht genau nichts, ausser deppert auf stdout auszuspucken'
[22]:
help(do_something)
Help on function do_something in module __main__:

do_something(some_number)
    Das ist die Dokumentation von dieser Funktion do_something. Sie macht genau nichts, ausser deppert auf stdout auszuspucken

Multiline String (btw.)

[25]:
def do_something(some_number):
    """Das ist die Dokumentation von dieser Funktion do_something.
    Sie macht genau nichts, ausser deppert auf stdout auszuspucken"""
    print('doing something with that number:', some_number)
[26]:
do_something.__doc__
[26]:
'Das ist die Dokumentation von dieser Funktion do_something.\n    Sie macht genau nichts, ausser deppert auf stdout auszuspucken'
[27]:
help(do_something)
Help on function do_something in module __main__:

do_something(some_number)
    Das ist die Dokumentation von dieser Funktion do_something.
    Sie macht genau nichts, ausser deppert auf stdout auszuspucken

Variables

[28]:
a = 42
a
[28]:
42
[29]:
type(a)
[29]:
int
[30]:
a = 'hallo'
[31]:
type(a)
[31]:
str
[32]:
a = 42.666
[33]:
type(a)
[33]:
float
[34]:
a = ['hallo', 666, 1.234]
[35]:
type(a)
[35]:
list
[36]:
a
[36]:
['hallo', 666, 1.234]
[37]:
del a
[38]:
try:
    a
except Exception as e:
    print(e)
name 'a' is not defined
[39]:
a = 666

globals() is a function that returns a dictionary containing all global variables (nur so nebenbei)

[40]:
globals()['a']
[40]:
666
[41]:
globals()['eine_neue_variable'] = ['hallo', 'welt', 42]
[42]:
eine_neue_variable
[42]:
['hallo', 'welt', 42]

Tuple Unpacking

[43]:
a = 1
b = 2

Das geht auch anders:

[44]:
a, b = 1, 2

Tuple?

[45]:
(a, b) = (1, 2)
[46]:
a, b
[46]:
(1, 2)

Vertauschen von Variablen: klassisch

[47]:
c = a
a = b
b = c
a, b
[47]:
(2, 1)
[48]:
a, b = 1, 2
[49]:
a, b = b, a
a, b
[49]:
(2, 1)

Assignment Details

[50]:
a = 42
id(a)
[50]:
140705292246608
[51]:
b = a
[52]:
id(b)
[52]:
140705292246608

Datatypes

Numbers

Integer Numbers (Literals)

[53]:
1234
[53]:
1234
[54]:
-1234
[54]:
-1234
[55]:
permission = 0o755
[56]:
register_content = 0xdeadbeef
register_content
[56]:
3735928559
[57]:
register_content = 0b11010001110101000
register_content
[57]:
107432

Arithmetic Operators

[58]:
6/4
[58]:
1.5

Python 2: 6/4 == 1. Erzwungen in Python 3: Floor Division

[59]:
6//4
[59]:
1

Modulo: Rest einer Division

[60]:
6%4
[60]:
2
[61]:
7%3
[61]:
1
[62]:
6%3
[62]:
0
[63]:
i = 6
i = i+7
i
[63]:
13
[64]:
i = 6
i += 7
i
[64]:
13

Wertebereich von Integers

[65]:
10**6
[65]:
1000000
[66]:
10**10
[66]:
10000000000
[67]:
10**100
[67]:
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
[68]:
hex(10**100)
[68]:
'0x1249ad2594c37ceb0b2784c4ce0bf38ace408e211a7caab24308a82e8f10000000000000000000000000'
[69]:
10**1000
[69]:
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Die groesste Zahl, die auf 64 Bit darstellbar ist

[70]:
i = 2**64-1
i
[70]:
18446744073709551615
[71]:
i += 1
[72]:
i
[72]:
18446744073709551616

Strings

[73]:
s = 'abc'
[74]:
s = "abc"
[75]:
s = 'abc\'def'
[76]:
s = "abc'def"
[77]:
s = 'abc"def'
[78]:
s = """erste zeile
zweite zeile"""
s
[78]:
'erste zeile\nzweite zeile'
[79]:
dos_path = 'C:\Programme\neues programm'
dos_path
[79]:
'C:\\Programme\neues programm'
[80]:
print(dos_path)
C:\Programme
eues programm

Raw Strings

[81]:
dos_path = r'C:\Programme\neues programm'
print(dos_path)
C:\Programme\neues programm

Datatype Conversions

[82]:
ein_string_der_aussieht_wie_ein_integer = '42'
type(ein_string_der_aussieht_wie_ein_integer)
[82]:
str
[83]:
ein_integer = int(ein_string_der_aussieht_wie_ein_integer)
ein_integer
[83]:
42
[84]:
type(ein_integer)
[84]:
int
[85]:
int(1.234)
[85]:
1

Zur Basis 16 …

[86]:
int('32', 16)
[86]:
50
[87]:
str(42)
[87]:
'42'
[88]:
str(24.234)
[88]:
'24.234'

Complex Datatypes

Listen

[89]:
l = []  # empty list
[90]:
type(l)
[90]:
list
[91]:
l = list()
l
[91]:
[]
[92]:
l = ['hallo', 42, 1.5]
l
[92]:
['hallo', 42, 1.5]
[93]:
len(l)
[93]:
3
[94]:
l.append(True)
l
[94]:
['hallo', 42, 1.5, True]
[95]:
l.extend([1, 2, 3])
l
[95]:
['hallo', 42, 1.5, True, 1, 2, 3]
[96]:
l += ['vier', 5]
l
[96]:
['hallo', 42, 1.5, True, 1, 2, 3, 'vier', 5]

Tuple? Wie Liste, nur immutable

[97]:
t = ('eins', 2, 3.0)
t
[97]:
('eins', 2, 3.0)
[98]:
type(t)
[98]:
tuple

Immutable: es gibt kein append() (und andere verändernde Methoden)

[99]:
try:
    t.append(4)
except Exception as e:
    print(e)
'tuple' object has no attribute 'append'

Dictionaries

[100]:
d = {}
type(d)
[100]:
dict
[101]:
d
[101]:
{}
[102]:
len(d)
[102]:
0
[103]:
d = dict()
d
[103]:
{}
[104]:
d = {
    'eins': 1,
    'zwei': 2,
}
[105]:
len(d)
[105]:
2
[106]:
d['eins']
[106]:
1
[107]:
try:
    d['drei']    # KeyError: no such key
except KeyError as e:
    print(type(e), e)
<class 'KeyError'> 'drei'
[108]:
value = d.get('drei')
print(value)
None
[109]:
if d.get('drei') is None:
    print('nix drin')
nix drin
[110]:
d['drei'] = 3
d['drei']
[110]:
3
[111]:
'drei' in d
[111]:
True
[112]:
del d['drei']   # delete key 'drei'
[ ]: