Python Conditional Statements


 Execute a block of Statements when status is truthful Python Conditional Statements
Python Conditional Statements

1) Execute a block of Statements when status is true.
2) Execute a block of Statements when a chemical compound status is true.
3) Execute a block of Statements when status is truthful otherwise execute another block of Statements
4) Decide amid several alternates(elif)
5) Execute a block of Statements when to a greater extent than than i status is truthful (Nested if)

Conditional Statements are business office of command period of time statements inward figurer programming,

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

In Python linguistic communication besides nosotros tin notice these 3 types of statements...
Generallly inward whatever Programming langguage conditional Statements are two types...
1) If Statement
2) Switch Statement
but inward Python no Switch statement, alone if statement
-------------------------------
Usage of Conditional Statements / Decision Making Statements

1) Execute a block of Statements when status 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 chemical compound status is true

Syntax:
if ((condition1/Expression1) in addition to (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 status is truthful otherwise execute another block of Statements

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

Example:

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

Syntax:

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

Example:
Initialize an Integer Variable in addition to verify the range

If the let on is inward betwixt 1 in addition to 100 in addition to thence display "Number is a Small Number"
If the let on is inward betwixt 101 in addition to one 1000 in addition to thence display "Number is a Medium Number"
If the let on is inward betwixt 1001 in addition to 10000 in addition to thence display "Number is a Big Number"
If the let on is to a greater extent than than 10000 in addition to thence display "Number is a High Number"
Otherwise display "Number is a either Zero or Negative Number"
-------------------------------
Python Program:

a=0;
if ((a>=1) in addition to (a<=100)):
  impress ("A is a Small Number");
elif ((a>100) in addition to (a<=1000)):
  impress ("A is a Medium Number");
elif ((a>1000) in addition to (a<=10000)):
  impress ("A is a Big Number");
elif (a>10000):
  impress ("A is a High Number");
else:
  impress ("A is either Zero or Negative Number");
-------------------------------
5) Execute a block of Statements when to a greater extent than than i status is truthful (Nested if)

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

Example:
Initialize a, b, c, in addition to d variables (Integer variables), cheque if the a variable 
is bigger than other 3 variables or not?

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

if (a>b):
  if (a>c):
    if (a>d):
      impress ("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):
      impress ("A is a Big Number");
    else :
       print ("A is Not a Big Number "+ "3rd Condition is non true");
  else :
    impress ("A is Not a Big Number "+ "2nd Condition is non true");
else :
 print ("A is Not a Big Number "+ "1st Condition is non true");
-------------------------------
Using Compound Condition

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

if ((a>b) in addition to (a>c) in addition to (a>d)):
    impress ("A is a Big Number");
else:
    impress ("A is Not a Big Number");
-------------------------------
Nested if Condition vs. Compound Condition

In Nested if status nosotros tin write multiple else parts, where equally inward Compound 
Condition nosotros tin write unmarried else business office only.
-------------------------------
Problem: Find the biggest variable (Integer variables) amid iv variables

Use Compound Condition in addition to else if...

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

if ((a>b) in addition to (a>c) in addition to (a>d)):
    impress ("A is a Big Number");
elif ((b>a) in addition to (b>c) in addition to (b>d)):
    impress ("B is a Big Number");
elif ((c>a) in addition to (c>b) in addition to (c>d)):
    impress ("C is a Big Number");
else:
    impress ("D is a Big Number");
-------------------------------
Also Read:

1) In Which sequence should We acquire Python?

2) Introduction to Python

3) Download & Install Python


4) Variables in addition to Data Types inward Python

5) Python Operators

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post