python_cheat_sheet
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python_cheat_sheet [2010/04/27 12:19] – ginko | python_cheat_sheet [2015/01/02 13:03] (current) – ginko | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| * Encodage <code python># -*- coding: utf-8 -*-</ | * Encodage <code python># -*- coding: utf-8 -*-</ | ||
| * Commentaire <code python>#</ | * Commentaire <code python>#</ | ||
| - | * Import <code python> | + | * Import <code python> |
| * Arguments (passés au script) | * Arguments (passés au script) | ||
| * Argv: (nécessite l' | * Argv: (nécessite l' | ||
| * Il existe aussi une implémentation de getopt (//cf//. [[http:// | * Il existe aussi une implémentation de getopt (//cf//. [[http:// | ||
| + | ===== Packaging ===== | ||
| + | * Encapsulation pour librairie autonome <code python> | ||
| + | main()</ | ||
| ===== Variables ===== | ===== Variables ===== | ||
| - | * Listes <code python> | + | * Listes |
| + | * Sets (mutable) fournit une interface implémentant les ensembles mathématiques (union, réunion, intersection, | ||
| * Ajouter un élément en fin de liste: <code python> | * Ajouter un élément en fin de liste: <code python> | ||
| + | * Tuple (immutable, mais plus performant) <code python> | ||
| + | * Matrices: installer et importer numpy ([[http:// | ||
| * Casts | * Casts | ||
| * String <code python> | * String <code python> | ||
| * Integer <code python> | * Integer <code python> | ||
| + | * Tuple <code python> | ||
| + | * Liste <code python> | ||
| * String délimité par ''''' | * String délimité par ''''' | ||
| + | * Sur plusieurs lignes <code python>""" | ||
| + | une string | ||
| + | sur plusieurs lignes"""</ | ||
| * Concaténation: | * Concaténation: | ||
| * Inclure des variables <code python> | * Inclure des variables <code python> | ||
| Line 26: | Line 37: | ||
| ===== IO ===== | ===== IO ===== | ||
| * Ouvrir un fichier <code python>#' | * Ouvrir un fichier <code python>#' | ||
| - | fichier=open('/ | + | fichier=open('/ |
| + | # il faut alors fermer le fichier | ||
| + | # une métode qui referme le fichier proprement: | ||
| + | with open('/ | ||
| + | </ | ||
| * '' | * '' | ||
| * Sortie vers le shell: <code python> | * Sortie vers le shell: <code python> | ||
| + | * Prompt au shell: <code python> | ||
| + | Pour travavailler avec les chemins de fichiers : '' | ||
| ===== Structure de code ===== | ===== Structure de code ===== | ||
| * Boucle '' | * Boucle '' | ||
| Line 35: | Line 52: | ||
| instruction1 | instruction1 | ||
| instruction2</ | instruction2</ | ||
| - | * Opérateur '' | + | * Opérateur '' |
| instruction</ | instruction</ | ||
| instruction</ | instruction</ | ||
| + | * '' | ||
| + | 1 : sqr, | ||
| + | 4 : sqr, | ||
| + | 9 : sqr, | ||
| + | 2 : even, | ||
| + | 3 : prime, | ||
| + | 5 : prime, | ||
| + | 7 : prime, | ||
| + | } | ||
| + | |||
| + | def zero(): | ||
| + | print "You typed zero.\n" | ||
| + | |||
| + | def sqr(): | ||
| + | print "n is a perfect square\n" | ||
| + | |||
| + | def even(): | ||
| + | print "n is an even number\n" | ||
| + | |||
| + | def prime(): | ||
| + | print "n is a prime number\n"</ | ||
| * '' | * '' | ||
| * '' | * '' | ||
| - | * '' | + | * '' |
| | | ||
| except Error: | except Error: | ||
| - | instructions</ | + | instructions</ |
| * Arrêter le script <code python> | * Arrêter le script <code python> | ||
| * Classe: <code python> | * Classe: <code python> | ||
| - | * Fonction: <code python> | + | * Fonction: <code python> |
| + | * Expansion des arguments dans un appel de fonction:< | ||
| + | function_name(*args) | ||
| + | kwargs = {' | ||
| + | function_name(**kwargs)</ | ||
| + | * Fonction anonyme (jettable, pour usage unique):< | ||
| * Test: <code python> | * Test: <code python> | ||
python_cheat_sheet.1272363556.txt.gz · Last modified: 2010/04/27 12:19 by ginko
