+ 1

What is the difference between ‘if’, ‘else’, and ‘elif’?

So in Python3 there’s the ‘if’, ‘else’ and ‘elif’ codes and what do they exactly do and what is the difference between them? I’m a bit of a slow learner and I can’t quite figure out what they exacty do. Also what do they mean when there’s a ‘not’ at the end of them? Like ‘if not’, ‘else not’ and ‘elif not’.

23rd Jun 2018, 3:28 AM
detectiveG
detectiveG - avatar
7 Answers
+ 5
At some times you want to execute certain command if some conditions satisfies otherwise you want to skip that part. For example if you want to perform division, then it can only be evaluate if denominator is not 0, then here come the if statement, using if-statement you can this. if condition1: """ commands to execute when if condition satisfies""" then there comes the conditions where you needs some more flexibility, as if my if-condition fails then i want to check another condition, and if that satisfies then execute some commands, here comes the role of 'elif'( shorthand of 'else if') elif condition2: """ command to execute when if statement is false and this elif statement is true""" you can use multiple elif-statement, from will execute any code associated inside an elif block only when, it's if-condition as well as all elif-conditions written above is false. example: elif condition3: """ this block will execute when condition1 and condition2 both are false and condition3 is true """
23rd Jun 2018, 3:52 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 5
now situation comes when you want that if any of your condition doesn't satisfies then certain command may have to execute. then role of else comes up else: """ this block will execute when you if-condition as well as each elif-conditions is false""" now, what's role of 'not' in those statements, you must know the condition you write are a boolean condition which gives 'True' or 'False' when condition satisfies and doesn't satisfies respectively. so not is just a boolean negation, which means boolean condition return true when condition is not true (means false) and vice-versa example: if not condition: """this block will execute when condition is not true(is false)""" I hope this helps and answers all your questions.
23rd Jun 2018, 3:58 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 1
'if not' means the statement inside the condition is excuted when the condition is false.
23rd Jun 2018, 3:43 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 1
i like your post
23rd Jun 2018, 6:04 AM
Tanvir Ahmed
Tanvir Ahmed - avatar
+ 1
in simple example . 1-if (your condition){} if true then code execute if false check next condition 2-elif/elsif (means if 1st condition is false then check this condition){} if again false check next condition 3- else ( if 2nd codition is false then execute this statment,){} code will execute this time, because "else" work when "if" and "elif"/"elseif" are false.."else" is like "then" in English word ex. if. (pizza is not available ) Then. (eat pasta) .....
23rd Jun 2018, 6:28 AM
Avinash Nadkar
Avinash Nadkar - avatar
0
Thank you guys so much! 😀
23rd Jun 2018, 4:01 AM
detectiveG
detectiveG - avatar