{"id":80208,"date":"2021-02-03T09:00:24","date_gmt":"2021-02-03T03:30:24","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80208"},"modified":"2021-02-03T09:00:24","modified_gmt":"2021-02-03T03:30:24","slug":"assertion-in-python","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/","title":{"rendered":"Assertion in Python with Examples"},"content":{"rendered":"<p>Assertion is a different spell for Python than what we generally assume it to be, it&#8217;s different yet simple but not worthy enough to use everywhere.<\/p>\n<p>All these complicated lines will surely be understood once you read the article.<\/p>\n<h2>What is Assertion in Python?<\/h2>\n<p>Typically Assertion in Python or a Python Assert Statement is one that asserts (or tests the trueness of) a condition in your code. This is also a Boolean expression that confirms the Boolean output of a condition.<\/p>\n<p>Simply the boolean statement checks the conditions applied by the user and then returns true or False. If it returns true, the program does nothing and moves to the next line of code and executes it.<\/p>\n<p>However, if it&#8217;s false, the program stops and displays an error. It is a debugging code hence saves a carbon copy of the indented code with the main code as well.<\/p>\n<h3>Why is Python Assertion Used?<\/h3>\n<p>It is mostly used to debug the files in the code and match them in the library as well.<\/p>\n<p>Assertions basically display the out of conduct rules in a particular code and displays error. It deals a lot with python errors which are dynamic in nature.<\/p>\n<h3>Where is Python Assertion Used?<\/h3>\n<ul>\n<li>As a debugger in python code.<\/li>\n<li>Helps the coder in locating errors.<\/li>\n<li>Helps in copying file indexes in the library.<\/li>\n<li>It helps in the indirect indentation of input.<\/li>\n<li>Provides a shallow copy of the indented code.<\/li>\n<li>It removes the bugs and saves the file as original in the main source code with the help of the library.<\/li>\n<li>As a debugger, it generates a carbon code while interpreting the main code, which is helpful in crossing the codes.<\/li>\n<\/ul>\n<p><b>Syntax of Python Assert<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">assert condition, error_message(optional)\n<\/pre>\n<p>Here,<\/p>\n<p><strong>1. condition(): <\/strong>The absolute boolean condition returning true or false.<\/p>\n<p><strong>2. error_message:<\/strong> This is a general message which is displayed while operating any code which has bugs.<\/p>\n<p><strong>3. Returns:<\/strong> Consequently, it Returns AssertionError, in case the condition evaluates to false along with the error message which when provided.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/01\/python-assert-statement-normal-image.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80214\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/01\/python-assert-statement-normal-image.jpg\" alt=\"python assert statement\" width=\"561\" height=\"368\" \/><\/a><\/p>\n<h3>Python Assert() Example and Syntax<\/h3>\n<p>A python assert statement works on some particular conditions of the code. It only evaluates the code which has a true output, the false output codes are treated as syntax errors.<\/p>\n<p>Similarly, If the condition is false, assert stops the program and gives an AssertionError as the output.<\/p>\n<p>Python Assert Statement is how we always want to see true value.<\/p>\n<p><strong>Python Assert Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var2=int(input(\"enter number\"))\nassert var==var2\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">enter number 67<br \/>\nenter number 78<br \/>\nTraceback (most recent call last):<\/div>\n<h3>AssertionError in Python<\/h3>\n<p>While generating the output of some codes, the assertion error is displayed which means that the bugs in the code are caught.<\/p>\n<h4>Using Python Assert without Error Message<\/h4>\n<p>This doesn&#8217;t allow the user to check which type of error is there in the code. The coder can, however, grant access to the same to the user.<\/p>\n<p><strong>Example of Assert without Error Message:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mark1= int(input(\"enter 1\"))\n\nmark2 = int(input(\"enter 2\"))\n\nmark3 = int(input(\"enter 3\"))\n\na=(mark1+mark2+mark3)\nb=3\nc=a\/b\nprint(c)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">enter 1 90<br \/>\nenter 2 98<br \/>\nenter 3 97<br \/>\n95.0<br \/>\n&gt;&gt;&gt;<\/div>\n<h4>Using Python Assert with an Error Message<\/h4>\n<p>This displays an error if the code goes wrong while processing.<\/p>\n<p><strong>Example of Python Assert with an Error Message:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def thisistech():\n    l= len(thisistech)\n    s= sum(thisistech)\n    return s\/l\n\nprint(\"your data has the average of: \")\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">your data has the average of:<br \/>\n&gt;&gt;&gt; thisistech()<br \/>\nTraceback (most recent call last):<\/div>\n<h4>Python Assert Syntax<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">assert &lt;thecondition&gt;\nassert &lt;thecondition&gt;, &lt;error message&gt;\n<\/pre>\n<h4>Python Assert Example<\/h4>\n<p>Now advisably, a divide function to divide two numbers is the most appropriate basic learner code, but, What happens when we initially try to divide a number by 0? The answer is undefined.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">p=int(input(\"enter a number\"))\nq=int(input(\"enter a number\"))\na=p\/q\nprint(a)\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">enter a number 6<br \/>\nenter a number 8<br \/>\n0.75<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>Using an Error Message in the Python Assert<\/h3>\n<p>Instead of continuously throwing six red lines at the developer, Python may want to display something more sophisticated and less hostile for sure. And for this, we are giving an error message to the assert statement.<\/p>\n<p><strong>Handling the AssertionError in Python:<\/strong> Sometimes the user is aware of the mistake in the code, this option gives a chance to the user to rectify it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def thisistech(a,b):\n     assert b!=0, \"this value raises an exception\"\n     return a\/b\nthisistech(2,0)\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Error<\/div>\n<h4><strong>Python Assert Applications<\/strong><\/h4>\n<p>It has extensive usability in testing and Quality assurance roles in any development domain during the run time for any integral value in code.<\/p>\n<p>Different types of assertions used depend upon the application which is used in the code and is executed and it also depends on the attributes.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">l=34,56,78,23,11,2\nfor j in l: \n    assert j &gt;= 26\n    print(\"wow\")\n    print (str(j)) \n<\/pre>\n<p><strong>Output :<\/strong><\/p>\n<div class=\"code-output\">wow<br \/>\n34<br \/>\nwow<br \/>\n56<br \/>\nwow<br \/>\n78<br \/>\nTraceback (most recent call last):<\/div>\n<h2>Conclusion<\/h2>\n<p>Having read so far, as a coder now you are capable of knowing when and how to assert things in python.<\/p>\n<p>We\u2019ve also read how assert statements can possibly give different errors in different code executions.<\/p>\n<p>Run time was a major focus on the execution of the code in a python program, hence the coder needs to see that the code should be dynamic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assertion is a different spell for Python than what we generally assume it to be, it&#8217;s different yet simple but not worthy enough to use everywhere. All these complicated lines will surely be understood&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80215,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3368,3369,3370],"class_list":["post-80208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-assertion-in-python","tag-python-assert-statement","tag-python-assertionerror"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Assertion in Python with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what is assertion in Python with examples. Python provides assert statements to check if given logical expression is true or false.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Assertion in Python with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what is assertion in Python with examples. Python provides assert statements to check if given logical expression is true or false.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-03T03:30:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Assert-Statement-TV.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Assertion in Python with Examples - TechVidvan","description":"Learn what is assertion in Python with examples. Python provides assert statements to check if given logical expression is true or false.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Assertion in Python with Examples - TechVidvan","og_description":"Learn what is assertion in Python with examples. Python provides assert statements to check if given logical expression is true or false.","og_url":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-02-03T03:30:24+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Assert-Statement-TV.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Assertion in Python with Examples","datePublished":"2021-02-03T03:30:24+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/"},"wordCount":783,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Assert-Statement-TV.jpg","keywords":["Assertion in Python","Python Assert Statement","python assertionerror"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/","url":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/","name":"Assertion in Python with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Assert-Statement-TV.jpg","datePublished":"2021-02-03T03:30:24+00:00","description":"Learn what is assertion in Python with examples. Python provides assert statements to check if given logical expression is true or false.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Assert-Statement-TV.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Assert-Statement-TV.jpg","width":1200,"height":628,"caption":"Assertion in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Assertion in Python with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80208"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80208\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80215"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}