Constructor or main function in python

 main() function:

  • By default, every program starts its execution from the main() function. In Python including a main() function is not mandatory.
  • It can structure our Python in a logical way that puts components of the program the most in one program's important function.
  • We can get the name of the current module executing by using the built-in variable name (2 underscores before and after of name).
  • Most non-python programmers are having the habit of writing a main()function where the important and starter code of programs are written.
  • In Python, we can also create main() and call it by checking name to main and then call any function, in this case main().

Example program:

  1. def area(length,breadth):

        return length*breadth

    def main():

        l=int(input("Enter length: "))

        b=int(input("Enter length: "))

        a=area(l,b)

        print("Area of rectangle = ",a)

    if __name__=='__main__':

        main()

        print("__name__ name of value is : ",__name__)

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me