Premiers pas et primitives

Via l’interpréteur

Après l’installation de python ouvrir l’interpréteur python

Le typage est dynamique. primitive type

Python X.X.X
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mot = 'bonjour'
>>> type(mot)
<class 'str'>
>>> nombre = 2011
>>> type(nombre)
<class 'int'>
>>>

En python tout est objet, même les types de base. Pour connaitre les méthodes/attributs d’un objet,il faut utiliser la primitive dir

>>> mot = 'bonjour'
>>> dir(mot)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_formatter_field_name_split', '_formatter_parser',
'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace',
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> mot.title()
'Bonjour'
  • Toute donnée dans python est un objet

  • Tout objet possède:
    • un type (immuable) que l’on peut connaitre par la primitive type;

    • un identifiant que l’on peut connaitre par la primitive id;

    • une valeur (modifiable ou non).

Utilisation de print

>>> print(mot)
bonjour
>>> print('La formation commence')
La formation commence

Accès à la documentation:

>>> print(mot.strip.__doc__)
S.strip([chars]) -> string or unicode

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping