What are the differences between assertion and if? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

What are the differences between assertion and if?

6th Jul 2016, 12:40 AM
Henky Chan
Henky Chan - avatar
3 ответов
+ 6
An if statement will not raise an exception when the condition is false. Assertion will raise an assertion error when the condition is false. An assertion exception will stop the program unless the exception is handled by a try-except block.
6th Jul 2016, 5:48 AM
Gershon Fosu
Gershon Fosu - avatar
+ 4
look at assertions as a method for development. so you can build various checkpoints across the code to ensure that variables/arguments are meeting the expected limitations (e.g. you know it must be number not letter, it must be positive , etc...). Then once you're done with development you can globally turnoff all the assertions. In case you will use IF statements (instead of assertions) for this, you will need to clean up the code and possibly introduce some errors.
13th Jul 2016, 9:01 AM
both are similar only difference is that unlike if statement, assertion raise an assertion error if condition is false.
17th Jul 2016, 3:42 AM
anmol limje
anmol limje - avatar