Python if Keyword
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")
Run example »
Related Pages
The else
keyword.
The elif
keyword.
Read more about conditional statements in our Python Conditions Tutorial.