Python if statement
Python if statement
Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as:
- If statement
- If-else statement
- elif statement
if statement
The if statement is the easiest conditional statement. It is used to test a particular condition. If the condition is true, a block of code (if statement block) will be executed.
Syntax:
if expression: statement
Flow chart of if statement: The flowchart of if statement is following

In the above flowchart, First it checks the condition if the condition is true, then conditional code(if statement) will be executed. If the condition is false, then first set of code after the end of the 'if' statement will be executed
Python programming language supposes any non-zero and non-null values as TRUE. If it is zero, then it is assumed as FALSE values.
Indentation in Python
Indentation is used to declare the block of code. All the statements of one block are intended at the same level indentation. Python doesn't allow the use of curly braces or parentheses for block-level code. Indentation makes programming simple and easy in Python.
Example: There are following the example of if statement
1. Write a program to check number is even
n = int(input("Enter the number:")) if(n%2==0): print("The number is even") print("The number is odd")
Output:
Enter the number: 10 The number is even
2. Write a program to check the weight of the passenger’s luggage
weight = float(input("How many Kilograms(kg) does your luggage?: ")) if (weight <30): print("You have charged ?300 for heavy luggage .") print("Thank you for your business.")
Output:
How many Kilograms does your suitcase weigh?: 35 There is a 250 charge for heavy luggage. Thank you for your business
3. Write a program to print the largest number of the three numbers
a = int(input("Enter the First number:")) b = int(input("Enter the Second number:")) c = int(input("Enter the Third number:")) if(a>b) and (a>c): print(a, 'is largest number' ) if(b>a) and (b>c): print(b, 'is largest number') if (c>a) and (c>b): print(c, "c is largest");
Output:
Enter the First number: 100 Enter the Second number: 145 Enter the Third number: 200 200 is largest
4. Write a program to check the given number is positive or negative
num = 3 if num >0: print(num, "is a positive number.") print("This is always printed.") num = -1 if num >0: print(num, "is a positive number.") print("This is also always printed.")
Output:
3 is a positive number. This is always printed. This is also always printed.