Python else Keyword

Example

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

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

Run Instance

Definition and Usage

The else keyword is used in conditional statements (if statements) and determines what to do when the condition is False.

The else keyword can also be used in try...except code blocks, see the following example.

More Examples

Example

Define a handling method for when no errors are raised in the try ... except block:

x = 5
try:
  x > 10
except:
  print("Something went wrong")
else:
  print("The 'Try' code was executed without raising any errors!")

Run Instance

Related Pages

if Keyword

elif Keyword

Please visit our Python Condition Tutorials Learn more about conditional statements.