Python | Conditional statment



Python | Conditional statements


         in real word program is more huge , so we need to skip some statements , and run a specific statements

         repeatedly so  that you make condition for some specific statement


        

in python , if and if...else statements are used for decision making


if Statement

if statement is simple decision making statement if provided condition is true then execute the statement otherwise no Example     x = 65     y = 75     if x < y :         print("65 is less than 75")     print("this is not If statement")
Output
    65 is less than 75



in this above Example, if x is less then to y so execute the statement , otherwise no and "this is not If statement" this statement is always print because it is not containing with if statement 


Example

    x = 65
    y = 75

    if x < y:
        print("65 is less than 75")

Output

    This will show Indentation Error


If…else statement
We can understand the if…else statement using Example Example     x = 65     y = 75     if x < y : print("65 is less than 75")     else : print("65 is not less than 75")     print("this is not If statement") Output     
    65 is less than 75     this is not If statement

in this above Example, if x is less then to y so execute the statement , otherwise execute else statement and "this is not If statement" this statement is always print because it is not containing with if statement Example     x = 95     y = 75     if x < y: print("95 is less than 75")     else: print("95 is not less than 75")     print("this is not If statement") Output     
    95 is not less than 75     this is not If statement

in this above Example, if x is not less then to y so execute else statement , "this is not If statement" this statement is always print because it is not containing with if statement 


Elif statement

    elif statement is used for , the if statement condition is not true then try this one   


Example

    x = 95
    y = 75

    if x < y:
print("95 is less than 75")

    elif x > y:
print("95 is greater than 75")

    print("this is not If statement")


Output

    95 is greater than 75
    this is not If statement


In this shown above Example, when will run the code by python interpreter check the if condition is true or false ,if true then execute statement and false then check  elif condition   
If true, the execute statement .


If … elif …. else statement

    If … elif …. else statement understand using Example



Example

    x = 95
    y = 95

    if x < y:
print("95 is less than 75")

    elif x > y:
print("95 is gretar than 75")
    
    else :
print("x equal to y")

    print("this is not If statement")



Output

    x equal to y
    this is not If statement


Explanation

In this shown above Example, when will run the code by python interpreter check the if condition is true or false ,if false then check elif condition is true or not , elif condition is also false then last run else statement 
 
short-hand if , else statement

    It means , write condition in small line of code 
    In this , there are three components

    The condition or expression
    The positive value 
    The negative value

    Syntax
    
    expression ? positive value : negative value

    short-hand if

    In one line to declare condition, if true then execute statement otherwise no

    Example

x = 95
        y = 75

    if x > y : print ("x is greater than y")

    Output

        x is greater than y

    short-hand if…else

    In one line to declare condition, if true then execute statement otherwise no

    Example

x = 95
        y = 97

        print ("x is greater than y") if x > y else print ("x is less than y")

    Output

        x is less than y

Python |Conditional statment

Python | Conditional statment

in real word program is more huge , so we need to skip some statements , and run a specific statements repeatedly so that you make condition for some specific statment

in python , if and if...else statements are used for decision making

if Statment

if statement is simple decision making statement

if provided condition is true then execute the statement otherwise no

Example

			
				x = 65
				y = 75

				if x < y:
					print("65 is less than 75")

				print("this is not If statement")	
			
		

Output

x is less than y

in this above Example, if x is less then to y so execute the statement , otherwise no

and

"this is not If statement" this statement is always print beacause it is not containing with if statement

Example

			
				x = 65
				y = 75

				if x < y:
				print("65 is less than 75")

			
		

Output

This will show Indention Error

Post a Comment (0)
Previous Post Next Post