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")
print("NO")
Usage and Description
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 error is 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!")
Related Pages
Please visit our Python Conditions Tutorials Learn more about conditional statements.