What is the use of “assert” keyword in Python?

0 votes
I have been reading some source code and in several places I have seen the usage of assert.

What does it mean exactly? What is its usage?
Oct 15, 2018 in Python by findingbugs
• 4,750 points
40 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can try the following in a interactive shell:

>>> assert 5 > 2
>>> assert 2 > 5
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
builtins.AssertionError:

The first statement does nothing, while the second raises an exception. This is the first hint: asserts are useful to check conditions that should be true in a given position of your code (usually, the beginning (preconditions) and the end of a function (postconditions)).

Asserts are actually highly tied to programming by contract, which is a very useful engineering practice

answered Oct 15, 2018 by Priyaj
• 56,140 points

Related Questions In Python

+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
1,088 views
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
47 views
0 votes
0 answers

what is the use of args and kwargs in python?

can you an example for each? READ MORE

May 10 in Python by Waseem
• 2,870 points
15 views
0 votes
0 answers

What is the use of get requests in python?

What are the other http requests in ...READ MORE

May 30 in Python by Waseem
• 2,870 points
10 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
69 views
0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 56,140 points
56 views
0 votes
1 answer

What is the fucntion of mkdir -p in Python

mkdir -p gives you an error if the ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 56,140 points
53 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.