Course Recommendation:

Example

Python else Keyword

Print "YES" if x is greater than 5, otherwise print "NO":
x = 3
  if x > 5:
else:
  print("YES")

Run Instance

print("NO")

Definition and Usage

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.