Conditional Statements in Python

Conditional statements are statements in python that provide a choice for the control flow based on a condition.It means that the control flow of the python program will be decided based on the outcome of condition.

TYPES OF CONDITIONAL STATEMENTS:

1.If conditional statement in python:if the simple code of block is to performed if the condition holds then the if statement is used.Here the condition mentioned holds then the code of the block runs otherwise not.

Syntax:

if condition:

#statements to execute if

#condition is true

2.if else conditional statements in python:in a conditional if statement the additional block of code is merged as an else statement which is performed when if condition is false.

Syntax:

if (condition):

#Executes this block if

#condition is true

else:

#Executes this block if

#condition is false

3.Nested if..else conditional statements in python:Nested if..else means an if-else statement inside another if statement.We can use one if or else if statement inside another if or else if statements.

4.If-elif-else conditional statements in python:The if ststements are executed from the top down.As soon as one of the conditions controlling the if is true,the statement associated with that if is executed,and the rest of the ladder is by passed.if none of thre conditions is true,then the final”else” statement will be  executed.

5.Ternary expression conditinal statements in python:The python ternary expression determines if a condition is true or flase and then returns the appropriate value in accordance with the result.It is useful in cases where we need to assign a value to a variable based on a simple condition,and we want to keep our code more concise-all in just one line of code.

Syntax:[on_true ] if [expression] else [on_false]

expression: condition _expression | lambda_expr

x = 10
y = 15
 
if x == 10:
    print("x equals 10.")
    if y > 20:
        print("y is greater than 20.")
    elif y < 20:
        print("y is less than 20.")
    else:
        print("y equals 20.")
else:
     print("x does not match.")

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top