If you need to include an If statement, inside of an Else statement, you might need an elif command, which combines the two of them together. Let’s look at an example real quick.
if num1 > num 2 :
# do something
else :
if num1 < num2 :
# do something else
else :
# do a third thing
Notice that everything revolves around these two numbers, and checking them. Since num1 cannot be both greater than and less than num2, it is a perfect instance to use the elif statement.
It would look something like:
if num1 > num2 :
# do something
elif num1 < num2 :
# do something else
else:
# do a third thing
Notice how this is a little cleaner, everything is on the same level of indentation, which makes it easier to read, and it takes one less line of code, so it is a little faster to read.
Elif works well, anytime you need to check an if statement, inside of an else statement – and that is the only time the block of code will actually be used. It cannot be used all the time, but when it can, it makes things easier to read and work with, reducing the chances of syntax errors.
Using the ElIf (Else If) in Python was originally found on Access 2 Learn