# 5 kleine Beispiele von https://www.python.org/
#
# leicht angepasst und ergänzt
#
# 13.11.2018

# -----------------------------------

# print() kann im interaktiven Modus entfallen

# Python 3: Simple arithmetic
print(1 / 2)
# 0.5

print(2 ** 3)
# 8

print(17 / 3)  # classic division returns a float
# 5.666666666666667

print(17 // 3) # floor division
# 5

# -----------------------------------

# Python 3: Simple output (with Unicode)
print("Hello, I'm Python!")
# Hello, I'm Python!

# Input, assignment
name = input('What is your name?\n')
print('Hi, %s.' % name)
# What is your name?
# Python
# Hi, Python.

# mit format() statt %
print('Hi, {}.'.format(name))

# mit f-string (verfügbar ab Python 3.6)
print(f'Hi, {name}.')
print(f'Hi, {name[:2].upper()}.')  # Nutzung eines Python-Ausdrucks
# Hi, PY.

# -----------------------------------

# For loop on a list
numbers = [2, 4, 6, 8]
product = 1
for number in numbers:
    # product = product * number
    product *= number
print('The product is:', product)
# The product is: 384

# -----------------------------------

# Python 3: List comprehensions
fruits = ['Banana', 'Apple', 'Lime']
loud_fruits = [fruit.upper() for fruit in fruits]
print(loud_fruits)
# ['BANANA', 'APPLE', 'LIME']

# List and the enumerate function
list(enumerate(fruits))
# [(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]

# alternativ mit Generatorausdruck statt List comprehension
loud_fruits = (fruit.upper() for fruit in fruits)
print(list(loud_fruits))

# -----------------------------------

# Python 3: Fibonacci series up to n

# https://de.wikipedia.org/wiki/Fibonacci-Folge
#
# Die Fibonacci-Folge ist die unendliche Folge von natürlichen Zahlen, die
# (ursprünglich) mit zweimal der Zahl 1 beginnt oder (häufig, in moderner
# Schreibweise) zusätzlich mit einer führenden Zahl 0 versehen ist.[1] Im
# Anschluss ergibt jeweils die Summe zweier aufeinanderfolgender Zahlen die
# unmittelbar danach folgende Zahl:
#
# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a + b
        # statt tuple packing und sequence unpacking könnte man eine
        # Hilfsvariable c nutzen:
        # c = a + b ; a = b ; b = c
    print()

fib(1000)
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

# Variante 2 mit einer Liste aller Fibonacci-Zahlen als Ergebnis
def fib_list(n):
    a, b = 0, 1
    fib_numbers = [] # Start mit leerer Liste
    while a < n:
        fib_numbers.append(a) # a an Liste anhängen
        a, b = b, a + b
    return fib_numbers

# Ausgabe als Python-Liste
print(fib_list(1000))
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
