Python Hello World

Python Hello World

Hello, World! seems to be the first line to code any programmer would run to test if the coding environment is properly set up. This culture has been a norm for both new and existing developers. As a new developer, or new to a language, you would definitely want your first program to be the famous “Hello, World!” program.

One reason why this culture is still practiced in the developer’s ecosystem is because the famous “Hello, World!” program is one of the easiest and bug-free programs you would write in any programming language.

In this article, we would maintain the same culture and write our first “Hello, World!” using the Python programming language. But wait, what the heck is Python? That snake? Well, we’ll find out soon enough.

Prerequisites

  • Have the latest version of Python installed on your computer

Brief History of Python

In the late ’80s, a Dutch programmer was looking for a hobby project to work on during the Christmas break. So he decided to write an interpreter for the new scripting language he had in mind for some while. He chose “Python” as the title of the project (Being a big fan of “Monty Python Flying Circus”. The old BBC television comedy sketch series). His name is Guido van Rossum. He created the Python programming language)

Introduction

Python is a general-purpose, high-level, and object-oriented programming language. It is a widely-used and interpreted language, which makes it more human-readable.

Python is a good choice for beginners because you can use it to write complex programs in an easy and efficient way. Unlike programming languages like Java that provide support for object-oriented paradigm, Python has support for many programming paradigms. Some of the paradigms include functional, object-oriented, and procedural (structured) programming.

Your First “Hello, World!” in Python

print("Hello, World!")

Yay! You just wrote your first “Hello, World!” in Python. While this looks extremely easy and straightforward, it is not the case with other programming languages like C# and Java. Any beginner would be able to relate to the program above because it looks like writing plain English to the computer. This is one of the advantages Python provides over other object-oriented languages.

Breaking Down The “Hello, World!” Program

The “Hello, World!” above seems easy and straightforward, but I bet you will still need an explanation for the syntax and how things are the way they are.

The program above instructs the python interpreter to print the string “Hello, World!” to the console. The print() function is a built-in function (it comes installed with Python) that takes in a couple of arguments and prints it out to the console. The print() function is one of the many built-in functions that come installed with python, and it is made available during execution.

In Python, functions are invoked using parentheses immediately after the function name. If the function takes in arguments, then these arguments are passed into the parenthesis -- just like our print() function above.

The print function does not only take in strings such as “Hello, World!” above. It can take in as an argument any Python data type which can be numbers or boolean values and prints it to the console. It can also take in variables and print them to the console as well.

Note that the “Hello, World!” is enclosed in double-quotes. Python is flexible enough to let you print the same “Hello, World!” in a single quote.

print('Hello, World!')

This flexibility is also what makes it popular among beginners, as you don’t need to bother about which to use when writing simple programs. Unlike Java that uses a single quote for a character and a double quote for strings, any of both can be used in Python for simple programs like the “Hello, World!” above (except for some special cases).

Ways You Can Write Your First “Hello, World!”

In the developer’s ecosystem, there are usually many ways of writing programs and solving problems as there are many ways that lead to the river. We will be exploring some of those ways in this section of the article.

  • Using the Python Shell

Go to your terminal and type the command

python

If python is properly installed on your machine, it will open the Python shell as shown below.

Using Python Shell 1

Then go ahead and write your “Hello, World!” program on the prompt as shown below.

Using Python Shell 2

  • Using the Python IDLE

Locate the Windows icon on your taskbar (If you are using a Windows OS), select “All apps”, and then search for the Python as shown below. If you have different versions of Python installed, they will all pop up here. Select one of them as shown below.

Using IDLE 1

Click on the “IDLE” app from the dropdown and a new window will pop up on the screen as shown below.

Using IDLE 2

Then run your “Hello, World!” code and see the output.

Using IDLE 3

The Python IDLE allows you to run python codes on the Python shell. It behaves similar to the method above. The main difference is that you don’t have to type in the python command before activating it. And also, the IDLE looks more aesthetically pleasing.

  • Writing Python codes in a file

One of the limitations of using the methods mentioned above is that it is not flexible to write multiple lines of python codes on either the Python shell or the Python IDLE. Python provides us a way around this by creating a file directly from the IDLE and writing your code in it.

using file 1

Locate and click on “File” from the tabs above. Then select “New file” which will open up a new window to write your Python codes. Alternatively, you can enter “Ctrl + N” on the keyboard which is a shortcut for opening a new window for your Python code.

using file 2

Write your “Hello, World!” code in the editor provided and save it with a suitable name, and in a suitable location where you can easily find it.

using file 3

Locate and click on the “Run” button from the tabs above and select “Run Module” when the dropdown comes. Alternatively, you can use the shortcut key - F5 to run the program directly.

using file 4

The above methods are the most fundamental ways you can write and run your Python programs without having to install any code editors. All of the tools used above came bundled with Python, so you don't have to download them anymore. When you get more comfortable with the methods above, then try and use other third-party code editors like PyCharm, VSCode, Atom, Sublime, e.t.c.

Adding Comments To The “Hello, World!” Program

Python allows us to add comments to our program which is a way of reminding us of what the program does, in case we will ever go through it in the future. Let us add a comment to our “Hello, World!” program describing what it does.

Adding comments

Python allows us to use the pound/number (#) sign in order to write comments in our code. When we use the pound sign in front of a statement, the Python compiler completely ignores anything after the pound sign, and then continues on the next line. There are several types of comments in Python but we will be considering the simplest one for our “Hello, World!” program.

Advantages Of Documenting Our Programs

Writing comments in your program is a means of documenting the program. This is one of the best practices every programmer is advised to stick to. Some advantages of writing comments includes:

  • Clarity: When you need to make reference to your code writing some time ago and you can’t remember what the program actually does. The comments that are written along with the program can give you a hint of what each function or statement does. This is particularly useful in complex and large programs containing a bunch of functions and packages. With the help of the comments, it will be easy to map out what the program does.

  • Reusability: The fact that you can still understand and remember what your code does after a long time doesn't mean everybody can. In a case where you are deploying a package to be used by other developers, they might get stuck at some point and attempt to check the codebase. Without proper comment or description of what each code does, it will be hard for the programmers to identify where the problem is thereby discouraging them from using the package any further.

  • OSS: Open Source Software (OSS) is software that anybody can modify because it is “Open” and publicly accessible to everyone. Python is an example of an OSS that anyone can contribute to. OSS is mostly developed by programmers all over the world. Because of this diversity, confusion might come up because the codebase is written by different sets of people in different locations and at different points in time. These people might not (or never get to) know each other. This problem is solved by proper documentation of a project. Adding comments to programs is a way of documenting a program for future reference. This is the reason why many robust open-source software like Python is still in existence today.

Conclusion

Knowing the history and basics of a programming language is a very important as leaning the language itself. This article gives us a high level overview of the basics of the Python programming language, its creator, history, and how the name came about. It then moved on to breakdown the famous "Hello, World!" using Python. Lastly, it branches through the writing comments and its importance the programming. While this article covers some of the basics of Python, check out the official Python documentation to know more about Python.

Feel free to drop your thought and suggestions in the discussion box. I will be available to attend to them. And also, if you have any questions, you can as well drop them in the discussion box.