Python Basics

Learn how to use a Python interpreter and run a basic “Hello World” program.

Introduction

In this course, we will be exploring Python interactively in the browser. We want you to learn as much as possible by modifying our code directly and trying things out!

If you prefer, you can follow along using a Python interpreter installed on your computer. Appendix A shows how to set this up.

Our first Hello World example will be illustrated with an installed interpreter. From that point on, we will always show runnable code inline with the text.

Hello World

So, let’s dive in with a classic Hello World program. We will start by opening an interactive Python interpreter. This course assumes your Python command is called python3, so our steps will look like this:

Press + to interact
$ python3
Python 3.7.3 (default, Apr 24 2020, 18:51:23)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello World')
Hello World

There is quite a lot going on here, so let’s break it down.

$ python3

$ is your shell prompt. Depending on your platform, this may be C:\> or something else.

python3 is the command to invoke the interactive Python interpreter.

>>> is the Python primary prompt. Anything typed here will be evaluated by the interpreter when the return key is pressed, unless the statement typed is the beginning of a code block (see later).

print('Hello World') is our actual Python code for printing Hello World. This calls the built-in function print(), passing 'Hello World' as an argument.

The text Hello World is then returned when we press enter.

The same program is shown below in the inline editor.

Press + to interact
print('Hello World')

Now click RUN and observe the output. Try changing the text 'Hello World' to something else to get used to working with the inline widget.

Exploring the interpreter

One of the main benefits of Python is its ability to operate dynamically. This includes the ability to inspect, modify, and even generate many aspects of a program at runtime.

For example, if we type:

Press + to interact
help(print)

We will get the help (or doc string) for the print() function.

We can also use the type() function to find out type information at runtime.

Press + to interact
print(type(print))

This tells us that print is an instance of classbuiltin_function_or_method. We can confirm this with:

Press + to interact
help(type(print))

Experimenting with help and type is a great way to learn how Python works. As an exercise, try experimenting with various nestings of help(), type() and print().

Note: We wrap some of the statements above in an outer print() so we can see the output. This is not needed for help() since it prints to the console anyway.

Running a script

Typing commands directly into the interpreter is useful for experimentation, but it is not the usual way to run production code. Instead, we save our code to a file with ‘.py’ extension and pass the file path as an argument to the interpreter.

To demonstrate this, create a file on your computer called hello.py and add the statement print('Hello World') to the top of the file.

Now save the file and run the script with:

Press + to interact
main.sh
hello.py
python3 hello.py

This will print “Hello World” to the console.