Presentation is loading. Please wait.

Presentation is loading. Please wait.

PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.

Similar presentations


Presentation on theme: "PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances."— Presentation transcript:

1 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

2 PH2150 Scientific Computing Skills if guess==number: #if followed by condition we are testing, colon indicates begin the if-block # indent shows that you are in the if-block, when the block of statements is completed you will drop back into the main program. 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 guess==number: #if TRUE execute statements # skips else else:#if FALSE executes the second block # returns to main program after these statements.

3 PH2150 Scientific Computing Skills if guess==number: #1 st condition # elif condition2: # checks for a 2 nd condition which if TRUE runs second block of statements. # elif condition3: # checks for a 3 rd condition which if TRUE runs 3 rd block of statements. # elif condition4: # checks for a 4 th condition which if TRUE runs 4 th block of statements. # else: # optional else if all above statements are FALSE # The el if 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..

4 PH2150 Scientific Computing Skills if condition1: # # runs statements then checks next if. if condition2: #checks for a 2 nd condition which if TRUE runs second block of statements. # elif condition3: # checks for a 3 rd condition which if TRUE runs 3 rd block of statements. # else: # if condition 1 is TRUE, but 2 and 3 are FALSE. # else: # optional else if condition 1 is FALSE # 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

5 PH2150 Scientific Computing Skills 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’] 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.

6 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]

7 PH2150 Scientific Computing Skills while expression: #while TRUE execute 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 Loops: the while loop is used when a condition is to be met, or if you want a piece of code to repeat forever.


Download ppt "PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances."

Similar presentations


Ads by Google