+ 115

Assertions

So to clarify, assertions in python are pretty much made for your use only and they are placed around the program to check that an integer, string, float, etc., is what you want it to be, and to notify you when it isn’t.

28th May 2018, 1:35 PM
...
... - avatar
47 odpowiedzi
+ 70
yes it is. well simplified. but there are other uses as well when you want to run a segment if and only if the assertion condition you put is true. in short it can be used as conditional purposes too. if I am wrong please correct me.
6th Jun 2018, 8:24 AM
Mohammad Hasibul Haque
Mohammad Hasibul Haque - avatar
+ 68
Real life usage of Assertion and try/except in program. def d2w(n): dicts = { 1 : "One", 2 : "Two", 3 : "Three", 4 : "Four", 5 : "Five", 6 : "Six", 7 : "Seven" } return dicts[n] while True: ui = input("Enter a number: ") try: assert 0 < int(ui) <= 7 print(d2w(int(ui))) except AssertionError: print("Number should be between 1 and 7") except ValueError: try: assert ui.lower() == "stop" print("Bye!") break except AssertionError: print("U didn't Entered INT or Stop") # This program takes number as input converter it to words and stops when user input "stop". # While writing this code i realised that if-else conditions can be replaced with Assertion and try/except at least for checking user input. ***You query or suggestions is highly appreciated.
27th Dec 2021, 4:43 PM
Alim Ansari
Alim Ansari - avatar
+ 18
Jevon I think the purpose of assertion is to keep the code running. Otherwise, the code will terminate and nothing else would be done after an exception. Exceptions don't have to be the end of the code. Hope this helps
31st Aug 2021, 10:48 PM
chan Sasquatch
+ 17
def age(age): assert age >= 0, "correct your age" print(f"your age is {age}") age(-8)
4th Mar 2022, 10:00 AM
yonas
yonas - avatar
+ 8
Aditya yes, there was some comment, I had replied to it the same that it is not any question. instead it is a reader's view on specific topic. No worries, it seems that comment is taken down
12th Apr 2021, 4:58 AM
Gayatri C
Gayatri C - avatar
+ 6
Well, personally i dont feel assertion will really be useful because i've been using VS Code IDE until now, when it comes to invalid type of int, float, string,etc. It usually comes right down inside terminal and tells us which line is problematic. So i dont need to clarify with assertion because it's not neccessary.
20th Aug 2021, 12:16 PM
Jevon Harry
Jevon Harry - avatar
+ 6
I think assertions in python can be useful for debugging our programs. It halts the program as soon as the return statemet is false. It is also possible to handle this errors which make them more useful.
4th Mar 2022, 6:30 AM
Musa Hodman
Musa Hodman - avatar
+ 5
Its not understanding for me
7th Jun 2021, 9:27 AM
S.Sathiyaraj
S.Sathiyaraj - avatar
+ 3
Fili Siegl "The lower() method returns a string where all characters are lower case." In the case, put on ui variable.
14th Feb 2022, 9:28 AM
L Eduardo Ferreira
L Eduardo Ferreira - avatar
+ 3
Thank you!
14th Feb 2022, 9:29 AM
Fili Siegl
Fili Siegl - avatar
+ 2
yes
28th May 2018, 3:47 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
6th Jun 2018, 9:46 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 2
Well explained! Now i got it!
15th Jun 2021, 6:59 AM
Alexandr Iunevici
Alexandr Iunevici - avatar
+ 2
Using assertion bring us some sort of proof of what we are doing and this help wen you are starting with programming new projects or even understanding the step by step the programm do
11th Aug 2021, 12:08 AM
pablo Salaza
pablo Salaza - avatar
+ 2
So if I understood correctly, Assertions are nothing but the indicators which indicates/inform us when a particular condition is given but not being followed.Pls correct me if I am wrong....
4th Sep 2021, 9:42 PM
samoresh saha
samoresh saha - avatar
+ 2
Of course, but it reduce the utility.
2nd Jan 2022, 12:13 PM
L Eduardo Ferreira
L Eduardo Ferreira - avatar
+ 1
yes
16th Mar 2021, 8:48 PM
AmirhoseinAfkari
AmirhoseinAfkari - avatar
+ 1
I think we use assertion to takes values logical
10th May 2022, 8:16 PM
Fethi Rabahallah
+ 1
I see the assert statement as a security during the development of a program. It allows to stop a program as soon as a condition is not valid without going further in the execution. Tell me if I'm wrong.
15th May 2022, 12:44 PM
Radouane
Radouane - avatar
+ 1
Is assertion used for test by software tester??
31st Oct 2022, 6:39 PM
OLFA DGHIM