Python if Keyword

❮ Python Keywords


Example

Print "YES" if x larger than 3:

x = 5
if x > 3:
  print("YES")
Try it Yourself »

Definition and Usage

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

Use the else keyword to execute code if the condition is False, see example below.


More Examples

Example

Print "YES" if x is larger than 6, otherwise print "NO":

x = 5
if x > 6:
  print("YES")
else:
  print("NO")
Try it Yourself »

Related Pages

The else keyword.

The elif keyword.

Read more about conditional statements in our Python Conditions Tutorial.


❮ Python Keywords

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.