The function calls
exit(1) are used to reveal the status of the termination of a Python program. The call
exit(0) indicates successful execution of a program whereas
exit(1) indicates some issue/error occurred while executing a program.
What is the Exit Code?
Quote = 'Dream without fear' print(Quote)
let’s run this code:
Dream without fear Process finished with exit code 0
From the code snippet, we can see the program was executed successfully since it returned the result and the process was finished with exit code 0.
Now Let’s do a mistake intentionally:
Quote = 'Dream without fear' print(Quot)
Traceback (most recent call last): File "G:/code_practice/exit.py", line 2, in <module> print(Quot) NameError: name 'Quot' is not defined Process finished with exit code 1
If someone notices carefully, the program was unable to deliver a result due to the name error that occurred for the misspelling of the variable ‘
Quote’. Hence the process was finished with exit code 1 implying that there were some issues in running the program.
? Remember: Therefore, a non-zero exit code is always treated as an abnormal exit. The non-zero exit code can be anything ranging from 1-255 indicating unsuccessful execution of a program. 0 is the default exit code used by python treated as the successful execution of a program. Any value outside the range of 0-255 is treated as modulo 256.
That is for example if we execute a statement
exit(10003) then it will execute
10003%256=19. This convention is applicable in most of the platforms but the meaning of the status codes may vary from one to another.
How to Terminate a Python Script?
When we run a program usually the interpreter exits from the program when it reaches the end of the script. There are many occasions when we need to terminate the program before the interpreter does so. In this sort of situation functions like
exit(1) allow us to come out of the loop automatically.
Terminating a Python Script with exit(0)
Let’s have a look at a practical example:
weights = [34,23,60,95,30,45] for weight in weights: if weight>80: print('This person is more than 80 kg.') exit(0)
34 23 60 This person is more than 80 kg. Process finished with exit code 0
The code above stopped execution when it had reached the condition (
weight>80) and the last 2 numbers of the list are not printed because of the
exit(0) function. This
exit(0) function automatically terminated the program after a certain condition (
weight>80) had been met.
Terminating a Python Script with exit(1)
weights = [34,23,'thirty',60,95,45] for weight in weights: if type(weight) != int : print('This is not an integer value.') exit(1) print(weight)
34 23 This is not an integer value. Process finished with exit code 1
For the non-integer value inside the list, the code stopped execution before completing the whole for loop. The process finished with
exit(1) as instructed in the code snippet. It makes the thing easier for the developer to choose an exit point for a program if there is any confusion of raising an error.