Download PH2150 Scientific Computing Skills

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Monitor (synchronization) wikipedia , lookup

Indentation style wikipedia , lookup

Structured programming wikipedia , lookup

C Sharp syntax wikipedia , lookup

Block cipher mode of operation wikipedia , lookup

Block sort wikipedia , lookup

Transcript
PH2150 Scientific Computing Skills
Control Structures in Python
• In general, statements are executed sequentially, top to bottom.
• There are many instances when you want to break away from this e.g,
• Branch point: choose to go in one direction or another based on meeting
some condition.
• Repeat: You may want to execute a block of code several times.
• Programming languages provide various control structures that allow for
more complicated execution paths.
• Here we will focus on two such examples conditionals and loops:
• Will see the if, while and for statements and associated else, elif, range,
break and continue commands
PH2150 Scientific Computing Skills
The if statement is used to check a condition: if the condition is TRUE, we run a block
of statements (the if-block), else (FALSE) we process another block of statements
(else-block) or continue if no optional else clause.
if guess==number: #if followed by condition we
are testing, colon indicates begin the if-block
<Block of statements> # indent shows
that you are in the if-block, when the block of
statements is completed you will drop back into
the main program.
if guess==number: #if TRUE execute statements
<Block of statements> # skips else
else:#if FALSE executes the second block
<Block of statements> # returns to main
program after these statements.
PH2150 Scientific Computing Skills
The elif statement allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE..
if guess==number: #1st condition
<Block of statements> #
elif condition2: # checks for a 2nd condition
which if TRUE runs second block of statements.
<Block of statements> #
elif condition3: # checks for a 3rd condition
which if TRUE runs 3rd block of statements.
<Block of statements> #
elif condition4: # checks for a 4th condition
which if TRUE runs 4th block of statements.
<Block of statements> #
else: # optional else if all above statements are
FALSE
<Block of statements> #
PH2150 Scientific Computing Skills
You may want to check for another condition after a condition resolves to TRUE. Here
you can use the nested if construct.
In a nested if construct, you can have an if...elif...else construct inside another
if...elif...else
if condition1: #
<Block of statements> # runs statements then checks next if.
if condition2: #checks for a 2nd condition which if TRUE runs
second block of statements.
<Block of statements> #
elif condition3: # checks for a 3rd condition which if TRUE runs
3rd block of statements.
<Block of statements> #
else: # if condition 1 is TRUE, but 2 and 3 are FALSE.
<Block of statements> #
else: # optional else if condition 1 is FALSE
<Block of statements> #
PH2150 Scientific Computing Skills
Loops: for loops are traditionally used when you have a piece of code which you
want to repeat number of times. As an alternative, there is the while Loop, the while
loop is used when a condition is to be met, or if you want a piece of code to repeat
forever.
for letter in 'Python':
# loops for each character in string
print 'Current Letter :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits:
# loops for each element in list
print 'Current fruit :', fruit
for index in range(len(fruits)): # loops for each element in
list
print 'Current fruit :', fruits[index]
FruitsA=[x for x in fruits if x[0]==‘a’] # returns [‘apple’]
PH2150 Scientific Computing Skills
Control Structures in Python: range()
The range() function creates a list of numbers determined by the input parameters.
range([start,] stop[, step]) -> list of integers
When step is given, it specifies the increment (or decrement).
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(5, 10)
[5, 6, 7, 8, 9]
>>> range(0, 10, 2)
[0, 2, 4, 6, 8]
e.g. How to get every second element in a list?
for i in range(0, len(data), 2):
print data[i]
PH2150 Scientific Computing Skills
Loops: the while loop is used when a condition is to be met, or if you want a piece of
code to repeat forever.
while expression: #while TRUE execute block of statements
<Block of statements>
var = 1
while var == 1 : # This constructs an infinite loop
num = raw_input("Enter a number :")
print "You entered: ", num
if num=='Q': # This gives a route out of loop
print “Goodbye”
break # break will terminate the loop