if-else Statement


☞ It is used to execute one block of code if the condition is true, and another block of code if the condition is false.

Syntax :
 
if <test_condition>:
        Statement_1
        Statement_2
        ……….
 else:
        Statement_1
        Statement_2
        ……….
Example :
 
x = int(input("Enter a number : "))
if x>=0:
    print("Positive Number")
else:
    print("Negative Number")
Output :
 
Enter a number : -6
Negative Number
Explanation :
  • If the user inputs a number greater than or equal to 0, the condition x >= 0 is true, and the program prints "Positive Number".
  • If the user inputs a negative number, the condition x >= 0 is false, and the program executes the code block following the else statement, printing "Negative Number".
This code provides a simple and concise way to determine whether a number is positive or negative.