Python Conditional Statements


Python Tutorials
Python Conditional Statements

1) Execute a block of Statements when condition is true.
2) Execute a block of Statements when a compound condition is true.
3) Execute a block of Statements when condition is true otherwise execute another block of Statements
4) Decide among several alternates(elif)
5) Execute a block of Statements when more than one condition is true (Nested if)

Conditional Statements are part of control flow statements in computer programming,

Control Flow Statements
1) Conditional Statements
2) Loop Statements
3) Branching Statements

In Python language also we can find these 3 types of statements...
Generallly in any Programming langguage conditional Statements are 2 types...
1) If Statement
2) Switch Statement
but in Python no Switch statement, only if statement
-------------------------------
Usage of Conditional Statements / Decision Making Statements

1) Execute a block of Statements when condition is true.

Syntax:

if (Condition/Expression):
Statement(s);

Example:

a, b = 100, 50;
if (a>b):
   print ("A is a Big Number");

a, b = 100, 50;
if (a>b):
   print ("A is a Big Number");
   print ("Hello Python");
-------------------------------
2) Execute a block of Statements when a compound condition is true

Syntax:
if ((condition1/Expression1) and (Condition2/Expression2)) :
Statement(s);

Example:
a, b, c = 100, 50, 700;
if ((a>b) or (a>c)):
   print ("A is a Big Number");   
-------------------------------
3) Execute a block of Statements when condition is true otherwise execute another block of Statements

Syntax:
if (Condition):
Statement(s);
else:
Statement(s);

Example:

a, b = 100, 200;
if (a>b):
  print ("A is a Big Number");
else :
  print ("B is a Big Number");
-------------------------------
4) Decide among several alternates(elif)

Syntax:

if (condition):
Statement(s);
elif (condition):
Statement(s);
elif (condition):
Statement(s);
else:
Statement((s);

Example:
Initialize an Integer Variable and verify the range

If the number is in between 1 and 100 then display "Number is a Small Number"
If the number is in between 101 and 1000 then display "Number is a Medium Number"
If the number is in between 1001 and 10000 then display "Number is a Big Number"
If the number is more than 10000 then display "Number is a High Number"
Otherwise display "Number is a either Zero or Negative Number"
-------------------------------
Python Program:

a=0;
if ((a>=1) and (a<=100)):
  print ("A is a Small Number");
elif ((a>100) and (a<=1000)):
  print ("A is a Medium Number");
elif ((a>1000) and (a<=10000)):
  print ("A is a Big Number");
elif (a>10000):
  print ("A is a High Number");
else:
  print ("A is either Zero or Negative Number");
-------------------------------
5) Execute a block of Statements when more than one condition is true (Nested if)

syntax:
if (condition){
 if (condition){
  if (condition){
  Statements
  --------------
  --------------
  }
  }
  }

Example:
Initialize a, b, c, and d variables (Integer variables), check if the a variable 
is bigger than other three variables or not?

a, b, c, d = 100, 90, 70, 500;

if (a>b):
  if (a>c):
    if (a>d):
      print ("A is a Big Number");
    else :
       print ("A is Not a Big Number");
-------------------------------
a, b, c, d = 100, 909, 70, 50;

if (a>b):
  if (a>c):
    if (a>d):
      print ("A is a Big Number");
    else :
       print ("A is Not a Big Number "+ "3rd Condition is not true");
  else :
    print ("A is Not a Big Number "+ "2nd Condition is not true");
else :
 print ("A is Not a Big Number "+ "1st Condition is not true");
-------------------------------
Using Compound Condition

a, b, c, d = 100, 90, 70, 50;

if ((a>b) and (a>c) and (a>d)):
    print ("A is a Big Number");
else:
    print ("A is Not a Big Number");
-------------------------------
Nested if Condition vs. Compound Condition

In Nested if condition we can write multiple else parts, where as in Compound 
Condition we can write single else part only.
-------------------------------
Problem: Find the biggest variable (Integer variables) among 4 variables

Use Compound Condition and else if...

a, b, c, d = 100, 90, 70, 50;

if ((a>b) and (a>c) and (a>d)):
    print ("A is a Big Number");
elif ((b>a) and (b>c) and (b>d)):
    print ("B is a Big Number");
elif ((c>a) and (c>b) and (c>d)):
    print ("C is a Big Number");
else:
    print ("D is a Big Number");
-------------------------------
Also Read:

1) In Which sequence should We learn Python?

2) Introduction to Python

3) Download & Install Python


4) Variables and Data Types in Python

5) Python Operators

0 comments:

Post a Comment