Python

Python main function Example

Hello, in this tutorial, we will learn about the main function in the Python programming language.

1. Introduction

The main function in python programming is the starting point of a program. The python interpreter runs the code sequentially. The main function in python programming gives the program a starting point for the execution and is useful to better understand how the program works. A simple python program looks like this –

Snippet

1
2
3
4
5
6
7
8
def main():
    print("Hello python programming.")
# declaring the call function
# this will call the main function and print the console log.
if __name__ == '__main__':
    main()

However, do remember it is not a compulsion to have the main function in python.

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the python from this link.

2. Python main function Example

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of their choice.

2.1 Main function

To understand the main function we can use the following code snippet.

Main function

01
02
03
04
05
06
07
08
09
10
11
12
13
# python main function
# defining the main function
def main():
    print("Hello, python programming.")
# declaring the call function
# this will call the main function and print the console log.
if __name__ == '__main__':
    main()
print("Good morning")

If everything goes well the following output will be shown in the IDE console.

1
2
Hello, python programming.
Good morning

2.2 Calling other functions from the Main function

To understand the calling of other functions from the main function we can use the following code snippet.

Calling other functions from the Main function

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# python main function
print("Calling other functions from the main() function\n")
def split(data):
    return str.split(data)
def companies():
    print("Getting the companies data")
    company = "ABC is the best company"
    print("Companies data is returned")
    return company
def main():
    data = companies()
    print(data)
    print("\n")
    newdata = split(data)
    print(newdata)
if __name__ == "__main__":
    main()
print("\nDone")

If everything goes well the following output will be shown in the IDE console.

01
02
03
04
05
06
07
08
09
10
Calling other functions from the main() function
Getting the companies data
Companies data is returned
ABC is the best company
['ABC', 'is', 'the', 'best', 'company']
Done

2.3 Main function as Python module

Let us understand this with the help of the following code snippet.

Main function as Python module

1
2
3
4
5
6
7
8
# considering code as an imported module
# here the AddValues.py is imported as a module
import AddValues
# calling the add(a, b) function written in the AddValues.py
AddValues.main(5, 5)
print("\nDone")

If everything goes well the following output will be shown in the IDE console.

1
2
3
The sum of a and b is = 10
Done

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned about the

  • main function introduction in python programming
  • Practical examples of using main function in python programming

You can download the sample class from the Downloads section.

4. Download the Project

This was an example of a main function in python programming.

Download
You can download the full source code of this example here: Python main function Example

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button