Hello, World!
Your first Python program, the print function, and comments
Tradition demands that the first program in any language print the words "Hello, World!". Python makes that exactly as short as you would hope.
That is the entire program. No main, no semicolons, no header
files, no compilation step. Just one line.
Why "Hello, World"?
The phrase dates back to Brian Kernighan's 1972 tutorial for the B
programming language, which later appeared in the classic book The C
Programming Language (1978). It has been the ritual first program ever
since. The choice is practical: printing output is the simplest form of
input/output (I/O), and every program is fundamentally about I/O —
reading data, transforming it, and writing results. print is your
first taste of that loop.
How it works: source to output
When you write a Python program and run it, here is what happens:
The Python interpreter reads your source code, compiles it to bytecode
(a lower-level representation), and executes it instruction by
instruction. When it hits print(...), it writes to standard
output — usually your terminal.
The print function
print is a built-in function that writes its arguments to standard
output. It is more flexible than it might look at first.
Multiple arguments
Changing the separator
Changing the ending
Printing any object
You can print numbers, lists, dictionaries, and most other objects
directly. Python will call str(obj) on each argument:
Comments
Comments start with # and run to the end of the line. Python has no
native multi-line comment syntax; people sometimes use triple-quoted
string literals for that, but a string is not the same as a comment.
Why not multi-line comments?
Many languages have /* ... */ or (* ... *) for multi-line comments.
Python does not, by design. Guido's rationale was that long comments
should be broken into shorter lines anyway for readability, and
triple-quoted strings serve the "comment out a block" use case well
enough.
Docstrings
A string at the very top of a module, function, or class is called a
docstring. Tools like help(), IDEs, and the pydoc command read
these to show documentation.
Docstrings are a convention, not a language feature, but they are so
ubiquitous that Python treats them specially: they are stored in the
__doc__ attribute of the object.
PEP 257: Docstring conventions
PEP 257 lays out the conventions:
use triple double-quotes """like this""", write a one-line summary
first, and add details in subsequent paragraphs. Most documentation
generators (Sphinx, MkDocs, pdoc) parse these docstrings to build API
references.
Your first challenges
Write a single print call that outputs exactly:
Hello, Grace!
Do not hardcode the literal string "Hello, Grace!"; instead, define a
variable name = "Grace" and use it in your print call. You can use
string concatenation, f-strings, or .format().
Use a single print call to output:
apple:banana:cherry
Pass three separate arguments to print (not one concatenated string) and use the sep parameter to join them with ":".
Test your knowledge
What does the following snippet print?
print("a", "b", "c", sep="-", end="!")
a b c
a-b-c\n
a-b-c!
a-b-c!\n
What is a docstring?
A comment that starts with #
Any triple-quoted string in the code
A string literal at the top of a module, function, or class, used for documentation
A string that is never executed
Which of the following is the correct way to print "Hello" without a trailing newline?
print("Hello", newline=False)
print("Hello", end="\n")
print("Hello", end="")
print("Hello", sep="")
You have run code, written a comment, and added a docstring. Next we will look at why those four spaces of indentation matter so much.