Can anyone tell me what is the use of assert? (in the simplest way possible) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone tell me what is the use of assert? (in the simplest way possible)

Please dont use big words. And if you can, use an example.

27th Aug 2021, 1:06 PM
Ervan
Ervan - avatar
2 Answers
+ 4
In some cases, it is simply not possible for your code to continue execution if a particular condition is not satisfied. In these situations, you can trigger an assertion in your code to end code execution and to provide an opportunity to debug the cause of the absent or invalid value. An assertion is a runtime check that a Boolean condition definitely evaluates to true. Literally put, an assertion ā€œassertsā€ that a condition is true. You use an assertion to make sure that an essential condition is satisfied before executing any further code. If the condition evaluates to true, code execution continues as usual; if the condition evaluates to false, code execution ends, and your app is terminated. for eg, salary = -50 assert salary > 0 , "Negative salary is invalid" Now this will stop the execution of your program because salary is less than 0 , assert will help you to debug the code most!
27th Aug 2021, 2:57 PM
Abhiyantā
Abhiyantā - avatar
0
Aaa i see thanks
27th Aug 2021, 1:24 PM
Ervan
Ervan - avatar