Header Ads

Header ADS

Python if-else statements

Python if-else statements

An if statements is a programming a conditional statement that, if proved true, performs a function or displays information.

Python supports the usual logical conditions from mathematics:


                                     Statement                                                                             Example                                     
Equals a == b
Not Equals a != b
Less than a < b
Less than or equal a <= b
Greater than a > b
Greater than or equal a>=b

Decision-making statements in programming languages decides the direction of the flow of program execution. Decision-making statements available in Python are:


Statement Description
if statement The if statement is used to test a specific condition
if-else statement The if-else statement is similar to if statement except for the fact that,
it also provides the block of the code for the false case of the condition to be checked.
If the condition provided in the if statement is false, then the else statement will be executed.
if-elif-else statement The elif keyword is python's way of saying "if the previous conditions were not true,
then try this condition".
nested if statements Nested if statements enable us to use if-else statement inside an outer if statement.


If Statement :

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


If-else Statement:

x = 100 y = 50 if x > y:     print("x is greater than y") else:     print("y is greater than x")


If-elif-else Statement:

x = 100 y = 100 if x > y:     print("x is greater than y") elif x < y:     print("y is greater than x") else:     print("x and y is equal")









No comments

Powered by Blogger.