Now that Python is installed, it’s time to move on to writing our first program.
What The Hello World?
There is a tradition in programming books and tutorials that the first test program you write in any new language is “hello world”. Hello World is a small program that prints out hello world
to the screen. That’s it.
The Hello World program is useful for a few reasons. Printing a line of text to the screen is about the simplest program you can write. It gives you output that you can verify. And, it is easy to change and play with.
That's why we are going to start this course with Hello World too. Along the way you’ll learn about how to run Python programs and how to print text. You’ll be using what you learn in this lesson in future lessons. So, pay attention!
You Need A Code Editor
This is a good opportunity to talk about code editors. As a programmer you will use a code editor. A code editor is a text editor with some features specific to writing code. You might also hear the term IDE or integrated development environment. That’s the same idea with more features.
There are many good code editors out there. I often use Emacs, Vim, or Atom (now deprecated) for a lot of my programming. Visual Studio Code is also a very popular choice. I hear notepad++ on Windows is good too.
It doesn’t matter what code editor you use to write Python code. All they need to be able to do is edit plain text files and save them with a .py extension. Other features like syntax highlighting or autocompletion are nice, but not required.
Pick a code editor and get familiar with it as you use this course. If you are unsure, just use Visual Studio Code.
FUN FACT: For many years I did all my programming in Notepad on Windows. I didn’t know any better. Yes, this really happened.
NOTE: A word processor like Google Docs or Microsoft Word are NOT code editors. They are word processors. They save files in a completely different format and add a bunch of markup behind the scenes.
Let’s Write Your First Python Program - Hello World
Your first program is called Hello World. The goal of the program is to print out the phrase “Hello World” to the screen.
Create a new file called journal.py
. You can do this in your code editor or using the unix touch
command:
Write the following code in journal.py
:
In a command line terminal (terminal on Mac or PowerShell on Windows) run the following command:
NOTE: You will need to run that command from inside the folder where the journal.py file resides.
You should get the following output:
Assuming that worked, let’s move on to adding more print statements. Update your code to match the following:
Now run it again to see the following output:
That is a good stopping point for today. In the video I show a few more variations, but you should get the idea.
Assignment
The assignment for this lesson is to create your own Hello World program. Use the above steps as a guide.
BONUS: You could play around with putting in different text and running it to see what happens. Also try excluding parentheses or quote characters to cause python errors and see what that does. You can learn a lot with breaking and fixing a simple program like Hello World.
Useful Links
Your First Python Program Is Done!
Congratulations on writing your first Python program. That’s another big step. You wrote code and ran a program. That makes you a programmer (you are someone who programs now). So keep it up and I’ll see you tomorrow for some fun with program design!
-Brian