Else, If, and Elif. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Else, If, and Elif.

Why did Python give the option of Putting Else: If, when you could just use the Elif instruction? Is there something extra you could do with it? E.G If 1 + 1 = 2: print("One plus one is two.") Else: If 1+1 != 2: print("One plus one doesn't equal two.") Instead of-- If 1 + 1 = 2: print("One plus one is two.") Elif 1+1 != 2: print("One plus one doesn't equal two.") Many Thanks. ^^

31st Dec 2016, 10:20 AM
ₚsʸ
ₚsʸ - avatar
3 Answers
+ 2
if you want to check multiple conditions and none of them are true, else is used printing "input was not correct" or something like that.
31st Dec 2016, 10:25 AM
Uran Kajtazaj
Uran Kajtazaj - avatar
+ 2
x = 2 if x == 1: doSomething() elif x == 2: doSomethingElse() else: print(x) if x == 1: doSomething() else: elif x == 2: doSomethingElse() print(x) Those 2 examples are different. The second case allows you to decide for something to happen regardless of the secondary condition.
31st Dec 2016, 10:35 AM
Dao
Dao - avatar
+ 1
else = not all . elif = an other test .
31st Dec 2016, 11:31 AM
Yassine Grib
Yassine Grib - avatar