Control Flow
-
Upload
marieswaran-ramasamy -
Category
Technology
-
view
126 -
download
4
Transcript of Control Flow
Control Flow
Python
Control Flow
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
Simplest form of repetition is while loop
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
Python Control Flow
Simplest form of repetition is while loop
num_moons = 3
whilewhilewhilewhile num_moons > 0: testprintprintprintprint num_moons
num_moons -= 1
Python Control Flow
Simplest form of repetition is while loop
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1do
Python Control Flow
Simplest form of repetition is while loop
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
3
do
Python Control Flow
Simplest form of repetition is while loop
num_moons = 3
whilewhilewhilewhile num_moons > 0: test againprintprintprintprint num_moons
num_moons -= 1
3
Python Control Flow
Simplest form of repetition is while loop
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
3
2
Python Control Flow
Simplest form of repetition is while loop
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
3
2
1
Python Control Flow
While loop may execute zero times
printprintprintprint 'before'
num_moons = -3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
printprintprintprint 'after'
Python Control Flow
While loop may execute zero times
printprintprintprint 'before'
num_moons = -3
not true when first tested…whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
printprintprintprint 'after'
not true when first tested…
Python Control Flow
While loop may execute zero times
printprintprintprint 'before'
num_moons = -3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
printprintprintprint 'after'
…so this is never executed
Python Control Flow
While loop may execute zero times
printprintprintprint 'before'
num_moons = -3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
printprintprintprint 'after'
before
after
Python Control Flow
While loop may execute zero times
printprintprintprint 'before'
num_moons = -3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
num_moons -= 1
printprintprintprint 'after'
before
after
Important to consider this case when designing
Python Control Flow
Important to consider this case when designing
and testing code
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
Python Control Flow
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
Python Control Flow
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
3
Python Control Flow
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
3
3
Python Control Flow
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
3
3
3
Python Control Flow
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
3
3
3
⋮
Python Control Flow
⋮
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
3
3
3
⋮
Nothing in here changes
the loop control condition
Python Control Flow
⋮
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
3
3
3
⋮
Python Control Flow
⋮
Usually not the desired behavior…
While loop may also execute forever
printprintprintprint 'before'
num_moons = 3
whilewhilewhilewhile num_moons > 0:
printprintprintprint num_moons
printprintprintprint 'after'
before
3
3
3
⋮
Python Control Flow
⋮
Usually not the desired behavior…
…but there are cases where it's useful
Why indentation?
Studies show that's what people actually pay
attention toattention to
Python Control Flow
Why indentation?
Studies show that's what people actually pay
attention toattention to
– Every textbook on C or Java has examples where
indentation and braces don't match
Python Control Flow
Why indentation?
Studies show that's what people actually pay
attention toattention to
– Every textbook on C or Java has examples where
indentation and braces don't match
Doesn't matter how much you use, but whole block
must be consistent
Python Control Flow
must be consistent
Why indentation?
Studies show that's what people actually pay
attention toattention to
– Every textbook on C or Java has examples where
indentation and braces don't match
Doesn't matter how much you use, but whole block
must be consistent
Python Control Flow
must be consistent
Python Style Guide (PEP 8) recommends 4 spaces
Why indentation?
Studies show that's what people actually pay
attention toattention to
– Every textbook on C or Java has examples where
indentation and braces don't match
Doesn't matter how much you use, but whole block
must be consistent
Python Control Flow
must be consistent
Python Style Guide (PEP 8) recommends 4 spaces
And no tab characters
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
Python Control Flow
Use if, elif, and else to make choices
not true when first tested…moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
…so this is not executedprintprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
…so this is not executed
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
this isn't true either…
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
…so this isn't executed
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
nothing else has executed…
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater' …so this is executed
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
greater
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
greater Always start with if
Python Control Flow
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
Can have any number of elif clauses (including none)
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
greater Always start with if
Python Control Flow
Can have any number of elif clauses (including none)
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
Can have any number of elif clauses (including none)
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
greater Always start with if
Python Control Flow
Can have any number of elif clauses (including none)
And the else clause is optional
Use if, elif, and else to make choices
moons = 3
ifififif moons < 0:
Can have any number of elif clauses (including none)
printprintprintprint 'less'
elifelifelifelif moons == 0:
printprintprintprint 'equal'
else:else:else:else:
printprintprintprint 'greater'
greater Always start with if
Python Control Flow
Can have any number of elif clauses (including none)
And the else clause is optional
Always tested in order
Blocks may contain blocks
num = 0
whilewhilewhilewhile num <= 10:
ifififif (num % 2) == 1:
printprintprintprint num
num += 1
Python Control Flow
Blocks may contain blocks
num = 0
whilewhilewhilewhile num <= 10:
ifififif (num % 2) == 1:
printprintprintprint num
num += 1
Count from 0 to 10
Python Control Flow
Blocks may contain blocks
num = 0
whilewhilewhilewhile num <= 10:
Print odd numbersifififif (num % 2) == 1:
printprintprintprint num
num += 1
Print odd numbers
Python Control Flow
Blocks may contain blocks
num = 0
whilewhilewhilewhile num <= 10:
ifififif (num % 2) == 1:
printprintprintprint num
num += 1
1
3
5
7
Python Control Flow
9
A better way to do it
num = 1
whilewhilewhilewhile num <= 10:
printprintprintprint num
num += 2
Python Control Flow
A better way to do it
num = 1
whilewhilewhilewhile num <= 10:
printprintprintprint num
num += 2
1
3
5
7
9
Python Control Flow
Print primes less than 1000
num = 2
whilewhilewhilewhile num <= 1000:
...figure out if num is prime...
ifififif is_prime:
printprintprintprint num
num += 1
Python Control Flow
Print primes less than 1000
num = 2
whilewhilewhilewhile num <= 1000:
Cannot be evenly divided
by any other integer
...figure out if num is prime...
ifififif is_prime:
printprintprintprint num
num += 1
Python Control Flow
by any other integer
Print primes less than 1000
num = 2
whilewhilewhilewhile num <= 1000:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial < num:
...figure out if num is prime...
ifififif is_prime:
printprintprintprint num
num += 1
Python Control Flow
ifififif ...num divisible by trial...:
is_prime = FalseFalseFalseFalse
trial += 1
Print primes less than 1000
num = 2
whilewhilewhilewhile num <= 1000:
Remainder is zero
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial < num:
...figure out if num is prime...
ifififif is_prime:
printprintprintprint num
num += 1
Python Control Flow
ifififif ...num divisible by trial...:
is_prime = FalseFalseFalseFalse
trial += 1
Print primes less than 1000
num = 2
whilewhilewhilewhile num <= 1000:
(num % trial) == 0
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial < num:
...figure out if num is prime...
ifififif is_prime:
printprintprintprint num
num += 1
Python Control Flow
ifififif ...num divisible by trial...:
is_prime = FalseFalseFalseFalse
trial += 1
Print primes less than 1000
num = 2
whilewhilewhilewhile num <= 1000:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
Python Control Flow
printprintprintprint num
num += 1
A more efficient way to do it
num = 2
whilewhilewhilewhile num <= 1000:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
Python Control Flow
printprintprintprint num
num += 1
A more efficient way to do it
num = 2
whilewhilewhilewhile num <= 1000:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
N cannot be divided
evenly by any number
greater than sqrt(N)
Python Control Flow
printprintprintprint num
num += 1
Any code that hasn't been tested is probably wrong
num = 2
whilewhilewhilewhile num <= 10:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
Python Control Flow
printprintprintprint num
num += 1
Any code that hasn't been tested is probably wrong
num = 2
whilewhilewhilewhile num <= 10:
2
3
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
4
5
7
9
Python Control Flow
printprintprintprint num
num += 1
Any code that hasn't been tested is probably wrong
num = 2
whilewhilewhilewhile num <= 10:
2
3
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
4
5
7
9
Python Control Flow
printprintprintprint num
num += 1
Any code that hasn't been tested is probably wrong
num = 2
whilewhilewhilewhile num <= 10:
2
3
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
4
5
7
9
Python Control Flow
printprintprintprint num
num += 1
Where's the bug?
Failures occur for perfect squares
num = 2
whilewhilewhilewhile num <= 10:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
Python Control Flow
printprintprintprint num
num += 1
Failures occur for perfect squares
num = 2
whilewhilewhilewhile num <= 10:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
2**2 == 4
Python Control Flow
printprintprintprint num
num += 1
Failures occur for perfect squares
num = 2
whilewhilewhilewhile num <= 10:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
2**2 == 4
So never check to see
if 4 % 2 == 0
Python Control Flow
printprintprintprint num
num += 1
Failures occur for perfect squares
num = 2
whilewhilewhilewhile num <= 10:
is_prime = TrueTrueTrueTrue
trial = 2
whilewhilewhilewhile trial**2 < num:
ifififif (num % trial) == 0:
is_prime = FalseFalseFalseFalse
trial += 1
ifififif is_prime:
2**2 == 4
So never check to see
if 4 % 2 == 0
Python Control Flow
printprintprintprint num
num += 1Or if 9 % 3 == 0, etc.