Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révisionLes deux révisions suivantes |
teaching:progappchim:notions_fondamentales [2021/01/18 11:57] – [D'autres types] villersd | teaching:progappchim:notions_fondamentales [2021/02/13 18:08] – [Variables locales et globales] villersd |
---|
====== Notions fondamentales ====== | ====== Notions fondamentales ====== |
Aide mémoire synthétique sur le langage Python. Les [[https://dvillers.umons.ac.be/wiki/floss:python#de_python_2_a_python_3|différences importantes entre la branche 2 et la branche 3]] seront commentées. La différence la plus fréquente est le passage de print à print() ! | Aide mémoire synthétique sur le langage Python. |
| |
===== Règles de base ===== | ===== Règles de base ===== |
//Cf.// [[http://www.courspython.com/boucles.html|cet autre cours]] avec des illustrations et des exemples de code exécutable sur le site [[http://www.pythontutor.com|pythontutor]]. | //Cf.// [[http://www.courspython.com/boucles.html|cet autre cours]] avec des illustrations et des exemples de code exécutable sur le site [[http://www.pythontutor.com|pythontutor]]. |
| |
FIXME : ajouter d'autres possibilités telles que présentées dans [[https://medium.com/analytics-vidhya/looping-techniques-in-python-3bbf907b8dfa|Looping Techniques in Python - Let’s learn about looping techniques using functions like enumerate, zip, sorted, reversed in python]] Indhumathy Chelliah; Medium, 30/07/2020 | FIXME : ajouter d'autres possibilités telles que : |
| * [[https://medium.com/analytics-vidhya/looping-techniques-in-python-3bbf907b8dfa|Looping Techniques in Python - Let’s learn about looping techniques using functions like enumerate, zip, sorted, reversed in python]] Indhumathy Chelliah; Medium, 30/07/2020 |
| * [[https://medium.com/better-programming/how-to-use-for-loops-better-in-python-1dfbc3d9e91f|How To Use For Loops Better in Python - A few functions that can improve your looping logic]] Yong Cui, Medium, Jan 8, 2020 |
| |
---- | ---- |
* Conversion en nombre (donnée numérique créée à partir d'une chaîne de caractères) : int("587"), float("3.14") | * Conversion en nombre (donnée numérique créée à partir d'une chaîne de caractères) : int("587"), float("3.14") |
* Pour la concaténation d'une liste de chaîne, la fonction join est plus adaptée que "+" (cf. [[https://towardsdatascience.com/do-not-use-to-join-strings-in-python-f89908307273|ici]]) : | * Pour la concaténation d'une liste de chaîne, la fonction join est plus adaptée que "+" (cf. [[https://towardsdatascience.com/do-not-use-to-join-strings-in-python-f89908307273|ici]]) : |
<code> | <code python> |
strings = ['A', 'bac', 'cali', 'jkppl'] | strings = ['A', 'bac', 'cali', 'jkppl'] |
text = ''.join(strings) | text = ''.join(strings) |
s.join(l) # joins a list or string l with substring s | s.join(l) # joins a list or string l with substring s |
</code> | </code> |
| |
| === Applications à la détection de palindromes et anagrammes === |
| |
| |
| <code python string-palindrome-01.py> |
| #!/usr/bin/env python3 |
| # -*- coding: utf-8 -*- |
| """ |
| Created on Mon Jan 25 10:58:37 2021 |
| |
| @author: villersd |
| |
| Un palindrome est un texte ou une séquence plus générale (notes de musique, |
| code génétique,...), dont l'ordre des lettres (ou des notes,...) reste le même |
| qu'on le lise de gauche à droite ou de droite à gauche. |
| Source : https://fr.wikipedia.org/wiki/Palindrome |
| """ |
| import unicodedata |
| |
| def palindrome(string_to_check): |
| if string_to_check.lower().replace(' ', '') == string_to_check.lower().replace(' ', '')[::-1]: |
| return True |
| else: |
| return False |
| |
| def remove_accents(input_str): |
| """ |
| Les lettres accentuées viennent compliquer le problème... |
| cf. https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-normalize-in-a-python-unicode-string |
| """ |
| nfkd_form = unicodedata.normalize('NFKD', input_str) |
| return u"".join([c for c in nfkd_form if not unicodedata.combining(c)]) |
| |
| |
| string0 = 'Esope reste ici et se repose' |
| print(string0, palindrome(string0)) |
| |
| string1 = 'Ésope reste ici et se repose' |
| print(string1, palindrome(string1)) |
| |
| print(string1, remove_accents(string1), palindrome(remove_accents(string1))) |
| |
| </code> |
| |
| <code python string-anagramme-00.py> |
| #!/usr/bin/env python3 |
| # -*- coding: utf-8 -*- |
| """ |
| Created on Mon Jan 25 11:42:18 2021 |
| |
| @author: villersd |
| |
| Une anagramme est un mot ou une expression obtenu en permutant les lettres |
| d'un mot ou d'une expression de départ |
| Source : https://fr.wikipedia.org/wiki/Anagramme |
| """ |
| string1 = 'manoir' |
| string2 = 'romain' |
| |
| print(string1, sorted(string1)) |
| print(string2, sorted(string2)) |
| |
| # exercice : utiliser cette fonction sorted() et appliquer les transformations |
| # de la fonction de vérification de palindromes pour détecter une anagramme. |
| |
| </code> |
| |
| |
=== Références === | === Références === |
* [[https://towardsdatascience.com/the-most-undervalued-standard-python-library-14021632f692|The Most Undervalued Standard Python Library Collections for data scientists]] Tyler Folkman, Medium, Oct 26, 2019 (video) | * [[https://towardsdatascience.com/the-most-undervalued-standard-python-library-14021632f692|The Most Undervalued Standard Python Library Collections for data scientists]] Tyler Folkman, Medium, Oct 26, 2019 (video) |
* [[https://levelup.gitconnected.com/introducing-high-performance-datatypes-in-python-with-the-collections-library-3d8c334827a5|Introducing high-performance datatypes in Python with the collections library]] George Seif, Medium, Oct 15, 2019 | * [[https://levelup.gitconnected.com/introducing-high-performance-datatypes-in-python-with-the-collections-library-3d8c334827a5|Introducing high-performance datatypes in Python with the collections library]] George Seif, Medium, Oct 15, 2019 |
| * [[https://medium.com/better-programming/your-one-stop-guide-to-collections-in-python-4c0b36243561|Your One-Stop Guide to Collections in Python - Make your code awesome using high-order containers]] Felix Antony, Medium, 21/01/2021 |
| |
Des types non intégrés par défaut dans Python peuvent facilement être implémentés, en utilisant les types répandus. C'est pas exemple le cas des [[https://fr.wikipedia.org/wiki/Arbre_enracin%C3%A9|arbres]] (informatique, théorie des graphes) : | Des types non intégrés par défaut dans Python peuvent facilement être implémentés, en utilisant les types répandus. C'est pas exemple le cas des [[https://fr.wikipedia.org/wiki/Arbre_enracin%C3%A9|arbres]] (informatique, théorie des graphes) : |
* Autres références : | * Autres références : |
* [[https://towardsdatascience.com/global-local-and-nonlocal-variables-in-python-6b11c20d73b0|Global, Local and Nonlocal variables in Python]] | * [[https://towardsdatascience.com/global-local-and-nonlocal-variables-in-python-6b11c20d73b0|Global, Local and Nonlocal variables in Python]] |
| * [[https://towardsdatascience.com/many-python-programmers-cannot-solve-this-puzzle-c5950841d14d|Many Python Programmers Cannot Solve This Puzzle - A brief introduction to “Python under the hood” for beginners]] Naser Tamimi, Medium, Dec 23, 2020 |
==== Passage d'arguments par tuples et dictionnaires ==== | ==== Passage d'arguments par tuples et dictionnaires ==== |
* Les arguments d'une fonction peuvent être transmis via un tuple en préfixant le nom du tuple par le symbole * (on utilise en général l'identifiant "*args" pour le tuple) | * Les arguments d'une fonction peuvent être transmis via un tuple en préfixant le nom du tuple par le symbole * (on utilise en général l'identifiant "*args" pour le tuple) |