Why is this happening

why do we need this even we have the if, elif, else conditional statement.

Hello @kumarpratikkant,
if, elif and else is concept of looping statements whereas try, except and finally keyword is the concept of exception handling which is mostly used to handle errors.
for example you are designing a division calculator taking two inputs from the user a and b and checking if a/b is greater then 1 you output the result else you print “result less than one”.
Now the user enters a =3 and b=0
so while going in if statement runtime error will occur and else block will not be executed.
ifelif

Now you use exception handling which on errors in try block will run the except block. the statements under finally block is always executed.
except

So here there is ZeroDivisionError in try block so except block is executed.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.