_
Versions of Python

www.emcu.it





-

Versions of Python

Today (2014) are available two version of Python and are:
Python 2.x.x
Python 3.x.x
The differences from this two version are in the links below.

The standard librariers of Python are here:

In this manual I used the Python v.2.7.3.

For check if Python is present on your RaspByan use the command below:

python -V

The answer must be similar to this (see below):



From a terminal if you write: Python
you enter in the
command interpreter
and you have the possibility to test immediately the syntax of Python.

For exit from the interpeter type: exit()

Very useful is the utility pip that is a tool for manage the Python libreries.
For instal pip use the command below:

sudo apt-get install python-pip

Basically the structure of the programs is accomplished simply identation (aligning with the right TAB) the instructions you write.
You no longer need to declare variables before using them.
The data types are automatically assigned according to the contents used during the assignments.
Python has the following keywords or reserved words; they cannot be used as identifiers

    and
    as
    assert
    break
    class
    continue
    def
    del
    elif
    else
    except
    exec (changed to a built-in function in 3.x)
    False (added in 3.x)
    finally
    for
    from
    global
    if
    import
    in
    is
    lambda
    None (added in 3.x)
    nonlocal (added in 3.x)
    not
    or
    pass
    print (changed to a built-in function in 3.x)
    raise
    return
    True (added in 3.x)
    try
    while
    with
    yield

Here some link regarding Python.


For execute a Python program you must use the syntax below.

python <file-name>

NOTE:

In a real program, that is a file, the first line must contain the pointer to the command interpreter of PythonOn Raspberry Pi that use RaspBian must be:

#!/user/bin/python


UP




-

Our first Python program

First I suggest to create a directory for our examples, I suggest the name PY.
Next, in the directory PY we create P1.py ,  follow the steps below.
cd /home/pimkdir PY
cd PY
nano P1.py

Write in P1.py the lines below.

#!/user/bin/python
print "Ciao da RaspBerry Pi"

Save the file (ctrl + X) and next use the command:

python P1.py

if you did everything correctly you must see something like this:




-

Of course there are Integrated Development Environments (IDE) that make life easier for programmers.
Look for example here:

https://wiki.python.org/moin/IntegratedDevelopmentEnvironments


UP