/* jquery */ /* jquery accordion style*/ /* jquery init */

26 March 2013

Learn Python - Interactive Python

Python has an interactive mode. A mode where we can type a statement and see the results immediately. It's a great place to experiment and gain confidence with the language. If you type something Python doesn't understand it will simply display an error message, and you can try again.

Python's interactive mode runs in a Linux terminal window. Two simple steps are all that's required. First open the LXDE Desktop menu on your Raspberry Pi and select the 'Other->Terminal' option. This will open a new terminal window.

Next to the '$' command prompt type 'python' and press return, as below:

$ python

You'll see a short message about the version of Python, and the interactive mode cursor '>>>' will appear. Now we can type any Python statement and press the 'enter' key to see the result.

Let's try to output a number with the 'print' keyword. Enter this statement:

>>> print 123.456

Now we'll use the same 'print' keyword to output a text string, as here:

>>> print "Hello"

Notice we've used quotes this time to tell Python this is a text string.

As Python understands numbers and mathematical symbols we can use Python as a calculator. Let's try a very simple statement:

>>> 2+2

After pressing return key you'll immediately see the result, namely 4. We can do more complex calculations, like this:

>>> (2*8) - (2*4)

Here we used symbols and brackets, just as you'd see in a math text book.

A post from my Learn Python on the Raspberry Pi tutorial.

No comments: