How to Use Python Main Functions
“I’ve seen a few tutorials that use
main() in Python. What does it mean?” That’s a great question. The
main() function may seem trivial when it’s not actually necessary.
Coding is all about being concise; you’re probably curious why developers want to add in more lines of code into a program. The
main() function may add an extra few lines of code into your Python program, but it does serve an important purpose.
In this guide, we’re going to talk about what the
main() function is, why it is useful, and how you can declare a main function in your code. Let’s get started.
What Is the main() Function?
Main is a special function that allows you to introduce more logic into the structure of your Python programs.
In Python, you can call a function at the bottom of your program and it will run. Consider the following example:
def cookies(): print("Cookies are delicious!") print("I like cookies.") cookies()
Let’s run our code and see what happens:
I like cookies. Cookies are delicious.
You can see that the statement: “I like cookies.” is printed out before “Cookies are delicious!” This is because we don’t call the
cookies() method, which stores the print statement for “Cookies are delicious!”, until the end of our code.
While this code works, it’s quite difficult to read. In other programming languages like C++ and Java, all the main code for a program is contained within a main function. It’s a good way of breaking down all of the code in your programs. People who are new to Python but who know other programming languages like C++ will thank you for adding in a
How to Create a Main Function
There are two parts of a Python main function. The first part is the actual
main() function which stores the code for our main program. Let’s call our
cookies() function and print out the statement: “I like cookies.” from within a
def cookies(): print("Cookies are delicious!") def main(): print("I like cookies.") cookies()
Finally, let’s call our function at the bottom of our program:
We can now run our code:
Cookies are delicious! I like cookies!
Our code returns the same as earlier. If you take a look at our code, you can see that it’s easier to read. While there may be more lines of code – and an extra function – it’s clear exactly what is happening in our program:
Cookies()is a function that prints “Cookies are delicious.”
- When our program is run, our two functions,
main(), are defined.
- Then we call the
- “I like cookies.” is printed to the console.
cookies()function is then called, printing “Cookies are delicious!” to the console.
Our code is not only cleaner, but it’s also more logical. If you come from another language like Java, you’ll know how useful this is.
The Value of __name__
Before we talk about the __name__ and __main__ statements that are often used together with a main function, we’ve got to talk about __name__. __name__ stores the name of a program.
When you run a file directly, __name__ is equal to
__main__. Consider this file called print_name.py:
We can run our code like this:
Our code returns:
Let’s say that we were to import this as a module into a file called main.py. Let’s create a file called main.py and import the print_name module:
Let’s run name.py:
Our code returns:
The code within our print_name.py file is executed because we imported it as a module in our main program. The print_name.py file prints __name__ to the console. Because we imported print_name as a module, the value of __name__ is print_name.
Python __name__ and __main__
You’ve probably seen something like this in a Python program with a main function:
if __name__ == "__main__":
What does this mean? That’s another good question. In Python, any variable that starts and ends with
__ is a special variable. These are reserved values that have specific purposes in a Python program.
__main__ refers to the scope where your code will execute. When you run a Python file directly, the value of “__name__” is set to “__main__”. However, when you run a file as a module, the value of “__name__” is not “__main__”; it is set to the name of the module.
This means that the line of code above will evaluate to True when our program is run, but only if we run our program directly.
If we reference our file as a module, the contents of the “if” conditional statement will not be executed. Let’s demonstrate how this works with an example.
How to Use __name__ and __main__
Let’s create a new Python script called username.py. In this file, we’re going to ask a user to select a username and check if it is more than five characters long. If the username is not longer than five characters, the user will be asked to choose a new username.
We’ll start by defining a global variable to store our user’s username:
username = ""
Next, we’re going to define two functions. The first function will ask a user to select a username and check if it is more than five characters long. The second function will print out that username to the Python shell:
def choose_username(): global username username = input("Choose a username: ") if len(username) > 5: print("Your username has been selected.") else: print("Please choose a username greater than five characters long.") choose_username() def print_username(): print(username)
In this example, we’ve used the “global” keyword so that the contents assigned to the “username” variable inside the
choose_username() method will be accessible globally. Now that we’ve defined our functions, we’re going to create a main function which calls these functions:
def main(): choose_username() print_username()
We’re then going to add in the
if __name__ = ‘__main__’ statement. This means that if we run our source file directly, the Python interpreter executes our two functions. If we run it as a module, the contents of our
main() method will not execute.
if __name__ == "__main__": main()
Let’s run our code:
Our code returns:
Choose a username: CareerKarma Your username has been selected. Career Karma
Our code runs the
choose_username() function, and then executes the
print_username() function. If we select a username that is fewer than four characters, we’ll see a response like this:
Choose a username: CK Please choose a username greater than five characters long. Choose a username:
We’re prompted to choose another username.
If we were to import this code as a module, our
main() function would not run.
main() function is used to separate blocks of code within a Python program. Writing a
main() function is mandatory in languages like Java because it makes it clear in what order lines of code run within a program. While it’s not necessary to write a
main() function in Python, writing one is a good way to improve the readability of your code.
Now you’re ready to start writing and working with main methods in Python like a professional!