Python Control Statement

The statements that allow the computer to select or repeat an action. Control statement selects one option among all options and repeats specified section of program.

Python If and if-else statements

Python if-else statement allows to execute a statement based on some logical expression.

Python if statement

Through Python if statement, a programs can branch to section of code or just skip it.

Example 1 :

print "Welcome to L4wisdom "
password= raw_input("Enter the password\t")
if password=="secret":
   print ".............................."
   print "welcome in the L4wisdom "
Python if statement
if statement
The above example shows branching, if you write right password, you would enter in if statement. If you type wrong password then it would skip the if statement. You may have noticed that the second line of the if structure , print "....." and print "Welcome to L4Wisdom", is indented, By indenting the line, it becomes a block. This block is executed if input string satisfy the if condition. To make a block, : is used, as used in the end of if statement. The all indent lines written after : make block as shown in figure below
Python Indentation
Tabs or Spaces ?
This is really question of personal style. Whatever you want to use but be consistent, if you want to use two spaces indentation then always abide with this through out program. Don't mix spaces and tabs. Although you can use both but this can lead to serious headache later.

Python if-else statement

It is also called a two-way selection statement, because it leads the program to make a choice between two alternative courses of action.
Here is the Python syntax for the if-else statement:
	if condition :
		sequence of statements-1
		sequence of statements-2

Example 2 :

print "Welcome to L4wisdom "
password= raw_input("Enter the password\t")
if password=="secret":
   print ".............................."
   print "welcome in the L4wisdom "
else :
   print "Access Denied";
if-else statmenet
if-else statmenet
If the value of password is equal to "secret:, the program print "welcome in the L4wisdom", just like before. But now thanks to the else statement, the program prints "Access denied" otherwise.

Python if-elif-else structure

Occasionally, a program contains several testing conditions that involve more than two alternative course of action. In multi-way if statements program checks each condition untill one evaluates to true or all evaluate to false. When a condition evaluates to True, the corresponding action of condition is took place. If no condition satisfy means evaluate to true then the corresponding action of trailing else is performed.
The syntax of the multi-way if statement is the following:
if condition-1:
	sequence of statements-1
elif condition-n:
	sequence of statements-n
	default sequence of statements
Let us consider a example of grade system. The below table shows the grade A,B,C,D ranges from 1 to 100.
Letter Marks Range
AAll grades above 89
BAll grades above 79 and below 90
CAll grades above 69 and below 80
DAll grades below 70
Grading System
See the implementation of program below.

Example :

num =int(raw_input("Enter the number:"))
if num > 89:
    letter = 'A'
elif num > 79:
    letter = 'B'
elif num > 69:
    letter = 'C'
else num > 89:
    letter = 'D'
print "The Grade is " , letter
elif statement
elif statements
I hope you have understood Python control statements.