title: Beginner's Workshop 3
Author:
Alexander Loechel
event: PyLadies Munich - Beginner's Workshop
keywords: Python, PyLadies, Workshop
data-transition-duration: 1500
css-all: css/workshop3.css
auto-console: Yes
id: title
class: slide title-slide centered level-1
data-x: 0
data-y: 0
Welcome to
Beginner's workshop 3
Theory of data flow and data control
id: recap-ws1
class: slide level-1
data-x: r-3500
data-y: 1000
Introduction
What is Python
About the Language
History and Background
Basic Philosophy - The Zen of Python
Tools and Setup
Python Interpreter
Virtual Environments
pip & easy_install
Hello World example
id: syslab
class: slide level-1
data-x: r+0
data-y: r+1000
Syllabus of the planned curriculum
Workshop 1 - Introduction, Setup and first steps (February 26th 2015)
Workshop 2 - Theory of data types and data structures
For beginners, March 12th 2015)
Workshop 3 - Theory of data flow / data contol
(For beginners, April 1nd 2015)
Workshop 4 - Repeating Workshop
(For beginners and language changer, April 16th 2015)
Workshop 5 and ongoing:
More specialized on your preferred direction:
web-development and data
up to 8-12 Workshops
id: recap-ws2-1
class: slide level-1
data-x: r+1000
data-y: 1000
Theory of data types and data structures
id: recap-ws2-2
class: slide level-1
data-x: r+0
data-y: r+1000
Theory of data types and data structures
id: recap-ws2-3
class: slide level-1
data-x: r+0
data-y: r+1000
Theory of data types and data structures
Standard type Hierarchy and type()
-Function
Mutable and Immutable Types
Numbers
Integrals (Booleans & Integers ) - Boolean Operations
Real (floating point )
Complex
id: recap-ws2-4
class: slide level-1
data-x: r+0
data-y: r+1000
Theory of data types and data structures
Sequences, Sets and Mappings
Tuples ((value1, value2, ...)
)
Strings (Unicode (Python2 u''
) & Byte (Python3 b''
) )
Lists ([]
)
Set & Frozenset
Dict ({ 'key': 'value', ... }
)
Callable types
Classes
functions and methods
Modules
id: intro-python
class: slide level-1
data-x: r+1000
data-y: 1000
1 # Python 3: Fibonacci series up to n
2 def fib (n ):
3 a , b = 0 , 1
4 while a < n :
5 print (a , end = ' ' )
6 a , b = b , a + b
7 print ()
8 fib (1000 )
method definition def fib(n)
variables a, b
discrete values 0, 1, 1000
data flow / control element for a loop: while
method calls print()
& fib()
id: base-1
class: slide level-1
data-x: r+0
data-y: r+1000
Basic Elements of data flow / data control
Simple statements
Expression statements
True
, 'text'
,
boolean / comparison operations a == b
List comprehension [x.id for x in my_list]
objects or procedures
Assignment statements (x = y
)
Augmented Assignment Statements (text += 'some text'
)
special simple statements with a meaning in context (pass
, return
, yield
,
raise
, break
, continue
, import
)
id: base-2
class: slide level-1
data-x: r+0
data-y: r+1000
Basic Elements of data flow / data control
Compound statements
Control flow statements
conditional switches (if ... elif ... else
)
loops (while
& for ... in ...:
)
important here: iterators & generators
Exception handling (try: ... except: ... finally: ...
)
Context Managers (with
)
Definition statements (class
, def
) and calls call()
id: class-def
class: slide level-1
data-x: r+0
data-y: r+1000
Definition Statements and Calls
everything in Python is an Object
1 import modul
2
3 class MyClass (inheritance1 , inheritance2 ):
4
5 def __init__ (self , param ):
6 self ._param = param
7
8 def get_param (self ):
9 return self ._param
10
11 def set_param (self , param ):
12 pass
13
14 my_obj = MyClass ('value' )
15 print (my_obj .get_param ())
id: ifthen
class: slide level-1
data-x: r+1000
data-y: 1000
Classic if then else
if a == 0 :
expression
elif a > 0 and a < 10 :
expression
elif a < 0 :
expression
else :
expression
Truth evaluation of values and Operations (Boolean, Comparison, Shifting, ... Operations)
Test via bool(expression)
id: bools1
class: slide level-1
data-x: r+0
data-y: r+1000
>> > True and True
True
>> > True or False
True
>> > not False
True
id: bools2
class: slide level-1
data-x: r+0
data-y: r+1000
>> > 1 > 0
True
>> > ' text ' .strip () == 'text'
True
>> > result = None
>> > result is not None
False
>> > 11 in range (5 )
False
id: bools3
class: slide level-1
data-x: r+0
data-y: r+1000
Truth Value of expressions
>> > # An empty sequence has a Truth-Value of False
>> > # A sequence with content has a Truth-Value of True
>> > my_list = []
>> > if not my_list :
>> > print ('List is empty' )
'List is empty'
>> > # NoneType has a Truth-Value of False
>> > return_value = function_call ()
>> > # if return_value is None
>> > if return_value :
>> > return_value .start ()
id: loops
class: slide level-1
data-x: r+1000
data-y: 1000
for index in list :
print (index )
index = 1
while index <= 100 :
print (index )
index += 1
id: aloops1
class: slide level-1
data-x: r+0
data-y: r+1000
>> > for fruit in ['banana' , 'apple' , 'orange' ]:
>> > print (fruit )
>> > else :
>> > print ('no more fruits available.' )
banana
apple
orange
no more fruits available .
id: aloops2
class: slide level-1
data-x: r+0
data-y: r+1000
Advanced for Loop & Context Manager
1 with open (file , 'r' ) as input_file :
2 line_number = 0
3 for line in input_file .readlines ():
4 line_number += 1
5 if line == 'Appendix' :
6 break
7 elif line .startswith ('#' ):
8 # Line starts with comment sign: ignore and continue
9 continue
10 print (line )
11 else :
12 print ('No Appendix found' )
13 print ('We have read {lines}' .format (lines = line_number ))
with
indicates a Context Manager - means that specific operations are automatically applied on exit of context
here the file is closed
break
quites the loop before the normal ending condition of the loop is reached
continue
indicates that all following expressions in the loop is not executed and next loop circle starts
else
is only executed if no break statement has quite the loop
id: iter-gen
class: slide level-1
data-x: r+1000
data-y: 1000
you can only loop (for ... in ... :
) over a sequence-like object
sequences implements a __next__() method
everything that behaves like a sequence and implements a __next__() method is iterable
id: gen
class: slide level-1
data-x: r+0
data-y: r+1000
1 import time
2
3 def my_takt_generator ():
4 num = 1
5 while True :
6 yield num
7 num = num % 4 + 1
8
9 takt = my_takt_generator ()
10
11 counter = 0
12 for takt_elem in takt :
13 print (takt_elem )
14 counter += 1
15 if counter >= 60 :
16 break
17 time .sleep (1 )
counts 1 2 3 4 1 2 3 4 ...
id: async
class: slide level-1
data-x: r+0
data-y: r+1000
1 class DistanceSensor (Sensor ):
2
3 @asyncio .coroutine
4 def get_distance (self ):
5 while true :
6 distance = yield from self ._messure_distance ()
7 if distance < 100 :
8 print ('Warning Distance too low' )
id: exceptions
class: slide level-1
data-x: r+1000
data-y: 1000
Exceptions & Exception Handling
1 def div (value1 , value2 ):
2 if value2 == 0 :
3 raise ZeroDivisionError
4 return value1 / value2
5
6 try :
7 value1 , value2 = 10 , 0
8 print (div (value1 , value2 ))
9 except Error as e :
10 print ('something went totally wrong' )
11 print (e .message )
id: try
class: slide level-1
data-x: r+1000
data-y: 1000
All following explanations and examples are practical session
please open your IPython Notebook http://localhost:8888/
id: next-meeting
class: slide centered level-1
data-x: 0
data-y: 5000
Thursday April 23th 2015 18:30 ???
Repeating Workshop - Exercises
id: overview
data-x: 0
data-y: 2500
data-scale: 8