Thursday, August 20, 2020

print("Hello World") - the most useful function

print("POST STARTED")

 print("Hello World") - I am sure 90% of you would have started your coding journey with this statement. I often goTo - "google.com" to check if the internet is working or not and to check if a language is properly installed or not - I use print("Hello World"). Don't forget you use a variant of print() to get your files printed on a hard copy.

Let us remove the world and focus on print - every language has this function. You don't even need to install additional packages to run this command. There are different circumstances where you will use print(). If you are developing automation - it makes sense to write a log file this can be easily achieved using print() (some languages require you to use write()). While executing a process on the console - the print() can help us track if the script is taking correct inputs or not. The same can be used to add messages to the popup window and make the codes more interactive.

I use print() primarily for debugging the script and secondarily for logging status. I just can't imagine having to debug a script without a print(). In the past couple of years, I worked on a Linux environment which had no dedicated display. The only way to see the script status was the console window. This is when I started exploring print command even more. I start off with adding a print command before and after a function call. This helps me to drill down on the function which is creating the issue. I would then divide the function (which is causing an error) into 3-4 blocks and add print commands at each break. Then I would go to a particular section in a function and I keep drilling down till I find the main issue causing line.

When drilling down to a particular line in code I would add print commands like - 

print("Entering function editFile")

print("editFile function finished")

print("Line 1.1")

print("line 1.2")

.
.
.
.

This might seem a bit crude but it always works for me. Then comes the interesting part and the most useful feature of print(). no matter what you give as an argument to print command - it will print it most of the time. You pass on the variable value to see what while is getting assigned a variable while the script is running. You can easily combine string and variable values.

print("Value of i = "+ $i)

Then, be it the matrix, array, vector, string, or even an entire file - you can simply print it without worrying much about the syntax. Let me speak a bit about print in python specifically. You give different object types in print and it will format it accordingly before printing. This just enhances readability by leaps and bounds. You read a CSV file and say print and it will highlight the column names for you and display the data as if you are seeing an excel sheet. you give it a 1-D array and it will print it in one row. You give 3-D array and it selects the best possible view to show the data.

The words may not seem to do the justice to the usefulness of the print() but once you start coding you will appreciate it. For the users using automation and checking the log files - remember the pains and efforts someone took to write a print statement in their code to make the end process user friendly. I would love to hear about your take on the print().


print("POST ENDED")

No comments:

Post a Comment