Some Points to Remember


☞ You can write test_condition with or without parenthesis () and after if or else statement colon (:) is necessary so that it can form a block.

Example :
 
x = int(input("Enter a number : "))
if x>0:           #without parenthesis
    print("Positive Number")
elif(x<0):       #with parenthesis
    print(“Negative Number”)
else:
    print("Neither Positive Nor Negative")
Output :
 
Enter a number : -6
Negative Number

☞ Non-zero(positive or negative) numbers evaluate to True whereas zero evaluates to False.

Example :
 
x = int(input("Enter a number : "))
if(x) :           
    print("Hello")
else:
    print("Bye")
Output :
 
Enter a number : 0
Bye

☞ Non-empty string evaluates to True whereas empty string evaluates to False.

Example :
 
x = ""
if(x) :           
    print("Hello")
else :
    print("Bye")
Output :
 
Bye

Indentation : It is a way to tell the interpreter that the group of statements belongs to a particular block of code.

☞ By default it is represented by 4 spaces. Below we are showing it with 4 red dots to make you understand the meaning of indentation but originally they are 4 red dots representing 4 spaces.

Syntax :
 
if <test_condition>:
....Statement_1
....Statement_2
....……….
else:
....Statement_1
....Statement_2
....……….