teaching:progappchim:notions_fondamentales

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
teaching:progappchim:notions_fondamentales [2022/05/02 08:52] – [Lire et écrire dans des fichiers] villersdteaching:progappchim:notions_fondamentales [2023/05/03 08:39] (Version actuelle) – [Variables locales et globales] villersd
Ligne 100: Ligne 100:
   * [[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   * [[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
   * [[https://medium.com/techtofreedom/the-art-of-writing-loops-in-python-68e9869e4ed4|The Art of Writing Loops in Python - Simple is better than complex]] Yang Zhou, Medium, 03/05/2021   * [[https://medium.com/techtofreedom/the-art-of-writing-loops-in-python-68e9869e4ed4|The Art of Writing Loops in Python - Simple is better than complex]] Yang Zhou, Medium, 03/05/2021
 +  * [[https://towardsdatascience.com/the-art-of-speeding-up-python-loop-4970715717c|The Art of Speeding Up Python Loop]] Casey Cheng, Oct 2022, Towards Data Science
 +
  
 ---- ----
Ligne 206: Ligne 208:
 </code> </code>
  
-=== Applications à la détection de palindromes et anagrammes ===+=== Applications à la détection de palindromesanagrammes et pangrammes ===
  
  
Ligne 270: Ligne 272:
 # de la fonction de vérification de palindromes pour détecter une anagramme. # de la fonction de vérification de palindromes pour détecter une anagramme.
  
 +</code>
 +
 +<code python  string_pangrammes-01.py>
 +#!/usr/bin/env python3
 +# -*- coding: utf-8 -*-
 +"""
 +Created on Mon May  9 11:54:40 2022
 +
 +@author: villersd
 +
 +Test d'une chaîne pour connaître le nombre de lettres de l'apahabet utilisées
 +et si elles le sont toutes (pangramme).
 +ref : https://fr.wikipedia.org/wiki/Pangramme
 +
 +"""
 +import string
 +print('ASCII letters : ', string.ascii_lowercase)
 +
 +# test string :
 +ts = "Portez ce vieux whisky au juge blond qui fume"
 +
 +# dict count strategy
 +letter_count_dict = dict( (key, ts.lower().count(key)) for key in string.ascii_lowercase )
 +print(letter_count_dict)
 +
 +# list count strategy
 +letter_count_list = [ts.lower().count(key) for key in string.ascii_lowercase]
 +print(letter_count_list)
 +
 +# using all()
 +print(all(letter_count_list))
 +
 +# one-liner : 
 +print(all([ts.lower().count(key) for key in string.ascii_lowercase]))
 +print("All ASCII letters : ", all(["Portez ce vieux whisky au juge blond qui fume".lower().count(key) for key in string.ascii_lowercase]))
 </code> </code>
  
 Pour une technique de détection utilisant les nombres premiers : [[https://mobile.twitter.com/fermatslibrary/status/1385957963429515266]] (programmer et comparer !) Pour une technique de détection utilisant les nombres premiers : [[https://mobile.twitter.com/fermatslibrary/status/1385957963429515266]] (programmer et comparer !)
 +
 +=== Chaînes préfixées ===
 +Les chaînes pêuvent être préfixées, pour tenir compte de types et d'utilisations particulières :
 +
 +^Prefix  ^Utilisation  ^Exemple  ^
 +|None  |chaîne de caractère habituelle  |"Hello world !"  |
 +|r  |raw string (utilisant plusieurs caractères \ s'échappement)  |print(r"C:\Users\johndoe\documents" |
 +|b  |chaîne binaire  |b"byte string"  |
 +|u  |chaîne unicode  |u"Unicode string"  |
 +|f  |chaîne de formatage fstring  |print(f"My cool string is called {name.upper()}." |
  
 === Références === === Références ===
Ligne 292: Ligne 339:
   * [[https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not|les opérateurs booléens]]   * [[https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not|les opérateurs booléens]]
   * [[https://docs.python.org/3/library/stdtypes.html#comparisons|les opérateurs de comparaison]]   * [[https://docs.python.org/3/library/stdtypes.html#comparisons|les opérateurs de comparaison]]
 +    * Les 6 opérateurs ==, !=, <, >, <=, >= sont destinés à comparer des valeurs d'objets (cf. les instructions conditionnelles,...)
 +    * Les opérateurs **is** et **is not** sont destinés à comparer l'identité d'objets, en particulier des objets uniques (singletons) du langage, comme **None**
  
 ==== Les listes ==== ==== Les listes ====
Ligne 341: Ligne 390:
   * [[https://www.datacamp.com/community/tutorials/python-list-comprehension|Python List Comprehension Tutorial]]   * [[https://www.datacamp.com/community/tutorials/python-list-comprehension|Python List Comprehension Tutorial]]
   * list comprehension,... : [[https://tgits.github.io/post/notes_apprentissage_python_comprehension/|Notes d'apprentissage de Python : les compréhensions]]   * list comprehension,... : [[https://tgits.github.io/post/notes_apprentissage_python_comprehension/|Notes d'apprentissage de Python : les compréhensions]]
 +  * [[https://towardsdatascience.com/reverse-python-list-ad10ad408021|How To Reverse Python Lists More Efficiently - Efficient reversal of lists in Python]] Giorgos Myrianthous, Medium, 07/10/2022
 +
  
 ==== Les tuples ==== ==== Les tuples ====
Ligne 348: Ligne 399:
 Référence : Référence :
   * [[https://www.datacamp.com/community/tutorials/python-tuples-tutorial|Python Tuples Tutorial]]   * [[https://www.datacamp.com/community/tutorials/python-tuples-tutorial|Python Tuples Tutorial]]
 +  * [[https://towardsdatascience.com/python-tuple-the-whole-truth-and-only-the-truth-hello-tuple-12a7ab9dbd0d|Python Tuple, the Whole Truth, and Only the Truth: Hello, Tuple! - Learn the basics of tuples and of using them]] Marcin Kozak, Towards Data Science (Medium), 21/01/2023
 +
 ==== Les dictionnaires ==== ==== Les dictionnaires ====
 //Cf.// la [[https://docs.python.org/3/library/stdtypes.html#mapping-types-dict|documentation officielle]] //Cf.// la [[https://docs.python.org/3/library/stdtypes.html#mapping-types-dict|documentation officielle]]
Ligne 399: Ligne 452:
 </code> </code>
  
 +Références complémentaires :
 +  * [[https://stackoverflow.com/questions/2541752/best-way-to-find-the-intersection-of-multiple-sets|Best way to find the intersection of multiple sets?]]
 ==== D'autres types ==== ==== D'autres types ====
 Des types "haute-performance" sont aussi intégrés à Python, via le module "collections" à importer : Des types "haute-performance" sont aussi intégrés à Python, via le module "collections" à importer :
Ligne 567: Ligne 622:
     * [[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     * [[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
 +    * [[https://tonylixu.medium.com/python-different-ways-to-call-function-e6f37bafefcf|Python — Different Ways to Call Function
 +How to call a function in Python?]] Tony, Medium, 19/04/2023
 ==== 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)
   * Les arguments d'une fonction peuvent être transmis via un dictionnaire dont les clés correspondent aux arguments nommés dans la définition de la fonction, en préfixant le nom du dictionnaire par les %%**%% (on utilise en général l'identifiant "%%**%%kwargs" pour le dictionnaire)   * Les arguments d'une fonction peuvent être transmis via un dictionnaire dont les clés correspondent aux arguments nommés dans la définition de la fonction, en préfixant le nom du dictionnaire par les %%**%% (on utilise en général l'identifiant "%%**%%kwargs" pour le dictionnaire)
 +  * cf. [[https://towardsdatascience.com/args-kwargs-python-d9c71b220970|*args and **kwargs in Python - Discussing the difference between positional and keyword arguments and how to use *args and **kwargs in Python]] Giorgos Myrianthous, Towards Data Science, Jun 22 2022
  
 === Passage par tuple === === Passage par tuple ===
Ligne 621: Ligne 679:
 plat : poularde plat : poularde
 </code> </code>
 +
 +Références :
 +  * [[https://medium.com/@mikehuls/python-args-kwargs-and-all-other-ways-to-pass-arguments-to-your-function-bd2acdce72b5|Python args, kwargs, and All Other Ways to Pass Arguments to Your Function - Expertly design your function parameters in 6 examples]] Mike Huls, Medium, 07/03/2023
 +  * [[https://python.plainenglish.io/args-and-kwargs-in-python-demystified-317fb1961720|*args And **kwargs In Python Demystified | by Liu Zuo Lin | Mar, 2023 | Python in Plain English]] Liu Zuo Lin, Medium, 26/03/2023
 ===== Modules de fonctions ===== ===== Modules de fonctions =====
 Des fonctions ou simplement des déclarations de variables peuvent être définies et regroupées dans un fichier (.py), et ensuite renseignées pour leur utilisation dans un programme grâce à la directive d'importation. Des fonctions ou simplement des déclarations de variables peuvent être définies et regroupées dans un fichier (.py), et ensuite renseignées pour leur utilisation dans un programme grâce à la directive d'importation.
Ligne 696: Ligne 758:
   * [[https://datawhatnow.com/things-you-are-probably-not-using-in-python-3-but-should/]] (f-strings vs format)   * [[https://datawhatnow.com/things-you-are-probably-not-using-in-python-3-but-should/]] (f-strings vs format)
   * [[https://python.plainenglish.io/become-a-master-of-string-formatting-in-python3-252334a8269a|Become a Master of String Formatting in Python3]] by Maxence LQ, Python in Plain English, 25/05/2021   * [[https://python.plainenglish.io/become-a-master-of-string-formatting-in-python3-252334a8269a|Become a Master of String Formatting in Python3]] by Maxence LQ, Python in Plain English, 25/05/2021
 +  * [[https://ibexorigin.medium.com/have-mercy-on-yourself-and-learn-these-9-everyday-python-f-string-codes-e8104677d7c7|Have Mercy on Yourself And Learn These 9 Everyday Python f-string Codes]] Bex. T., Medium, 14/03/2023
  
  
Ligne 737: Ligne 800:
   * Try ... Except :   * Try ... Except :
     * [[https://medium.com/better-programming/how-to-start-using-try-statements-in-python-5043fe69058d]]     * [[https://medium.com/better-programming/how-to-start-using-try-statements-in-python-5043fe69058d]]
 +    * [[https://medium.com/@niklas_lang/exception-handling-in-python-8cc8f69f16ad|Exception Handling in Python. Understanding how to use Python Try… ]] by Niklas Lang, October 2022, Towards Data Science
   * https://realpython.com/python-keyerror/   * https://realpython.com/python-keyerror/
   * ...   * ...
Ligne 864: Ligne 928:
   * [[https://levelup.gitconnected.com/10-python-tips-for-better-code-1bbffde3b44d|10 Python Tips For Better Code]] (Abhay Parashar, Medium, 17/12/2020)   * [[https://levelup.gitconnected.com/10-python-tips-for-better-code-1bbffde3b44d|10 Python Tips For Better Code]] (Abhay Parashar, Medium, 17/12/2020)
   * [[https://somacdivad.medium.com/3-tips-for-writing-pythonic-code-b090956a6107|3 Tips For Writing Pythonic Code]] David Amos, Medium, 17/03/2022   * [[https://somacdivad.medium.com/3-tips-for-writing-pythonic-code-b090956a6107|3 Tips For Writing Pythonic Code]] David Amos, Medium, 17/03/2022
 +  * [[https://realpython.com/learning-paths/writing-pythonic-code/|Write More Pythonic Code - Learning Path ⋅ Skills: Best Practices, Writing Idiomatic Python]] (Real Python)
  • teaching/progappchim/notions_fondamentales.1651474376.txt.gz
  • Dernière modification : 2022/05/02 08:52
  • de villersd