Intro to Python: Section 2
The Hello World project is the first program you will write in nearly any programming course. It’s a boring program that simply prints out the message “Hello, World” or similar to the console.
It’s overused for good reason. It’s a simple introduction to the language and teaches you about the concept of output. Output is anything the program spits out. It’s the data, information, visuals, music, etc. that comes out of your computer. Without output, your computer would be useless. User input, or providing a way for the user to interact with the computer, is equally important. We will discuss input in the next section.
Getting Started
Begin by installing the tools you need to code in Python. Start by installing Python itself, if you have not done so already. This is mandatory.
You can write Python in any code editor you’d like. You can even write it in Microsoft Notepad or VIM if you’re feeling hardcore. However, the advantage of a code editor is it provides syntax highlighting. Syntax highlighting colors sections of your code differently based on what different elements of your code do. It’s a very useful feature.
They also have autocompletion features, make it easy to read Python documentation, and provide debugging tools. Some even catch errors before you execute the program, kind of like spell check for programmers. I’d highly encourage you to check out Visual Studio Code and install the Python extensions for it. PyCharm is another good development environment, which is paid software but free if you’re a student.
The screenshots I show will be from Visual Studio Code, but any editor will work.
If using VS Code, be sure to install the Python extension. You can do so by clicking the Extensions button in the left panel, searching for “Python” and installing the extension by Microsoft.
Code and Run Hello World
It’s standard practice to place all your project files in an individual folder. You can then open that folder as a workspace in Visual Studio Code, or any IDE, and it will easily allow you to navigate between files.
For this exercise, you can create a new file called “helloworld.py” wherever you want. The .py extension is the file extension for python source files.
The print function is all you need to print out the message “Hello, World” to the screen. A message is a string of characters. We wrap strings in quotes with quotation marks.
The print function takes a string as its argument. An argument is information passed to a function when the function is being called. Arguments are placed in the parenthesis directly after a function call.
print("Hello, World.")
Code language: Python (python)
After entering the code, save the file. Now you’re ready to run it. Hit F5 if you’re in VS Code to start debugging. If you haven’t installed the Python extension, it will prompt you to do so. You can also run the program from the “Run and Debug” menu on the left.
The program should execute and print the message “Hello, World.” to the screen in the terminal.
Congratulations! You’ve just created your first Python application.
Running From System Console/Terminal
You may also run your console programs from outside the IDEs.
To do so, you need to open PowerShell in Windows or a Terminal in Linux or Mac and navigate to the folder containing your python file.
The command to change directories in Windows and Linux is usually ‘cd <directory>’ where directory is the folder you’re trying to get to.
Once you’re in the directory, type the command:
python helloworld.py
The command is simply instructing the computer to use python to run this application.
If you named your file something else, you’ll have to type that instead. Then hit return or enter. You should see the message printed out before.
Comments
Comments are a powerful tool for documenting your code. It’s important to have useful comments in your projects, so you can understand what’s going on if you or someone else needs to revisit your code later.
To create a comment in python, use the hash tag or number/digit symbol followed by whatever you’d like to place in the comment.
# this is a python comment
Code language: Python (python)
What you type in a comment is entirely ignored by the compiler.
Comments should usually go above the line or section they’re describing.
# print the message
print("Hello, World.")
Code language: Python (python)
The above example isn’t the best use of a comment. As you practice python, or any programming language, the ultimate goal is to write readable code that is discernable with few comments required. It’s quite obvious that the print(“Hello, World.”) function is printing a message, so you don’t really need a comment here.
While you’re still learning, it’s OK to use lots of comments if you find them helpful. When you’re developing real applications in a professional setting you should avoid over commenting.
You can also use three quotation marks to mark the beginning and end of a multi-line comment, if you’ve got a lot to write. This is good if you need to write a long comment. Comments shouldn’t require you to scroll horizontally to read them.
"""
This is
a multi line
comment
"""
Code language: Python (python)
In the next section, you will learn how to use Python to read user input and manipulate information.