May I ask anyone to explain assertion in your words?
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.
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 :)
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.
An assertion is a ode statement that makes assumptions in a coding block explicit. Assertions can also be used to debug a program.
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.
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.
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
as mentioned in the lesson it is a sanity check , checks if the code is True or False and helpful when using variables
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.
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.
its something like if but raise an assertion error if the condition is false...