To program decisions and do different things depending on different situations can be achieved by the control statements. There are three control flow statements in Python - if, for and while.
The if statement:
The if statement contains a logical expression using which data can be compared, and a decision is made based on the result of the comparison.
Here if statement, condition is evaluated first. If condition is true, the statement(s) block are executed. Otherwise, the next statement following the statement(s) block is executed.
inti= int(input('Enter One or Two : '))
if inti==1:
print"Value of integer, int is 1"
if inti==2:
print"Value of integer, int is 2"
The else Statement:
An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value.
inti= int(input('Enter One or Two : '))
if inti==1:
print "Value of integer, inti is 1"
else:
print "I told you to enter 1. You have entered another value"
The while Loop:
The while loop is one of the looping constructs available in Python. The while loop continues until the expression becomes false. The expression has to be a logical expression and must return either a true or a false value
count = 0 while (count < 9): print 'line number:', count count = count + 1 print "Finished!"
The for Loop:
The for loop in Python has the ability to iterate over the items of any sequence, such as a list or a string.
Try the following code
fruits = ['banana', 'apple', 'mango'] for index in range(len(fruits)): print 'Current fruit :', fruits[index] print "No more fruits!"
The break Statement:
The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C.
The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops.
for letter in 'Bioinformatics': if letter == 'i': break print 'Current Letter :', letter var = 10 while var> 0: print 'Current variable value :', var var = var -1 if var == 5: break print "End!"