1.

Conditional Statements in Python

Answer»

  • If Statements: If statement is a condition statement that will perform some operation, if the expression given in it evaluates to true as shown below:
>>> var = "Good"
>>> if var == "Good":
... print("Same")
...
Same

  •  Elif Statements:  This statement is used in conjunction with the if statement to add some other condition which is evaluated if the condition in if statement fails.
>>> var = "Good"
>>> if var == "Good":
... print("Same")
... elif var != "Good":
... print("Not Same")
...
Same

  • Else Statements:  This statement is used to perform some operation, if all the if and elif statements evaluates to be false.
>>> var = "Good"
>>> if var != "Good":
... print("Not Same")
... else:
... print("Same")
...
Same

The final if-elif-else ladder looks like shown below:




Discussion

No Comment Found