Skip to content

playground_03_control_flow

Miguel Sozinho Ramalho edited this page Oct 17, 2017 · 3 revisions

Playground 03 - Control Flow

Decision making

Loops

If

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.

python decision making

Good reference

Top

x = 10
if(x < 0): #parenthesis are optional
    print("smaller than zero") #indentation is required
elif(x == 0): # else if
    # commands can be written in the same line as long as they are separated by ;
    print("zerooooooo"); print("oooooooooo") 
else:
    print("bigger than", 0)
    print("I'm still within the else clause!")

print("They kicked me out of the clause :'(")

For

A for loop statement allows us to execute a statement or group of statements multiple times, usually a fixed number of times.

Python loops

Good reference

Top

IWantYouBack = ["ABC", "It's easy as 1 2 3", "As simple as do re mi"]

for i in IWantYouBack:
    print(i, len(i))

# bad idea to insert/remove things on the iterable you are iterating through, so we make a copy
for i in IWantYouBack[:]:
    IWantYouBack.insert(0, "I want you back")

for i in range(5):  # i = 0, 1, 2, 3, 4 -> [0, 5[
    print(i)

for i in range(10, 50, 10):  # i = 10, 20, 30, 40
    print(i)


# strange concatenation example
people = ["Person"] * 3 + ["Killer"] + ["Not"] * 2
print(people)  # ['Person', 'Person', 'Person', 'Killer', 'Not', 'Not']

for p in people:
    if(p == "Killer"):
        print("Found the killer!")
        break  # exits the loop
    else:  # useless in this case, just wanted to show off the continue statement
        continue
else:  # this branch runs when no break happens in the for loop
    print("No killer was found")


# enumerate returns the index and the value
for i, v in enumerate(people):
    if(v == "Killer"):
        print("Found the killer at position {0}".format(i))
        #print("Found the killer at position %d" % i)
        break
else:
    print("No killer was found")

While

A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.

Python while loop flow

Good reference

Top

from random import randrange

bird = "duck"
while bird != "goose":
    print("waiting...")
    if(randrange(0, 101) < 30):
        bird = "goose"
print("I'm a goose!")

from random import randrange
i = 0
while i < 100:
    i += randrange(0, 8)
    if i % 10 == 0:
        print("multiple of 10: %d" % i)
        break
else:
    print("No multiple of 10...")

Top

Clone this wiki locally