Python Control Flow

  1. if/then/else
  2. for-loop/else
  3. while-loop/else
  4. functions
  5. class (?)

1. if/then/else

Note there is no "else if" or need to indent this, python uses "elif". Again, note the indentation controls the flow.


In [ ]:
a = 1.0
if a == 0.0:
    print('zero')
elif a > 10.0 or a < -10:
    print("too big")
else:
    print("close enough")

Things to try :

  • seting a=0.001 0.00001 etc. or try 1e-10 1e-50 1e-100 (when is it really zero in python?)
  • a = 100
  • a = -5

2. for-loop

The for-loop in python runs over an iterator, for example a python list is the most common one to use.

In python one is also allowed to add an else clause, often overlooked!


In [ ]:
for i in [1,3,-1,10,100,0]:
#for i in range(1,10,2):
    if i<0: 
        continue
    if i>10:
        print("break")
        break
    if i<3:
        pass
        print("pass")
    print(i)
else:
    print("only if there is no break")

Things to try:

  • i>100 and notice the differences

The above mentioned for-loop can be compacted in python (not recommmended, as your readability goes down the drain):


In [ ]:
for i in [1,3,-1,10,100,0]:
    if i<0:  continue
    if i>10: print("break"); break
    if i<3:  pass; print("pass")
    print(i)
else:
    print("only if there is no break")

3. while-loop

The python while loop needs a termination statement. As the for-loop, it also has the else clause.


In [ ]:
a = 0
sum = 0
while a<10:
    a += 1
    sum += a
    print(a,sum)
    if sum>100:
        break
else:
    print("final sum",sum)

Things to try:

  • sum>10

4. functions

Functions are the classic analog of functions in languages like Fortran and C. python of course is object oriented, and so it has a class as well, much like C++ and java.


In [ ]:
def mysqrt(x):
    # this is my sqrt function (comment vs. docstring)
    import math
    if x < 0:
        return -math.sqrt(-x)
    else:
        return math.sqrt(x)
    
for x2 in [-4.0,0.0,4.0]:
    print(mysqrt(x2))
    
print(x2)

In [ ]:
dir(mysqrt)

In [ ]:
print(mysqrt.__doc__)

Functions can have default arguments:


In [ ]:
def mysqrt(x,verbose=False,classic=False):
    import math
    if classic:
        if verbose: print("classic",x)
        return math.sqrt(x)
    if x < 0:
        if verbose: print("fixing",x)
        return -math.sqrt(-x)
    else:
        if verbose: print("correct",x)
        return math.sqrt(x)

print(mysqrt(-4.0))
print(mysqrt(-2.0,True))
print(mysqrt(-2.0,classic=True))

In [ ]:
try:
    print(mysqrt(-2.0,classic=True))
except:
    print("some error, deal with it")

Scope of variables: as in other languages, objects inside a function are not visible outside and vice versa, but again, perhaps with a tiny twist


In [ ]:
a1 = 1.0
def testa(x):
    global a1
    print(a1+x)
    a1 = a1 + x
    # y = a1 - x
testa(2.0)
testa(2.0)
# print(y)

In [ ]:
import math
print(math.pi)

def mypi(x):
    # this will overwrite \pi !!!
    math.pi = x
    return {x,x}
a=mypi(3)
print(math.pi)
print(x2)
print('a',a)
#print("a=%g"  % a)
print(type(a))
print(type(mypi))

5. classes

Under the hood everything in python is an object, a class. But you can define your own classes.


In [ ]: