Syntax

The indentation (4 spaces) in python it's important!

The main Syntax are:

- if, elif, else;
- for and list-comprehension;
- while;
- break and continue;
- function;
- decorator;
- class.

if, elif, else

In [1]:
# define some sets
gis = set(['arcgis', 'mapinfo', 'qgis', 'grass', 'udig'])
opensource = set(['qgis', 'grass', 'udig'])
usingpython = set(['qgis', 'grass', 'arcgis'])


soft = 'arcgs'
if soft in gis:
    print('it is a GIS software')
elif soft in opensource:
    print('it is opensource')
elif soft in usingpython:
    print('it use python')
else:
    print('something else...')
something else...
In [5]:
for i in list(range(len(gis))):
    print(list(gis)[i])
grass
udig
arcgis
qgis
mapinfo
In [2]:
# define some sets
gis = set(['arcgis', 'mapinfo', 'qgis', 'grass', 'udig'])
opensource = set(['qgis', 'grass', 'udig'])
usingpython = set(['qgis', 'grass', 'arcgis'])


soft = 'arcgis'
if soft in gis:
    print('it is a GIS software')
elif soft in opensource:
    print('it is opensource')
elif soft in usingpython:
    print('it use python')
else:
    print('something else...')
it is a GIS software

Use a if condition to assign a variable

In [3]:
usepython = True if soft in usingpython else False
print(usepython)
True

cycle for

In [4]:
for soft in gis:
    print("%s\n%s" % (soft, '=' * len(soft)))
    if soft in opensource:
        print('it is opensource')
    else:
        print('it is propretary')
    if soft in usingpython:
        print('it use python')
    else:
        print('it use something else')
    print()
qgis
====
it is opensource
it use python

arcgis
======
it is propretary
it use python

grass
=====
it is opensource
it use python

mapinfo
=======
it is propretary
it use something else

udig
====
it is opensource
it use something else

List comprehension

In [5]:
[(soft, len(soft)) for soft in gis]
Out[5]:
[('qgis', 4), ('arcgis', 6), ('grass', 5), ('mapinfo', 7), ('udig', 4)]

List comprehension with a if condition

In [6]:
[(soft, len(soft)) for soft in gis if soft in opensource]
Out[6]:
[('qgis', 4), ('grass', 5), ('udig', 4)]
In [7]:
{soft: len(soft) for soft in gis if soft in opensource}
Out[7]:
{'grass': 5, 'qgis': 4, 'udig': 4}

We can defie also nested cycles

In [8]:
a = list(range(3))
b = list(range(3, 6))
print(a, b)
[0, 1, 2] [3, 4, 5]
In [9]:
[(i, j) for i in a for j in b]
Out[9]:
[(0, 3), (0, 4), (0, 5), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)]
In [10]:
[[(i, j) for i in a] for j in b]
Out[10]:
[[(0, 3), (1, 3), (2, 3)], [(0, 4), (1, 4), (2, 4)], [(0, 5), (1, 5), (2, 5)]]

Hands on!

Using the list comprehension return a list of tuple with the pairs key and value, of contact. [2 minutes], like:

[('pietro', 333123808), ('jonh', 123123123)]
In [11]:
contact = {'pietro': 333123808, 'jonh': 123123123}

Possible solutions are:

In [12]:
[(k, v) for k, v in contact.items()]
Out[12]:
[('pietro', 333123808), ('jonh', 123123123)]
In [13]:
[item for item in contact.items()]
Out[13]:
[('pietro', 333123808), ('jonh', 123123123)]

while

In [14]:
x = 0
# condition, brak the cycle when False
while x < 4:
    print('a' * x)
    x += 1
a
aa
aaa

continue and break

Sometimes is useful interupt the cycle:

In [15]:
for soft in gis:
    print("%s\n%s" % (soft, '=' * len(soft)))
    if soft in opensource:
        print('it is opensource')
        break
    else:
        print('it is propretary')
    if soft in usingpython:
        print('it use python')
        continue
    else:
        print('it use something else')
    print('\n')
qgis
====
it is opensource

Try and except

Python allow to manage the errors:

In [16]:
numerator = 1
denominator = 0. # change to 0
try:
    print(numerator/denominator)
except ZeroDivisionError:
    print("Divide a number with 0 is not a valid operation!")
else:
    print("Here we are...")
finally:
    print("Do something at the end")
Divide a number with 0 is not a valid operation!
Do something at the end
In [17]:
numerator = 1
denominator = 0.5 # change to 0
try:
    print(numerator/denominator)
except ZeroDivisionError:
    print("Divide a number with 0 is not a valid operation!")
else:
    print("Here we are...")
finally:
    print("Do something at the end")
2.0
Here we are...
Do something at the end
In [ ]: