May I ask anyone to explain assertion in your words? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

May I ask anyone to explain assertion in your words?

22nd Dec 2015, 2:41 AM
jurtly - avatar
11 Answers
+ 15
for example, you have x and y. if you changed their values a lot of times and you don't know that x == y , you should use assert. If the statement after assert is false, the program raises an exception and stops.
4th Jan 2016, 10:48 PM
Mihai Dancaescu
Mihai Dancaescu - avatar
+ 9
An assertion is used to test if a statement is true. For example: x = 4 assert x == 4 blablabla In that case, the assertion is true so the code will keep on running. If the assertion was false, the computer would have raised an error and ended the program. Basically, this is a "Do It Yourself" error, you can make whatever error you want with it :)
22nd Dec 2015, 1:53 PM
Vacuum - avatar
+ 6
Assertions are normally used as like a check thing. Mainly in debugging, since you have to activate it. Say a method calls another method and you know that it's data should be some value you can write an assertion check at that point and if something breaks in your project by switching on Assertions you can pin point errors down.
13th Apr 2016, 4:10 PM
Xaeros - avatar
+ 3
An assertion is a ode statement that makes assumptions in a coding block explicit. Assertions can also be used to debug a program.
23rd Jan 2016, 3:52 PM
Michael O. Jacob
Michael O. Jacob - avatar
+ 2
if makes choice for program control flow where as assertion is just used for checking or testing values. its not used to make choice for particular functionality. and, we assume that, they are true whenever executed in program. If not, raise an exception. Hope this helps.
27th Mar 2016, 5:37 AM
Mohan Pawar
Mohan Pawar - avatar
+ 2
Assertion is used to check logic where you know the outcome of the code snippet should be True. If something changes and it evaluates false then you are notified with an error.
4th Jul 2016, 10:24 AM
Richard Smith
Richard Smith - avatar
+ 2
is like if you want to check something. for example if I have 2 numbers 3 and 4 and i whant to see which is bigger I check if the first is bigger than the second one,if i get an error it means my statement(assert num1>num2) is not true if I don't it means my statement is true
12th Jul 2016, 12:38 PM
+ 1
as mentioned in the lesson it is a sanity check , checks if the code is True or False and helpful when using variables
22nd Jun 2016, 3:05 PM
xzys - avatar
+ 1
Assertions help us to make sure our program works as expected. Imagine you make a large code to solve some mathematical problem. Supouse you have a solved problem that you can use to test your code, but when you run it the result it gives is not right. You may insert assertions along the code to verify partial results to see where you are making the mistake and then fix it.
30th Sep 2016, 2:35 PM
+ 1
You need to understand the difference between assertion with try/except. Assertions should be used to check something that should never happen while an exception should be used to check something that might happen (something in which you don't have control like user input). Exceptions address the robustness of your application while assertions address its correctness. NOTE: The thumb rule is that use assertions when you are trying to catch your own errors and exceptions when trying to catch other people's errors.
16th Nov 2018, 3:33 AM
Tamoghna Saha
its something like if but raise an assertion error if the condition is false...
10th Aug 2016, 4:48 AM
Samiul Sk
Samiul Sk - avatar