This unit introduces a simple program that prints the string "Hello World!" to the standard output in English and Japanese. It also shows how to create execute the program from the interactive interpreter and also how to execute it from a source file only. Finally it introduces the in-built help system.
Every story has a beginning, and so has every programming tutorial. This one is sticking to good tradition of starting with a simple program that prints the string “Hello World!” to the standard output.
The purpose of this program is to learn how to output messages to better understand what happens within. Furthermore this unit also shows how to run said program and how to use the in-built help system.
print(…) built-in function
Prints the values to a stream, or to sys.stdout by default.
help(…) built-in function
Print documentation about an object.
#! /usr/bin/env python3 print("Hello World!")
The actual “Hello World!” program in Python is trivial. It uses the in-built print(…) function to output the string.
Python scripts can be executed in two ways:
Running a script file
This is the persistent form of running a Python application. It’s done by passing the script name to the python command.
% python3 hello.py Hello World!
Note: Shell prompts are mostly a matter of preference. I use the “%” sign to indicate that a command is entered in a bash shell.
Starting the interpreter and interactively typing the program
The interactive mode is invoked by running the python command without arguments. In this mode, every entered script line is executed immediately. This is very useful when you want to interactively see what the program does at each step and introspect the data structures as you go. This mode can also be entered when passing a script file as argument and the “-i” option and is invaluable with debugging Python programs.
% python3 >>> print("Hello World!") Hello World!
The prompt of the interpreter is “>>>”. In the above example I start from a bash shell and enter the Python command interpreter. The third, un-prefixed line is output that the program writes to the system standard output (stdout).
#! /usr/bin/env python3
A hash character in a Python script starts a comment that lasts until the end of the line. That’s why the first line in the hello.py script is ignored by it. The purpose of this line is to tell the shell on UNIX like systems (Linux, OS X) how to execute the script. The result is, that if the executable bit is set on the file, it can be run without having to pass it to the python command in the terminal, as shown in the following example
% python hello.py
A few years ago Python 3 was released, which contained extensive
language cleanup and broke all existing code. Porting the old
software to Python 3 is on-going, but the old interpreter is
still around, often in parallel to keep the previous scripts
running until the migration is completed. The result of this is,
that sometimes (especially on Linux systems) you’ll find more
than one Python installation, and the current version is
post-fixed with the major version number like this:
I’ll use this command notation in this tutorial to be explicit,
but on some systems the command will be
Python is fully object oriented. Everything is an object, including functions, classes and even basic data types. This means that everything in Python has a basic interface that you can rely on. The specifics are explained in later units, but one implication that you’ll want to know is that you can pass almost anything as argument to the print() function.
>>> print(5) # integer 5 >>> print(True) # bool True >>> print(print) # function <built-in function print>
Indeed, you can even pass the print function to the print function and it will output a string representation for what it is.
The help() function prints documentation about functions, classes, types, modules and more. It’s a wrapper around the pydoc utility, that does the same form a system command shell.
>>> help(print) ... % pydoc3 print ...