Skip to main content

Conditional Statements:

Python supports the following conditional statements:

  • if statements
  • if else statements
  • elif statements
  • else
  • nested if…elif…else statements

if statements: Syntax for using the if keyword is as follows:
if [conditional expression]:
    [statement(s) to execute]
--> if keyword and the conditional expression is ended with a colon. In [conditional expression] some conditional expression is introduced that is supposed to return a boolean value, i.e., True or False. If the resulting value is True then the [statement to execute] is executed, which is mentioned below the if condition with a tabspace (This indentation is very important).
if…else statements: Syntax for using the if else keyword is as follows:

if [test expression]:
    [statement(s)]
else:
  [statement(s)]

Example: Program to Read the Marks


if…elif…else statements:
 Syntax for using the if…elif…else statements is as follows:

if[condition #1]: 
    [statement #1]
elif[condition #2]:
    [statement #2]
elif[condition #3]:
    [statement #3]
else:
    [statement when if and elif(s) are False]
Example: Program to Read a Number from the User then check if it is Positive, Negative and Zero

Nested If Statement: Syntax for using the Nested If Statements as follows:

if[condition #1]:
    if[condition #1.1]:
        [statement to exec if #1 and #1.1 are true]
    else:
        [statement to exec if #1 and #1.1 are false]
else:
    [alternate statement to execute]
Example: Program to Find the Biggest among Three Numbers



Comments

Popular posts from this blog

Guessing the Number Game

Find the final amount after Discount

Simple Arithmetic Calculator - User Defined Function