Python Language : Simplicity and Expressivity

Kastoria TEIWM April 2019

Gérard Rozsavolgyi

In [2]:
PythonLogo
Out[2]:

Main Python strengths

  • Simplicity
  • Concision (~1/3 of equivalent program in Java or C)
  • Portability
  • Batteries Included

Other caracteristics

  • python scripts
  • Interpreted
  • Object Oriented
  • Weak types (vs Java)
  • Open Source

Python users

  • Google
  • YouTube
  • Dropbox
  • Internet Boxes
  • Scientific tool
  • Web mining
  • Natural language processing
  • Deep Learning (TensorFlow, PyTorch, Scikit-Learn)

Inconvenients

  • Rather slow
  • Hard roadmap from Python2 to Python3

Environnements

  • Commandline REPL (Read Eval Print Loop)
  • Dedicated editor(IDLE, Emacs, PyCharm, SublimeText, Atom, VSCode, vim, etc.)
  • Jupyter notebooks

Jupyter notebook:

  • Browser is the IDE
  • Easy debug
  • Comments / Illustrations included

Practise

  • Launch the notebook
  • Choose the Kernel
  • Start coding in a cell
  • Press >| (RUN)
  • or Shift ENTER

Various Kernels available

  • Python
  • virtualenvs
  • JavaScript
  • SQL
  • Octave

Notebook in a notebook

In [3]:
from IPython.display import HTML
HTML('<iframe src=http://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/SymPy.ipynb width=700 height=350></iframe>')
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/display.py:689: UserWarning: Consider using IPython.display.IFrame instead
  warnings.warn("Consider using IPython.display.IFrame instead")
Out[3]:

Let's start coding in Python

Basic types

  • Integers
  • Floats
  • Booleans
  • String
  • None (like void in C++)

Elementary data structures

  • Lists
  • Tuples
  • Dictionnaries

numerical examples

In [4]:
1 + 3
Out[4]:
4
In [5]:
5 * 7
Out[5]:
35
In [6]:
5 / 2
Out[6]:
2.5
In [7]:
5 // 2
Out[7]:
2
In [8]:
0.1 + 0.1 + 0.1 - 0.3
# What's going on ?
Out[8]:
5.551115123125783e-17

Booleans

In [9]:
True and True
Out[9]:
True
In [10]:
True and False
Out[10]:
False
In [11]:
True or False
Out[11]:
True
In [12]:
( 2 > 3 ) and ( 1 < 2)
Out[12]:
False
In [13]:
( 1 < 2 < 5 )
Out[13]:
True

Strings

Declare and print

In [14]:
mystring = 'qwerty'
print(mystring)
qwerty
In [15]:
otherstring = "a nice string"
print(otherstring)
a nice string

Length

In [16]:
empty = ''
len(empty)
Out[16]:
0
In [17]:
len(otherstring)
Out[17]:
13
In [18]:
len(None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-96b07938784c> in <module>
----> 1 len(None)

TypeError: object of type 'NoneType' has no len()

Operations on strings

In [19]:
S1 = "Γειάσου "
S2 = "Κόσμε !"
print(S1+S2)
Γειάσου Κόσμε !

We can't do anything:

In [20]:
S1*S2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-e4eb3fd43725> in <module>
----> 1 S1*S2

TypeError: can't multiply sequence by non-int of type 'str'

But still

In [21]:
S1 * 3
Out[21]:
'Γειάσου Γειάσου Γειάσου '
In [22]:
# or
print('*'*60)
************************************************************

Extract substrings

In [23]:
S = 'azertyuiop'
print(S[1])
print(S[1:5])
z
zert

and what about this ?

In [24]:
print(S[-1])

print(S[-2])

S[::-1]
p
o
Out[24]:
'poiuytreza'
In [28]:
S = "Hello, the weather is rainy today, isn't it ? "
S.split(',')
Out[28]:
['Hello', ' the weather is rainy today', " isn't it ? "]

search and replace in strings

In [29]:
S.find('rainy')
# Returns the index of 'rainy' in the string S
Out[29]:
22
In [30]:
S.replace('rainy','nice')
Out[30]:
"Hello, the weather is nice today, isn't it ? "
In [31]:
S = "ahoouutch  "
S.rstrip()
# cleaning spaces
Out[31]:
'ahoouutch'

Import interesting libs

In [32]:
# Let's import networkx to deal with graphs
import networkx as nx
# Let's create a digraph G
G = nx.DiGraph()
# and add 3 nodes to G
G.add_nodes_from('ABC')
# add an edge from A to B
G.add_edge('A','B')
# and an other from A to C
G.add_edge('A','C')
print(G)
# We can give some pos
pos = { 'A' : (0,0), 'B' : (1,1), 'C' : (-1,1) }
# and draw the graph
nx.draw(G, pos, edge_color='b',node_color='y')

Manipulate data in csv files

In [33]:
import pandas
In [ ]:
%%bash
mkdir data
In [35]:
%%file data/data.csv
Langage,Strengths, Weaknesses
Java, Structured, Verbose
JavaScript,'Ubiquitous','Weard'
PHP, Simple,'Not very concise'
Python,Powerful,'2to3'
Overwriting data/data.csv
In [36]:
# Read again file
df = pandas.read_csv('data/data.csv')
In [37]:
df
Out[37]:
Langage Strengths Weaknesses
0 Java Structured Verbose
1 JavaScript 'Ubiquitous' 'Weard'
2 PHP Simple 'Not very concise'
3 Python Powerful '2to3'

.. et manipulate texts

In [38]:
import nltk
In [39]:
phrase="today, the weather is mild and I would like to go around the lake"
tokens=nltk.word_tokenize(phrase)
tokens
# Try completions nltk. ...
Out[39]:
['today',
 ',',
 'the',
 'weather',
 'is',
 'mild',
 'and',
 'I',
 'would',
 'like',
 'to',
 'go',
 'around',
 'the',
 'lake']

We can use bigger texts

In [40]:
from urllib import request
url='http://www.gutenberg.org/cache/epub/1497/pg1497.txt'
response = request.urlopen(url)
republic = response.read().decode('utf8')
type(republic)

# to go through a proxy:
# proxies = {'http': 'http://www.myproxy.com:3128'}
# request.ProxyHandler(proxies)
Out[40]:
str
In [41]:
len(republic)
Out[41]:
1239079
In [42]:
republic[231900:232071]
Out[42]:
'And the steps of the ladder leading up to this highest or\r\nuniversal existence are the mathematical sciences, which also contain\r\nin themselves an element of the universal'
In [ ]: