import%20marimo%0A%0A__generated_with%20%3D%20%220.23.16%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Manipulation%20de%20fichiers%20et%20affichage%20format%C3%A9%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.%20Manipulation%20de%20fichiers%0A%0A%20%20%20%20%23%23%23%201.1.%20Ouverture%20et%20fermeture%20de%20fichiers%0A%0A%20%20%20%20%2F%2F%2F%20admonition%20%7C%20Ouverture%20et%20fermeture%20de%20fichiers%0A%20%20%20%20%20%20%20%20type%3A%20note%0A%0A%20%20%20%20-%20La%20fonction%20%60open()%60%20permet%20de%20cr%C3%A9er%20un%20objet%20%60file%60%20%C3%A0%20l%E2%80%99aide%20d%E2%80%99un%20nom%20d%E2%80%99un%20fichier%20et%20avec%20un%20mode.%20On%20peut%20%C3%A9galement%20pr%C3%A9ciser%20l'encodage%2C%20ce%20qui%20est%20plus%20propre%2C%20surtout%20en%20fran%C3%A7ais%20pour%20%C3%A9viter%20tout%20probl%C3%A8me%20avec%20les%20accents.%0A%20%20%20%20-%20La%20fonction%20%60.close()%60%20permet%20de%20fermer%20le%20fichier.%20La%20syntaxe%20g%C3%A9n%C3%A9rale%20(avec%20encodage%20utf8)%20est%20la%20suivante%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20f%20%3D%20open(%22nom_du_fichier.txt%22%2C%20mode%2C%20encoding%3Duft8)%0A%20%20%20%20%23%20travail%20sur%20le%20fichier%0A%20%20%20%20f.close()%0A%20%20%20%20%60%60%60%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%7C%20**Argument**%20%7C%20**Mode**%20%7C%0A%20%20%20%20%7C%3A------------%3A%7C%3A-----------------------------------------------------%3A%7C%0A%20%20%20%20%7C%20%20%20%20%20%60%22r%22%60%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lecture%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%7C%20%20%20%20%20%60%22w%22%60%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%C3%A9criture%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%7C%20%20%20%20%20%60%22a%22%60%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ajout%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%7C%20%20%20%20%20%60%22r%2B%22%60%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20lecture%20et%20%C3%A9criture%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%201.2.%20%C3%89criture%20dans%20un%20fichier%0A%0A%20%20%20%20-%20La%20fonction%20%60.write()%60%20permet%20d%E2%80%99%C3%A9crire%20une%20cha%C3%AEne%20dans%20le%20fichier%20et%20retourne%20le%20nombre%20de%20caract%C3%A8res%20%C3%A9crits.%0A%20%20%20%20-%20Il%20est%20aussi%20possible%20d%E2%80%99utiliser%20la%20fonction%20%60print()%60%20avec%20l%E2%80%99argument%20%60file%20%3D%60.%0A%20%20%20%20-%20Il%20est%20possible%20de%20proc%C3%A9der%20%C3%A0%20des%20%C3%A9critures%20s%C3%A9quentielles%20dans%20un%20fichier.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20f1%20%3D%20open(%22fichier.txt%22%2C%20%22w%22%2C%20encoding%3D'utf8')%20%23%20si%20le%20fichier%20n'existe%20pas%2C%20il%20sera%20cr%C3%A9%C3%A9%20automatiquement%0A%20%20%20%20s1%20%3D%20%22Bonjour%20tout%20le%20monde%5Cn%22%0A%20%20%20%20f1.write(s1)%0A%20%20%20%20l%20%3D%20%5B'a'%2C'b'%2C'c'%2C'd'%2C'e'%5D%0A%20%20%20%20f1.writelines(l)%20%23%20rajoute%20%C3%A0%20la%20fin%20du%20fichier%20les%20cha%C3%AEnes%20contenues%20dans%20l%0A%20%20%20%20f1.close()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20admonition%20%7C%20%60with%60%0A%20%20%20%20%20%20%20%20type%3A%20tip%0A%0A%20%20%20%20Les%20instructions%20%60with%20open(...)%60%20permet%20de%20cr%C3%A9er%20un%20fichier%20et%20de%20le%20fermer%20automatiquement%20(donc%20pas%20besoin%20de%20l%E2%80%99instruction%20%60.close()%60)%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20with%20open(%22exemple.txt%22%2C%20%22w%22%2C%20encoding%3D'utf8')%20as%20f%20%3A%20%23%20noter%20les%20%3A%20comme%20pour%20les%20structures%20de%20contr%C3%B4les%20et%20les%20boucles%0A%20%20%20%20%20%20%20%20print(%22Bonjour%20tout%20le%20monde%20%3A%5Cn%22%2C%20file%20%3D%20f)%20%23%20alternative%20a%20f.write(...)%0A%20%20%20%20%20%20%20%20f.write(%22On%20%C3%A9crit%20dans%20le%20fichier...%22)%0A%20%20%20%20%20%20%20%20f.write(%22%20et%20on%20n'est%20pas%20oblig%C3%A9%20de%20le%20fermer%20apr%C3%A8s%20open()%20%3A-)%22)%0A%20%20%20%20print(%22cette%20ligne%20n'appara%C3%AEt%20pas%20dans%20le%20fichier%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%201.3.%20Lecture%20d'un%20fichier%0A%0A%20%20%20%20Il%20existe%20plusieurs%20fa%C3%A7ons%20de%20lire%20un%20fichier%2C%20les%20principales%20%C3%A9tant%20r%C3%A9sum%C3%A9es%20dans%20le%20tableau%20ci-dessous.%0A%0A%20%20%20%20%7C%20**Fonction**%20%7C%20**Mode%20de%20lecture**%20%7C%0A%20%20%20%20%7C%3A------------%3A%7C%3A-----------------------------------------------------%3A%7C%0A%20%20%20%20%7C%20%20%20%20%20%60.read(n)%60%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lit%20au%20plus%20n%20caract%C3%A8res%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%7C%20%20%20%20%20%60.read()%60%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lit%20la%20totalit%C3%A9%20du%20fichier%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%7C%20%20%20%20%20%60.readline()%60%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lit%20une%20ligne%20%C3%A0%20la%20fois%20dans%20un%20str%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%7C%20%20%20%20%20%60.readlines()%60%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20lit%20toutes%20les%20lignes%20dans%20une%20list%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20with%20open(%22exemple.txt%22%2C%20%22r%22%2C%20encoding%3D'utf8')%20as%20file%20%3A%20%23%20on%20ouvre%20le%20fichier%20cr%C3%A9%C3%A9%20dans%20l'exemple%20pr%C3%A9c%C3%A9dent%0A%20%20%20%20%20%20%20%20lines%20%3D%20file.readlines()%0A%20%20%20%20%20%20%20%20print(lines)%20%23%20noter%20les%20retours%20%C3%A0%20la%20ligne%20%5Cn%20qui%20sont%20apparus%20lors%20de%20la%20lecture%20(sauf%20pour%20la%20derni%C3%A8re%20ligne)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%201.4.%20Lecture%20et%20enregistrement%20de%20donn%C3%A9es%20%C3%A0%20partir%20d'un%20fichier%0A%0A%20%20%20%20On%20termine%20avec%20un%20exemple%20qui%20montre%20comment%20enregistrer%2Flire%20des%20donn%C3%A9es%20depuis%20un%20fichier.%20Cela%20vous%20sera%20utile%20pour%20par%20exemple%20sauver%20les%20r%C3%A9sultats%20de%20simulations%20num%C3%A9riques%2C%20acc%C3%A9dez%20%C3%A0%20des%20mesures%20d'exp%C3%A9riences%2C%20etc...%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Exemple%201%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20data%20%3D%20%5B6.5%2C%208.3%2C%20-1.1%2C%2010.01%5D%0A%0A%20%20%20%20%23%20%C3%A9criture%20des%20donn%C3%A9es%20dans%20un%20fichier%2C%20ligne%20par%20ligne%0A%20%20%20%20with%20open%20(%22temperatures.txt%22%2C%20%22w%22%2C%20encoding%20%3D%20'utf8')%20as%20ff%20%3A%0A%20%20%20%20%20%20%20%20for%20value%20in%20data%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ff.write(str(value))%0A%20%20%20%20%20%20%20%20%20%20%20%20ff.write(%22%5Cn%22)%0A%0A%20%20%20%20%23%20chargement%20des%20donn%C3%A9es%20dans%20une%20liste%20par%20compr%C3%A9hension%0A%20%20%20%20with%20open%20(%22temperatures.txt%22%2C%20%22r%22%2C%20encoding%20%3D%20'utf8')%20as%20ff%20%3A%0A%20%20%20%20%20%20%20%20temperatures%20%3D%20%5Bfloat(ligne.strip())%20for%20ligne%20in%20ff%5D%0A%20%20%20%20print(temperatures)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Exemple%202%3A%20les%20donn%C3%A9es%20dans%20le%20fichier%20%60.txt%60%20sont%20s%C3%A9par%C3%A9es%20par%20des%20virgules.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20with%20open%20(%22temps.txt%22%2C%20%22r%22%2C%20encoding%20%3D%20%22utf8%22)%20as%20fff%20%3A%0A%20%20%20%20%20%20%20%20dat%20%3D%20fff.read()%0A%20%20%20%20%20%20%20%20temps%20%3D%20%5Bfloat(datum)%20for%20datum%20in%20dat.split(%22%2C%22)%5D%0A%20%20%20%20print(temps)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Enfin%2C%20il%20existe%20d'autres%20types%20de%20fichiers%20(%60.csv%60%20par%20exemple%2C%20un%20format%20compatible%20par%20exemple%20avec%20les%20tableurs)%20pour%20stocker%20des%20donn%C3%A9es%2C%20qui%20peuvent%20%C3%AAtre%20charg%C3%A9es%20automatiquement%20avec%20les%20librairies%20adapt%C3%A9es.%20On%20montre%20ci-dessous%20un%20exemple%20avec%20un%20fichier%20%60employes.csv%60%20qui%20contient%20les%20lignes%20suivantes%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20nom%2Cage%2Cville%2Csalaire%0A%20%20%20%20Alice%2C28%2CParis%2C45000%0A%20%20%20%20Bob%2C32%2CLyon%2C52000%0A%20%20%20%20Claire%2C25%2CMarseille%2C38000%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20La%20fonction%20%60csv.reader()%60%20lit%20le%20fichier%20ligne%20par%20ligne%20et%20retourne%20un%20iterateur.%20Chaque%20%C3%A9l%C3%A9ment%20est%20une%20liste%20contenant%20les%20valeurs%20de%20la%20ligne.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20csv%0A%0A%20%20%20%20return%20(csv%2C)%0A%0A%0A%40app.cell%0Adef%20_(csv)%3A%0A%20%20%20%20with%20open('employes.csv'%2C%20'r'%2C%20encoding%3D'utf-8')%20as%20fichier%3A%0A%20%20%20%20%20%20%20%20lecteur%20%3D%20csv.reader(fichier)%0A%20%20%20%20%20%20%20%20for%20ligne%20in%20lecteur%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(ligne)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20R%C3%A9ciproquement%2C%20on%20peut%20%C3%A9crire%20dans%20un%20csv%20avec%20%60csv.writer()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(csv)%3A%0A%20%20%20%20%23%20Donn%C3%A9es%20%C3%A0%20%C3%A9crire%20(une%20liste%20par%20ligne)%0A%20%20%20%20donnees_employes%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%5B'nom'%2C%20'age'%2C%20'ville'%2C%20'salaire'%5D%2C%0A%20%20%20%20%20%20%20%20%5B'Alice'%2C%20'28'%2C%20'Paris'%2C%20'45000'%5D%2C%0A%20%20%20%20%20%20%20%20%5B'Bob'%2C%20'32'%2C%20'Lyon'%2C%20'52000'%5D%2C%0A%20%20%20%20%20%20%20%20%5B'Claire'%2C%20'25'%2C%20'Marseille'%2C%20'38000'%5D%0A%20%20%20%20%5D%0A%0A%20%20%20%20with%20open('nouveaux_employes.csv'%2C%20'w'%2C%20newline%3D''%2C%20encoding%3D'utf-8')%20as%20fichier2%3A%0A%20%20%20%20%20%20%20%20writer%20%3D%20csv.writer(fichier2)%0A%20%20%20%20%20%20%20%20writer.writerows(donnees_employes)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20La%20m%C3%A9thode%20%60csv.DictReader()%60%20est%20plus%20pratique%20car%20elle%20retourne%20chaque%20ligne%20sous%20forme%20de%20dictionnaire%2C%20utilisant%20les%20en-t%C3%AAtes%20de%20la%20premi%C3%A8re%20ligne%20comme%20cl%C3%A9s%20pour%20un%20acc%C3%A8s%20plus%20intuitif.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(csv)%3A%0A%20%20%20%20with%20open('employes.csv'%2C%20'r'%2C%20encoding%3D'utf-8')%20as%20fichier3%3A%0A%20%20%20%20%20%20%20%20lect%20%3D%20csv.DictReader(fichier3)%0A%20%20%20%20%20%20%20%20for%20line%20in%20lect%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20affichage%20format%C3%A9%2C%20cf%20section%20suivante%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7Bline%5B'nom'%5D%7D%2C%20%7Bline%5B'age'%5D%7D%20ans%2C%20habite%20%C3%A0%20%7Bline%5B'ville'%5D%7D%20et%20gagne%20%7Bline%5B'salaire'%5D%7D%20%E2%82%AC%20par%20an%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Et%20on%20peut%20%C3%A9crire%20directement%20un%20dictionnaire%20dans%20un%20fichier%20%60.csv%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(csv)%3A%0A%20%20%20%20%23%20Donn%C3%A9es%20sous%20forme%20de%20dictionnaires%0A%20%20%20%20employes%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B'nom'%3A%20'Alice'%2C%20'age'%3A%20'28'%2C%20'ville'%3A%20'Paris'%2C%20'salaire'%3A%20'45000'%7D%2C%0A%20%20%20%20%20%20%20%20%7B'nom'%3A%20'Bob'%2C%20'age'%3A%20'32'%2C%20'ville'%3A%20'Lyon'%2C%20'salaire'%3A%20'52000'%7D%2C%0A%20%20%20%20%20%20%20%20%7B'nom'%3A%20'Claire'%2C%20'age'%3A%20'25'%2C%20'ville'%3A%20'Marseille'%2C%20'salaire'%3A%20'38000'%7D%0A%20%20%20%20%5D%0A%0A%20%20%20%20%23%20Ecriture%20avec%20DictWriter%20(produit%20le%20m%C3%AAme%20fichier%20qu'avec%20writer%20plus%20haut)%0A%20%20%20%20with%20open('employes_dict.csv'%2C%20'w'%2C%20newline%3D''%2C%20encoding%3D'utf-8')%20as%20fichier4%3A%0A%20%20%20%20%20%20%20%20fieldnames%20%3D%20%5B'nom'%2C%20'age'%2C%20'ville'%2C%20'salaire'%5D%0A%20%20%20%20%20%20%20%20writ%20%3D%20csv.DictWriter(fichier4%2C%20fieldnames%3Dfieldnames)%0A%20%20%20%20%20%20%20%20writ.writeheader()%0A%20%20%20%20%20%20%20%20writ.writerows(employes)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.%20Affichage%20format%C3%A9%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%202.1.%20La%20fonction%20%60.format()%60%0A%0A%20%20%20%20Le%20but%20du%20formatage%20est%20d%E2%80%99avoir%20plus%20de%20contr%C3%B4le%20sur%20le%20formatage%20des%20sorties%20et%20aller%20au-del%C3%A0%20d%E2%80%99un%20affichage%20de%20valeurs%20s%C3%A9par%C3%A9es%20par%20des%20espaces.%20Cela%20permet%20de%20cr%C3%A9er%20plus%20simplement%20des%20cha%C3%AEnes%20de%20caract%C3%A8res%20contenant%20une%20ou%20plusieurs%20variables%3A%20par%20la%20suite%2C%20cela%20sera%20utile%20pour%20nommer%20des%20graphiques%2C%20des%20fichiers%2C%20etc...%0A%0A%20%20%20%20Il%20y%20a%20plusieurs%20mani%C3%A8res%20de%20formater%20les%20sorties%20%3A%20avec%20utilisation%20de%20la%20fonction%20.format()%20et%20utilisation%20des%20_f-strings_.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20admonition%20%7C%20%60.format()%60%0A%20%20%20%20%20%20%20%20type%3A%20note%0A%0A%20%20%20%20La%20fonction%20%60s.format()%60%20permet%20d%E2%80%99ins%C3%A9rer%20les%20objets%20pass%C3%A9s%20en%20arguments%20dans%20la%20cha%C3%AEne%20%60s%60.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20nom%20%20%20%20%3D%20'Dupont'%0A%20%20%20%20prenom%20%3D%20'Antoine'%0A%20%20%20%20niveau%20%3D%20'Master%201'%0A%20%20%20%20c1%20%3D%20%22L'%C3%A9tudiant%20%7B%7D%20%7B%7D%20est%20en%20%7B%7D%22.format(prenom%2C%20nom%2C%20niveau)%0A%20%20%20%20print(c1)%0A%0A%20%20%20%20c2%20%3D%20%22%7B%7D%20%7B%7D%20%7B%7D%22.format(%22un%22%2C%20%22deux%22%2C%20%22trois%22)%0A%20%20%20%20print(c2)%0A%0A%20%20%20%20c3%20%3D%20%22%7B2%7D%20%7B0%7D%20%7B1%7D%22.format(%22un%22%2C%20%22deux%22%2C%20%22trois%22)%20%23%20noter%20l'emplacement%20pris%20par%20chacun%20des%20arguments%20de%20.format()%20!%0A%20%20%20%20print(c3)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Le%20formattage%20est%20outil%20tr%C3%A8s%20puissant%20pour%20cr%C3%A9er%20des%20cha%C3%AEnes%20de%20caract%C3%A8res.%20Quelques%20exemples%20suppl%C3%A9mentaires%20%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20print(%22Il%20s'appelle%20%7B%7B%7B%7D%7D%7D%22.format(%22Dupont%22))%0A%20%20%20%20print(%22%3E%7B%7D%3C%22.format(%22-%22*10))%0A%0A%20%20%20%20n%20%3D%2099%0A%20%20%20%20print(%22Il%20y%20a%20%7B%7D%20jours%22.format(n%2B1))%0A%0A%20%20%20%20a%2C%20b%20%3D%205%2C%203%0A%20%20%20%20print(%22a-b%20%3D%20%7Bd%7D%20et%20a%2Bb%20%3D%20%7Bc%7D%22.format(c%3Da%2Bb%2C%20d%3Da-b))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20On%20peut%20m%C3%AAme%20utiliser%20le%20formattage%20avec%20des%20dictionnaires.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20stock%20%3D%20%5B'papier'%2C%20'enveloppe'%2C%20'classeur'%2C%20'encre'%2C%20'buvard'%5D%0A%20%20%20%20s%20%3D%20%22Nous%20avons%20de%20l'%7B0%5B3%5D%7D%20et%20du%20%7B0%5B0%5D%7D%20en%20stock%22%0A%20%20%20%20print(s.format(stock))%0A%0A%20%20%20%20print(%22Il%20s'appelle%20%7B0%5Bname%5D%7D%22.format(dict(name%3D'Jean')))%0A%0A%20%20%20%20d%20%3D%20dict(poids%20%3D%2012000%2C%20animal%20%3D%20'%C3%A9l%C3%A9phant')%0A%20%20%20%20print(%22L'%7B0%5Banimal%5D%7D%20p%C3%A8se%20%7B0%5Bpoids%5D%7D%20kg%22.format(d))%0A%20%20%20%20return%20(stock%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20admonition%20%7C%20f-string%0A%20%20%20%20%20%20%20%20type%3A%20note%0A%0A%20%20%20%20Depuis%20Python%203.6%2C%20l'usage%20de%20la%20fonction%20%60.format()%60%20a%20%C3%A9t%C3%A9%20simplifi%C3%A9%20avec%20l%E2%80%99introduction%20des%20_f-strings_.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20m%20%3D%2099%0A%20%20%20%20print(%22J'ai%20%7B%7D%20photos%22.format(m%2B1))%0A%20%20%20%20print(f%22J'ai%20%7Bm%2B1%7D%20photos%22)%0A%0A%20%20%20%20toto%20%3D%20%22Dupont%22%0A%20%20%20%20print(f%22Bonjour%20%7Btoto.title()%7D!%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(stock)%3A%0A%20%20%20%20S%20%3D%20%22Nous%20avons%20de%20l'%7B0%5B3%5D%7D%20et%20du%20%7B0%5B0%5D%7D%20en%20stock%22%0A%20%20%20%20print(S.format(stock))%0A%20%20%20%20print(f%22Nous%20avons%20de%20l'%7Bstock%5B3%5D%7D%20et%20du%20%7Bstock%5B0%5D%7D%20en%20stock%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%202.2.%20Formatage%20num%C3%A9rique%0A%0A%20%20%20%20Le%20formattage%20peut%20aussi%20servir%20%C3%A0%20g%C3%A9rer%20l'affichage%20des%20nombres%2C%20par%20exemple%20pour%20afficher%20un%20nombre%20limit%C3%A9%20de%20d%C3%A9cimales%2C%20pour%20cr%C3%A9er%20des%20noms%20de%20fichiers%2C%20etc...%0A%0A%20%20%20%20%2F%2F%2F%20admonition%20%7C%20Formattage%20d'entiers%0A%20%20%20%20%20%20%20%20type%3A%20note%0A%0A%20%20%20%20Pour%20les%20entiers%2C%20on%20peut%20sp%C3%A9cifier%3A%0A%20%20%20%20-%20le%20format%20d'affichage%20des%20entiers%20avec%20%60d%60%2C%20%60b%60%2C%20%60o%60%2C%20%60x%60%20et%20%60X%60%0A%20%20%20%20-%20le%20remplissage%20par%20des%20z%C3%A9ros%20(en%20pr%C3%A9fixant%20la%20taille%20de%200)%0A%20%20%20%20-%20l%E2%80%99obligation%20de%20signe%20(avec%20%60%2B%60%2C%20%60-%60%20ou%20%60%E2%80%99%20%E2%80%99%60)%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%2F%2F%2F%20admonition%20%7C%20Formattage%20de%20flottans%0A%20%20%20%20%20%20%20%20type%3A%20note%0A%0A%20%20%20%20Pour%20les%20flottants%2C%20on%20peut%20utiliser%3A%0A%20%20%20%20-%20%60f%60%20pour%20notation%20fixe%0A%20%20%20%20-%20%60e%60%20(ou%20%60E%60)%20notation%20scientifique%0A%20%20%20%20-%20%60g%60%20(ou%20%60G%60)%20format%20g%C3%A9n%C3%A9ral%20(fixe%20dans%20%24%5B10%5E%7B%E2%88%924%7D%2C%2010%5E6%5D%24)%0A%20%20%20%20-%20%60%25%60%20pour%20une%20notation%20en%20pourcentage%0A%20%20%20%20-%20%60k%60%20d%C3%A9cimales%20en%20ajoutant%20un%20%60.k%60%20avant%20la%20lettre%20de%20format%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20Quelques%20exemples%20de%20formmatages%20num%C3%A9riques...%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20q%20%3D%20100%0A%20%20%20%20pi%20%3D%203.1415926535897931%0A%20%20%20%20k%20%3D%20-54%0A%20%20%20%20print(%22%7B%3A.4e%7D%22.format(pi))%20%23%20notation%20scientifique%20avec%204%20d%C3%A9cimales%20apr%C3%A8s%20la%20virgule%0A%20%20%20%20print(%22%7B%3Ag%7D%22.format(pi))%0A%20%20%20%20print(%22%7B%3A.2%25%7D%22.format(q%2F(47*pi)))%0A%20%20%20%20return%20k%2C%20pi%2C%20q%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20print(f%22%7B0.00000001%3Ag%7D%22)%0A%20%20%20%20print(f%22%7B1e-3%3Ag%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pi%2C%20q)%3A%0A%20%20%20%20msg%20%3D%20%22R%C3%A9sultat%20sur%20%7B%3Ad%7D%20%C3%A9chantillons%20%3A%20%7B%3A.2f%7D%22.format(q%2C%20pi)%0A%20%20%20%20print(msg)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20st%20%3D%20%22%7B0.real%7D%20et%20%7B0.imag%7D%20sont%20les%20composantes%20du%20complex%20%7B0%7D%20%22%0A%20%20%20%20msg2%20%3D%20st.format(3-5j)%0A%20%20%20%20print(msg2)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(k%2C%20q)%3A%0A%20%20%20%20print(%22%7B%3A%2Bd%7D%20%7B%3A%2Bd%7D%22.format(q%2C%20k))%0A%20%20%20%20print(%22%7B%3A-d%7D%20%7B%3A-d%7D%22.format(q%2C%20k))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20print(%22virgules%20des%20milliers%20%3A%20%7B%3A%2C%7D%22.format(1234567890.123))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20nn%20%3D%20100%0A%20%20%20%20print(%22J'ai%20%7B%7D%20photos%22.format(nn))%0A%20%20%20%20print(%22J'ai%20%7B%3Ad%7D%20photos%22.format(nn))%0A%20%20%20%20mm%20%3D%20100.5%0A%20%20%20%20print(%22J'ai%20%7B%7D%20photos%22.format(mm))%0A%20%20%20%20print(%22J'ai%20%7B%3Ad%7D%20photos%22.format(mm))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Quelques%20exemples%20bien%20utiles%20pour%20g%C3%A9rer%20l'alignement%20de%20plusieurs%20chaines%20de%20caract%C3%A8res...%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20spi%20%3D%20%22Le%20nombre%20Pi%22%0A%20%20%20%20print(%22%5B%7B%7D%5D%22.format(spi))%0A%20%20%20%20print(%22%5B%7B%3A25%7D%5D%22.format(spi))%0A%20%20%20%20print(%22%5B%7B%3A%3E25%7D%5D%22.format(spi))%0A%20%20%20%20print(%22%5B%7B%3A%5E25%7D%5D%22.format(spi))%0A%20%20%20%20print(%22%5B%7B%3A%3C25%7D%5D%22.format(spi))%0A%20%20%20%20print(%22%5B%7B%3A-%3C25%7D%5D%22.format(spi))%0A%20%20%20%20lng%20%3D%209%0A%20%20%20%20print(%22%5B%7B%7D%5D%22.format(spi%5B%3Alng%5D))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20num_tel%20%3D%20%7B33606081033%20%3A%20%22Nicolas%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2033698003821%20%3A%20%22Victor%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2033606439889%20%3A%20%22Rudi%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2033699999999%20%3A%20%22Tony%22%7D%0A%20%20%20%20for%20numero%2C%20name%20in%20num_tel.items()%20%3A%0A%20%20%20%20%20%20%20%20print(f%22%7Bname%3A10%7D%20%3A%20%7Bnumero%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Enfin%2C%20un%20exemple%20de%20g%C3%A9n%C3%A9ration%20de%20plusieurs%20noms%20de%20fichiers%20de%20fa%C3%A7on%20automatique%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20for%20i%20in%20range(20)%3A%0A%20%20%20%20%20%20%20%20print(%22fichresult_%7B%3A0%3D12%7D.txt%22.format(i))%0A%20%20%20%20print(%22Et%20on%20peut%20continuer...%20!%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2Fadmonition%20%7C%20Documentation%0A%20%20%20%20%20%20%20%20type%3A%20tip%0A%0A%20%20%20%20Vous%20l'aurez%20compris%2C%20le%20formatage%20est%20un%20outil%20tr%C3%A8s%20puissant.%20Voici%20quelques%20liens%20de%20la%20documentation%20officielle%20sur%20les%20possibilit%C3%A9s%20offertes%3A%0A%20%20%20%20-%20https%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Fstring.html%23format-specification-mini-language%0A%20%20%20%20-%20https%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Fstring.html%23format-examples%0A%0A%20%20%20%20Pour%20les%20f_strings%3A%0A%20%20%20%20-%20https%3A%2F%2Fdocs.python.org%2Ffr%2F3%2Ftutorial%2Finputoutput.html%23formatted-string-literals%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
c54907e2526a6d234d9c60a6a853b0f1