In this article, we will show you Python if statement with examples.
If statements are control flow statements which helps us to run a particular code only when a certain condition is satisfied.
Python if statement flow diagram:
Python if syntax:
if expression: statement1 statement2; ...........
Python if statement examples:
Example1:
x=20 if x>5: print(x) print("Welcome to r2schools")
Output:
20 Welcome to r2schools
Example2:
x=int(input("Enter the a number:")) if x>0: print(x) print("Given number is positive") if x<0: print(x) print("Given number is negative")