Python

Posted on sam. 24 novembre 2018 in Programmation

Python est un langage de programmation objet, multi-paradigme et multiplateformes. Il favorise la programmation impérative structurée, fonctionnelle et orientée objet. Il est doté d'un typage dynamique fort, d'une gestion automatique de la mémoire par ramasse-miettes et d'un système de gestion d'exceptions ; il est ainsi similaire à Perl, Ruby, Scheme, Smalltalk et Tcl.

Encodage

# -*- coding: utf-8 -*-

Affichage

Caractères spéciaux

Code Equuivalent
\n Retour à la ligne
\t Tabulation

Concaténation avec variables

>>> moutons = 42
>>> print "Il y a", moutons, "moutons."
Il y a 42 moutons.

Affichage avec formatage

>>> moutons = 42.0
# Entier
>>> print "Il y a %d moutons." % moutons
Il y a 42 moutons.
# Flottant
>>> print "Il y a %f moutons." % moutons
Il y a 42.000000 moutons.
# Chaine de caractères
>>> print "Il y a %s moutons." % moutons
Il y a 42.0 moutons.

Répétition

>>> print "." * 10
..........

Block de texte

>>> print """
... C'est l'histoire d'un mouton
... qui voulait être ami avec
... un pinguoin.
... """

C'est l'histoire d'un mouton
qui voulait être ami avec
un pinguoin.

Arguments

from sys import argv

script, first, second, third = argv

Fichier

Lecture

txt = open(filename)
print "Contenu du fichier :"
print txt.read()