Eed Eed - 2 months ago 8
Python Question

If vs. else if vs. else statements?

Are:

if statement:
if statement:


the same as

if statement:
elif statment:


and

if statement:
else statement:


the same?
If not, what's the difference?

Answer

No, they are not the same.

if statement:
if statement: 

If the first statement is true, its code will execute. Also, if the second statement is true, its code will execute.

if statement:
elif statment:

The second block will only execute here if the first one did not, and the second check is true.

if statement:
else:

The first statement will execute if it is true, while the second will execute if the first is false.

Comments