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
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
We could also pass
>>> print(6 * 3) 18
Here, in fact, we are passing
6 * 3 inside the interpreter will be more clear in further in the tutorial, when we'll save our scripts to a file.