by Matthew Barlowe
“Hello World” is a very common first program for just about any programming langauge. I’m sure there’s some history behind it, but I’ve never bothered to look it up. Usually it’s just a simple program of very limited lines that prints out the phrase “Hello World.” The purpose of this tutorial is to get you setup to write that first program.
The first thing to do is open your favorite text editor. I use NeoVim which is based on Vim a text editor that comes with Unix. Vim is know for being dificult if you aren’t used to it, but there are many others out there: Nano and Emacs come with the MacOS Unix as well, you could use Atom which is built by Github, or even plain old TextEdit. The main thing is to use something that won’t add extra unseen characters to what you type like a word processor such as Microsoft Word or Apple Pages.
Once you have your text editor open type these lines into them:
#!/usr/bin/env python3 print("Hello World")
Once that is done save your file as
helloworld.py and exit the text editor if you are using
a terminal editor and type
python3 helloworld.py which will
produce this output:
Congratulations, you’ve just written your first python program! Obviously, there’s a lot more to it than this but even in just this simple program you can learn a lot.
Breaking Down the Code
Ok let’s look at the code and see what’s going on in this program. The first line of the code is what’s called a shebang line. This isn’t unique to python scripts but is common in the Linux/Unix environment. A shebang line tells the shell what type of interpreter to use to process the text of the file. You can read more about shebangs and their use in *nix1 envronments operating. This includes Unix, Mac OS X, Linux among others. Most usually it refers to Linux and Mac OS X as those are the two most popular *nix environments. at this site. Basically, this line is telling Unix what type of python to use to run the script.
If you have followed my other tutorials you may be asking now, “Why did
/usr/bin/env instead of
where our Homebrewed python3 is located? An excellent question. The reason
/usr/bin/env instead of where our python is actually located
env is an actual Unix command that will create
an environment using the environment variables and then run the program passed
to it in the arguments which in this case is python3.
env will then use the
$PATH variable, the
same one mentioned in the Homebrew tutorial, of the environment
and go down the directories on that
$PATHuntil it finds the
right directory of the program which it then executes. To sum it all up
it just makes your code more portable because it will search that computer
to find where
python is to exectue the script.
And one last thing the shebang is good for is letting people know exactly what type of code they are looking at. It will say python3 if working with version 3.x, python2 if working with version 2.x, and just plain python if the code will run with either version. You won’t have to worry about python 2.x for much longer as it will soon be deprecated but there is plenty of legacy code out there still.
The next part above is where the code actually starts doing something. In
this case that something is printing the phrase “Hello World.” Now there are
a lot more things you can do than this in python, but
SourcesWhat is env
What form should shebang take
- *nix is a term commonly used to refer to operating systems based on the Unix [return]