Python if Keyword

Example

Print "YES" if x is greater than 5:

x = 9
if x > 5:
  print("YES")

Run Instance

Definition and Usage

The if keyword is used to create conditional statements (if statements) and allows you to execute the code block only when the condition is True.

Use the else keyword to execute code when the condition is False, please refer to the following example.

More Examples

Example

Print "YES" if x is greater than 9, otherwise print "NO":

x = 5
if x > 9:
  print("YES")
else:
  print("NO")

Run Instance

Related Pages

else Keyword

elif Keyword

Please use our Python Condition Learn more about conditional statements in the tutorials.