What's the use of assertion in Python | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 3

What's the use of assertion in Python

15th Jul 2019, 10:41 PM
Mary Darlington
Mary Darlington - avatar
3 ответов
+ 4
Possibly because in the event of a false condition it will terminate a program with an AssertionError but honestly I don't use it that frequently.
15th Jul 2019, 11:53 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
Assert can be used to debug or prevent wrong input from user. Like assert (age<0), 'incorrect age' Also, can be used for debugging and knowing exactly where the values are wrongly compared. Like if you are in a while loop, and there are multiple layers of while and if else statements, and code goes wrong, you can put assert as checks and find out exactly when the code is going wrong.
24th Jul 2019, 7:56 PM
ketan - avatar
For example, we have a statement that must be provided when writing a program. In such cases, we can quickly find errors when there is an error by using the assert method. Thanks to this method in long and complex programs, we can immediately catch the error and correct it. Otherwise, we have to examine all our code thoroughly to debug it. We can easily disable it at any time. Let's say we used the if method; And we don't want the program to run faster and take up too much space. Then we will have to delete all our entries one by one, and while deleting them, there is a danger of accidentally deleting if else codes that we use for other purposes. That's why the assert method makes our job much easier in debugging.
19th Oct 2021, 5:39 PM
Lütfi Burak ATMACA
Lütfi Burak ATMACA - avatar