Electron microscopy
 
Assert and Assertion in Python
- Integrated Circuits -
- An Online Book -
Integrated Circuits                                                                                   http://www.globalsino.com/ICs/        


Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

=================================================================================

Assertion is a programming concept used while writing a code where the user declares a condition to be true using assert statement prior to running the module. The assertions assert or guarantee a condition to be true.  If the condition is True, the control simply moves to the next line of code. In case if it is False the program stops running and returns AssertionError Exception. They are primarily used during the development phase to check assumptions about the program:

  • Spotting Bugs Early: Assertions help catch bugs early in the development process. They allow you to validate certain conditions during runtime and raise an error if those conditions are not met. This can help identify issues before they propagate further into the codebase. 

  • Clarity on Error Locations: When an assertion fails, it raises an AssertionError with a specific message. This helps in pinpointing exactly where in the codebase the issue occurred, aiding in debugging. 

  • Supplement to Testing: While testing is crucial for verifying the correctness of a program, assertions serve as an additional layer of defense. They provide a way to enforce conditions that must hold true during execution. 

  • Checking Input Data: Assertions can be used to validate input data provided by users. If the input doesn't meet certain criteria (e.g., wrong data type), an assertion can raise an error, indicating that the input is invalid. 

  • Checking Data Structures: Assertions can ensure that invariants (conditions that should always be true) on data structures are maintained. For example, ensuring that a list remains sorted or that a dictionary always has certain keys. 

  • Validating Return Values: Assertions can check constraints on return values from functions or methods. This ensures that the returned values meet certain criteria before they are used further in the program. 

  • Enforcing Procedure Constraints: Assertions can be used to enforce constraints within procedures. For example, ensuring that a function doesn't return duplicate values in a list or that certain conditions are met before executing a block of code. 

The assert statement takes an expression and evaluates it. If the expression evaluates to True, the program continues execution as normal. However, if the expression evaluates to False, the assert statement raises an AssertionError exception, halting the program's execution, for instance, the code:

   Output:

============================================

Add elbow, curve, straight line connectors into an existing PowerPoint file: code:
         Create a pptx file and save and open it

Output:
 Create a pptx file and save and open it

 

         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         

 

 

 

 

 

 

 

 

 

 

=================================================================================