issue78:tuto_python
                Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| issue78:tuto_python [2013/12/07 12:17] – créée andre_domenech | issue78:tuto_python [2014/03/22 20:14] (Version actuelle) – fredphil91 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ==== 1 ==== | ||
| + | ** | ||
| Welcome back. It’s hard to imagine that it’s been 4 years since I began this series. I thought that I’d shelve the media manager project for a bit and return to some basics of Python programming. | Welcome back. It’s hard to imagine that it’s been 4 years since I began this series. I thought that I’d shelve the media manager project for a bit and return to some basics of Python programming. | ||
| Ligne 14: | Ligne 16: | ||
| Python 2.x | Python 2.x | ||
| + | ** | ||
| + | Bienvenue à nouveau. | ||
| + | |||
| + | Il est difficile d' | ||
| + | |||
| + | Ce mois-ci, je vais revisiter la commande d' | ||
| + | |||
| + | Puisque la syntaxe de la fonction print est différente entre Python 2.x et 3.x, nous allons les examiner séparément. Rappelez-vous, | ||
| + | |||
| + | >>> | ||
| + | |||
| + | >>> | ||
| + | |||
| + | et la sortie sera en gras, comme ceci : | ||
| + | |||
| + | La chaine a est Salut Python | ||
| + | |||
| + | Python 2.x | ||
| + | |||
| + | ** | ||
| Of course you remember the simple syntax for the print function in 2.x uses the variable substitution of %s or %d for simple strings or decimals. But many other formatting options are available. For example, if you need to format a number with leading zeros, you can do it this way: | Of course you remember the simple syntax for the print function in 2.x uses the variable substitution of %s or %d for simple strings or decimals. But many other formatting options are available. For example, if you need to format a number with leading zeros, you can do it this way: | ||
| Ligne 27: | Ligne 49: | ||
| PI = 3.142. | PI = 3.142. | ||
| + | ** | ||
| + | Bien sûr, vous vous souvenez que la syntaxe simple pour la fonction print en 2.x utilise la substitution de variable de %s ou %d pour les chaînes simples ou les nombres décimaux. Mais beaucoup d' | ||
| + | |||
| + | >>> | ||
| + | Votre valeur est 004 | ||
| + | |||
| + | Dans ce cas, nous utilisons la commande de format ' | ||
| + | |||
| + | >>> | ||
| + | |||
| + | >>> | ||
| + | |||
| + | PI = 3.142. | ||
| + | |||
| + | ==== 2 ==== | ||
| + | ** | ||
| Here we use the float formatting option. The ‘%5.3f’ says to produce an output with a total width of five and three decimal places. Notice that the decimal point takes up one of the places of the total width. | Here we use the float formatting option. The ‘%5.3f’ says to produce an output with a total width of five and three decimal places. Notice that the decimal point takes up one of the places of the total width. | ||
| Ligne 39: | Ligne 77: | ||
| The following table shows the various possible substitution keys and their meanings. | The following table shows the various possible substitution keys and their meanings. | ||
| + | ** | ||
| + | Ici, nous utilisons l' | ||
| + | |||
| + | Une autre chose que vous pourriez ne pas avoir réalisé est que vous pouvez utiliser les clés d'un dictionnaire dans le cadre de la commande format. | ||
| + | |||
| + | >>> | ||
| + | |||
| + | >>> | ||
| + | |||
| + | Bienvenue Fred Farkel de Denver! | ||
| + | |||
| + | Le tableau suivant présente les différentes clés de substitution possibles et leurs significations. | ||
| + | |||
| + | ** | ||
| Python 3.x | Python 3.x | ||
| Ligne 57: | Ligne 109: | ||
| This version of Python is 3.3.2 | This version of Python is 3.3.2 | ||
| + | ** | ||
| + | |||
| + | Python 3.x | ||
| + | |||
| + | Avec Python 3.x, nous avons beaucoup plus d' | ||
| + | |||
| + | Pour vous rafraîchir la mémoire, voici un exemple simple de la fonction print en 3.x. | ||
| + | |||
| + | >>> | ||
| + | Salut Python | ||
| + | |||
| + | >>> | ||
| + | Python est SUPER sympa ! | ||
| + | |||
| + | Les champs de remplacement sont enfermés dans des accolades " | ||
| + | |||
| + | >>> | ||
| + | Cette version de Python est 3.3.2 | ||
| + | |||
| + | |||
| + | ==== 3 ==== | ||
| + | ** | ||
| As they say on the TV ads, “BUT WAIT… THERE’S MORE”. If we wanted to do some inline formatting, we have the following options. | As they say on the TV ads, “BUT WAIT… THERE’S MORE”. If we wanted to do some inline formatting, we have the following options. | ||
| Ligne 71: | Ligne 145: | ||
| >>> | >>> | ||
| | | | | ||
| + | ** | ||
| + | Comme ils le disent sur les publicités télévisées, | ||
| + | |||
| + | :<x alignement à gauche avec une largeur de x | ||
| + | :>x alignement à droite avec une largeur de x | ||
| + | :^x alignement centré avec une largeur de x | ||
| + | |||
| + | Voici un exemple : | ||
| + | |||
| + | >>> | ||
| + | |A gauche | ||
| + | >>> | ||
| + | | A droite| | ||
| + | >>> | ||
| + | | | ||
| + | |||
| + | |||
| + | ** | ||
| You can even specify a fill character along with the justification/ | You can even specify a fill character along with the justification/ | ||
| Ligne 93: | Ligne 185: | ||
| Well, that should give you enough food for thought for this month. I’ll see you at the start of the 5th year. | Well, that should give you enough food for thought for this month. I’ll see you at the start of the 5th year. | ||
| + | ** | ||
| + | |||
| + | Vous pouvez même spécifier un caractère de remplissage avec la justification ou la largeur : | ||
| + | |||
| + | >>> | ||
| + | *****321.4 | ||
| + | |||
| + | Si vous avez besoin de formater une sortie de date ou d' | ||
| + | |||
| + | >>> | ||
| + | |||
| + | >>> | ||
| + | 29/10/13 | ||
| + | |||
| + | >>> | ||
| + | 10:45:01 | ||
| + | |||
| + | Afficher le séparateur des milliers en utilisant une virgule (ou tout autre caractère) est simple. | ||
| + | |||
| + | >>> | ||
| + | Voici un grand nombre 7.219.219.281 | ||
| + | |||
| + | Eh bien, cela devrait vous donner assez de nourriture pour la pensée pendant ce mois. Je vous revois au début de la 5e année. | ||
issue78/tuto_python.1386415078.txt.gz · Dernière modification : 2013/12/07 12:17 de andre_domenech
                
                