Python    up http://www.python.org
 
 


 gcd   Michael Neumann
#
# Größter Gemeinsamer Teiler
# 

def ggt(a, b):
   if a < b: a,b = b,a
   while a%b != 0:
      a,b = b,a%b
   return b
Greatest common divisor


 Hello World   Michael Neumann
# Hello World in Python

print "Hello World"
Prints "Hello World" onto the screen.


 OO - Shape, Circle, Rectangle   Michael Neumann
from math import pi

class Color:
  def __init__(self, red=0, green=0, blue=0):
    self.red   = red
    self.green = green
    self.blue  = blue

# no abstract classes in Python!
class Shape:
  def __init__(self, x, y, color):
    self.x     = x
    self.y     = y
    self.color = color

  def draw(self):
    raise "abstract method"

  def getArea(self):
    raise "abstract method"

  def move(self, x, y):
    self.x += x
    self.y += y

class Circle(Shape):
  def __init__(self, x, y, radius, color):
    Shape.__init__(self, x, y, color)
    self.radius = radius

  def draw(self):
    pass # do something here

  def getArea(self):
    return int(pi * self.radius)

class Rectangle(Shape):
  def __init__(self, x, y, width, height, color):
    Shape.__init__(self, x, y, color)
    self.width  = width
    self.height = height

  def draw(self):
    pass # do something here

  def getArea(self):
    return self.width * self.height


## Test the Classes ##

BLACK  = Color()
WHITE  = Color(255, 255, 255)

circle = Circle(100, 100, 50, BLACK)
rect   = Rectangle(20, 20, 5, 5, WHITE)

circle.move(10, 10)

circle.x = 50
circle.radius = 5

print circle.getArea()     # => 15
print rect.getArea()       # => 25

print rect.width           # => 5
rect.width = 10
print rect.width           # => 10 

circle.draw()
rect.draw()



 QuickSort   Michael Neumann
#
# QuickSort in Python
# von Michael Neumann
# 

def quicksort(arr):
   if len(arr) <= 1: return arr
   m = arr[0]
   return quicksort(filter(lambda i,j=m: i<j, arr)) + \
          filter(lambda i,j=m: i==j, arr) + \
          quicksort(filter(lambda i,j=m: i>j, arr))



# Aufruf
print quicksort([5,99,2,45,12,234,29,0])
QuickSort


 Squares (1)   Michael Neumann
arr = range(1,11)

for i in arr: print i*i,
Outputs the squares from 1 to 10.


 Squares (2)   Michael Neumann
for i in map(lambda a: a*a,range(1,11)): print i,
Outputs the squares from 1 to 10.