Knowledge for the World

Your first Python program

Hello, world!
  • by tyler (203)
  • 10 minutes

This is the beginning of a series on Python basics. The series is intended for anyone who wants to learn Python, including those who are new to programming. This series is specifically designed for macOS and Linux users.

In this guide, we'll write our very first program. Yes, it will be the classic "hello, world" program, but most importantly we'll cover setting up your environment and executing the program.

1

If you're already comfortable with the Python setup on your system, feel free to skip this step. For this series, we're going to use Python 3, and this step covers how to set up a Python 3 environment on macOS and Linux environments.

macOS

Most unix systems (like macOS) come with a system supported Python installation. Currently, macOS ships with Python 2.7.

However, if you're learning Python, you should learn Python 3. And we will.

There are multiple ways to install and use Python 3 on a mac, but I recommend Homebrew as a starting point. Follow this link, to learn how to install Homebrew.

Then you can simply run:

brew install python

to install the latest version of Python 3.

Linux

sudo apt-get update
sudo apt-get install python3.6
2

Let's get to it.

Create a new file called hello_world.py and paste the following:

print('Hello, world!')

And that's it! Let's execute this thing.

Open a console (an app called "Terminal" on many systems) and navigate to your working directory. Then run:

python3 hello_world.py
3

As an exercise, can you create a file and execute the following Python code?

Can you guess the output?

Hint: You've seen it before!

(lambda _, __, ___, ____, _____, ______, _______, ________:
    getattr(
        __import__(True.__class__.__name__[_] + [].__class__.__name__[__]),
        ().__class__.__eq__.__class__.__name__[:__] +
        ().__iter__().__class__.__name__[_:][_____:________]
    )(
        _, (lambda _, __, ___: _(_, __, ___))(
            lambda _, __, ___:
                bytes([___ % __]) + _(_, __, ___ // __) if ___ else
                (lambda: _).__code__.co_lnotab,
            _ << ________,
            (((_____ << ____) + _) << ((___ << _____) - ___)) + (((((___ << __)
            - _) << ___) + _) << ((_____ << ____) + (_ << _))) + (((_______ <<
            __) - _) << (((((_ << ___) + _)) << ___) + (_ << _))) + (((_______
            << ___) + _) << ((_ << ______) + _)) + (((_______ << ____) - _) <<
            ((_______ << ___))) + (((_ << ____) - _) << ((((___ << __) + _) <<
            __) - _)) - (_______ << ((((___ << __) - _) << __) + _)) + (_______
            << (((((_ << ___) + _)) << __))) - ((((((_ << ___) + _)) << __) +
            _) << ((((___ << __) + _) << _))) + (((_______ << __) - _) <<
            (((((_ << ___) + _)) << _))) + (((___ << ___) + _) << ((_____ <<
            _))) + (_____ << ______) + (_ << ___)
        )
    )
)(
    *(lambda _, __, ___: _(_, __, ___))(
        (lambda _, __, ___:
            [__(___[(lambda: _).__code__.co_nlocals])] +
            _(_, __, ___[(lambda _: _).__code__.co_nlocals:]) if ___ else []
        ),
        lambda _: _.__code__.co_argcount,
        (
            lambda _: _,
            lambda _, __: _,
            lambda _, __, ___: _,
            lambda _, __, ___, ____: _,
            lambda _, __, ___, ____, _____: _,
            lambda _, __, ___, ____, _____, ______: _,
            lambda _, __, ___, ____, _____, ______, _______: _,
            lambda _, __, ___, ____, _____, ______, _______, ________: _
        )
    )
)

Credit goes to Ben Kurtovic for this exercise.

Series: Python Basics
Show all in this series