Pulsars
0 %
Log inSign up

Discovering Python

Your first program: displaying a message

Your first program: displaying a message

In programming, the very first program traditionally written displays a welcome message. In Python, this fits in a single line, thanks to the print function:

print("Hello, world!")

When you run this program, Python displays exactly the text between the quotes. A function, like print, is a bit like a machine that you give a piece of information to (here, the text to display), and which performs a specific action with it.

You can display several elements at once, separated by commas:

print("The result is:", 42)

Python will display: The result is: 42 (the elements are automatically separated by a space).

It is also essential to know how to comment your code. A comment is a line of text that Python completely ignores when running: it is only used to explain the code for yourself or for other people who will read it. In Python, a comment starts with a hash #:

# (This program displays a welcome message)
print("Hello, world!")

Getting into the habit of commenting your code, even simply, is an excellent practice: it helps you remember, weeks later, what each part of the program does, and it helps others understand your work.

Now try, in your own Python console, displaying your first name followed by a message of your choice. You have just written your very first program!