Python basics for NLP
Vincent Claveau IRISA-CNRS
Programming environment
Is Python installed
– Type : which python3 in the command prompt (applications->utilitaires->Terminal) – It should answer: /usr/bin/python3
Text file editor like emacs, gedit...
– Create a new document named hello.py, save it as pure txt (not rtf, doc...) and type:
#!/usr/bin/python3 #coding: utf-8 print('hello world\n')
Programming environment
Go in the directory where you saved the file
– Windows: cd Dekstop cd .. – Unix/Mac: ls pwd
Make hello.py an executable
– Type chmod u+x hello.py in the command prompt
Run your program
– ./hello.py
Python basics
Data structures
Scalar: var
– integer: var = 3 float: var = 7.456 – string: var = 'I love Python' – boolean: var = True var = False
List (table, array...)
t_misc = ['titi', 3.1415, var] t_misc.append('toto') t_misc[0] = 'tutu'
Dictionary (associative array, hash...)
h_misc2 = { 'pi': 3.1415, 12:'December' } h_misc2['vincent'] = 'claveau'
Python basics
Programming structures
- Conditional structures (note the indent)
if nb == 5:
…
elif line == “blabla\n” or 3 in t_prime:
…
else
…
Python basics
Programming structures
- Loops