Let's learn about the main function written in any programming language and what it does, and in Python programming language how can we use the main function and how to control its execution.
The main is a function in most of the programming languages and this is written as ‘main () ‘, when user wants to call it. This is the function created with the intention to initialize the execution of program. This function is necessary to use in c, c++ and java programming languages and on the other hand it is not necessary to use this function in python programming language.
In programming languages other than the python programming languages, the compiler looks out for the main () function during the compilation of the program. Basically main functions are supposed to be executed first while the program execution and user’s programmers are supposed to write the initial program execution instructions in this main function itself.
It is not necessary to use the main function in python because python is a High Level General Purpose Programming Language (HLL), but if programmer wants to use main function they can use it and also programmers can customize this main function and point it to another function and force the execution.
In the previous blogs, we learnt about the libraries in python, working of libraries and how their execution takes place and also learnt how we can create a library/module, but here comes a very important role of the main and name statement in python.
#this is module file module.py
def printer():
print("This is a function from library" )
return None
printer()
and
#this is module file
import module
This is the output. In both of the cases we get same we are getting the same output twice in case, we directly run the code file and getting
the result at once in module execution.
Basically, whenever we create a library or
module from scratch and inside it, if we create a function, return something, and
finally call the function in module then what happens here is, that the called function will be executed and the same happens in case of the
modules import.
When we import the library to use in another program at that
time whenever that same program is tested, it is observed that the functions
that are called in that previous module we used, gets executed automatically and
shows the return results. Even if programmers don’t want it to run it is
getting executed and so this is unwanted action and to solve this python gives
a statement to fix this issue.
We use the statement,
If __name__ == “__main__”:
printer()
After using the above statement in the main module or library file, whenever the code is executed, we see that the result is only displayed once that is after calling the function module.printer(), instead of showing result twice and in case of module execution result is displayed if it is run directly and not from import, that is, modify module file as:
#this is module file
def printer():
print("This is a function from library" )
return None
if __name__ == "__main__":
printer()
and keep the code file as it is, and what we see in the result is what
we are expecting.
So name and main statements are very important and
need to be used wherever it is needed.
Let's talk about the Exception Handling in Python. We come across programs which may throw error and do not execute further. But we want the program to be executed fully without being stopped in between. So, here comes the role of Exception Handling.
Exception Handling is used to avoid errors and continue executing the program. Try and Except keywords are used in exception handling.
As the word 'try' suggests, that the code inside the try statement should be executed first and if it fails, the it should execute the except statement.
Let's understand this with an example:
Case 1- Before try and except statement:
Code:
print('Enter num1')
num1=input()
print('Enter num2')
num2=input()
print('This line is very important')
Output:![]() |
Click on image to view |
Case 2- After applying try and except statement:
Code:
print('Enter num1')
num1=input()
print('Enter num2')
num2=input()
try:
print('The sum of num1 and num2 is',int(num1)+int(num2))
except:
print('Invalid Literal')
print('This line is very important')
Output:
![]() |
Click on image to view |
As you can observe in Case-1, that when we do not use try and except statement, it gives us an error and the execution stops there, whereas in Case-2, we use try and except statement, so firstly the try statement is executed and if it fails, the except statement is executed and the code below it, is also executed. So, this is the use of try and except statements.
Use Case:
Exception Handling is generally used when we work with internet connectivity. Suppose if someone has low internet connectivity or no internet connection, and he/she is downloading something. By using try and except statement, we handle the program by showing 'No Internet' as output, instead of hampering the download process and hence the program is executed successfully.
0 Comments
If you have any doubts, please let me know.