What is if __name__ == "__main__" in Python?
Have you seen the expression if __name__ == "__main__" in Python and are you wondering how is it used in Python programs? In this Python tutorial, we go through the usage of __name__ and __main__ in Python. ---------- C H A P T E R S ---------- (00:00:00) The special variable __name__ in Python (pronounced dunder name). (00:00:36) Print the value of the __name__ variable in the Python shell. (00:00:57) Print the value of __name__ for the random module. (00:01:20) Write a sample Python program to clarify the concept of __name__. (00:03:00) Explain how you can use __name__ in your Python programs. (00:04:50) Check the value of __name__ using an if statement to prevent the code in a module from being executed when the module is imported. (00:06:50) Understand the concept of the Python main function. (00:08:20) Learn how to pass arguments to your Python application using the sys module (specifically sys.argv). We will see how checking the value of __name__ in Python can be useful when running your program either directly or as a Python module. You can find the full tutorial on the Codefather blog: https://codefather.tech/blog/if-name-main-python/ If you want to learn Python have a look at the full list of Codefather tutorials: