My Python Journey and the Main Function

I have been interested in computers and how they function since my Dad purchased our first family computer back in the mid-eighties. It was an IBM clone made by Commodore that had an 8088 chip, no hard drive and two 5 1/4 floppies. I used to spend a lot of time playing games and just tinkering with the applications we had for it. I did a little Basic programming but never really got into it. Over the years I became increasingly interested in learning a programming language and tried to do so on many occasions, but nothing seemed to stick. A few months in to the pandemic I decided to give it another go and picked Python to start tinkering with. I started with "Learn Python 3 The Hard Way" by Zed Shaw. This was a great book for me as it got me in coding right away so I could start building things and learning from what I built. "Automate the Boring Stuff with Python" by Al Sweigart and "Python for Everybody: Exploring Data in Python 3" by Dr. Charles Severance quickly became staples as well. Once I learned Python syntax a bit and created a few things from the examples in the book I was ready to start on some of my own projects. I am by no means a Python expert and still consider myself a noob at this, but I wanted to start documenting my journey and some things I have learned that have helped me the most. Writing this stuff down helps reinforce my learning and if it can help anyone else out there, then all the better. So on to my first tip!

One of the best Python tips I was ever given was from my youngest and that was to create a main function that is used to refer to the other functions in your app. This tip is great because it really helped me organize the flow of my programs and makes it easier for others to review and provide feedback. The main function is structured the way any other function, but as you build out your application you can reference your other functions here making it easy to follow the flow of the application, which comes in handy when you need to troubleshoot an issue.

def main():

otherStuff = otherFunctions()

Another component that can be added is a conditional that will allow the program to execute automatically and make it easy for people to use your application as an additional library.

If __name__ == "__main__":

main()

Essentially, this conditional will execute the main function if there is one to be called. Learning this structure has helped me immensely in making my code more readable and easier to troubleshoot. This is a tip that I learned only very recently and I wish I had learned it at the very beginning of my journey. This is why I have added it as my first tip.