Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Hello world

As customary when learning a new language, the first program a programmer writes is called "Hello, world!". It's a very simple program that outputs the text string "Hello, world!" to the console. But, as we'll see, there's a lot of stuff going on even in a program that's so simple. So let's fire up the Python interpreter and type the "Hello, world!" program.

>>> print("Hello, world!")
Hello, world!
>>>

From this one-liner alone, we can already learn a couple of things about Python.

First of all, it introduces the concept of a text string. A text string (or just "string") is simply a sequence of characters.

A string is delimited by double quotes at both ends, and can contain any printable character and some other special sequences (we'll get to them later).

Before we go on, you should notice that you can evaluate strings as you previously evaluated arithemitc expressions. So let's try entering a string at the prompt and see what happens.

>>> "Hello, world!"
'Hello, world!'

The interpreter is telling us that the value of the string "Hello, world!" is, well, 'Hello, world!'. You may notice that the interpreter is returning the string enclosed by single quotes instead of double quotes. This is because, in Python, they are syntactically equivalent. The difference is that, by convention, you put strings you need to show to the user between double quotes, and other kinds of string (for example, the path of a file) between single quotes.

Going further with our sample script, we call the print function passing our "Hello, world!" string as an argument. For now you can think of a function as something that receives values and does something with it. The arguments to the function (the values we are passing it) are enclosed by paretheses and, if more than one, separated by commas. So, for example, the pow function elevates a number to a power. It needs two arguments, that is the base and the exponential. So to obtain the value of 32 we write

>>> pow(3, 2)
9

In the particular case of print, it can receive a string  or a number and print it to the console. Notice that, when passed a string, print strips the surrounding quotes away before printing.

We could also pass print an expression.

>>> print(6 * 3)
18

Here, in fact, we are passing print the result of the multiplication between 6 and 3. The interpreter first evaluates the expression and than passes the result to print. Why this is different than just typing 6 * 3 inside the interpreter will be more clear in further in the tutorial, when we'll save our scripts to a file.

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, klez
 Last updated by: chrisdavidmills,