Python Kontrollstrukturen

Kontrollstrukturen bringen Dynamik ins Programm!

  • if

    if true:
       # this code

    if a==2 and b ==3: 
       # this code             => bei true
    elif a==2 and b !=3: 
       # that code             => bei false
    else:
       # the other code     => bei allen anderen Konstellationen


       statt:   _x=3
                 if _x ==3:
                    b = 4
                 else:
                    b = 6
       verkürzt:
                 _x = 3
                 _b = 4 if a == 3 else 6

  • while

    _x = 1
    while _x < 5:
        print(_x)
        _x += 1
    else:
    print("sis is se end")

    Mit "break" kann man eine for-Schleife zwingend verlassen.

  • for

    for _x in range(start, end, increment):      Achtung: Bedingung ist < end
         print(_x)


    _fruits = ["apple","pineapple","citrus","strawberry","raspberry","orange"]
    for _fruit in _fruits:
       print(_fruit + "juice")

    for _x in "citrus":
       print(_x)

    c
    i
    t
    r
    u
    s


    Mit "pass" kann man eine for-Schleife zwingend verlassen.

  •    

back to python