Différences
Ci-dessous, les différences entre deux révisions de la page.
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/11 12:01] – [Le type de données texte ou "string" :] villersd | teaching:progappchim:notions_fondamentales [2021/02/02 23:25] – [Structures répétitives] villersd | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Notions fondamentales ====== | ====== Notions fondamentales ====== | ||
- | Aide mémoire synthétique sur le langage Python. | + | Aide mémoire synthétique sur le langage Python. |
===== Règles de base ===== | ===== Règles de base ===== | ||
Ligne 91: | Ligne 91: | ||
//Cf.// [[http:// | //Cf.// [[http:// | ||
+ | |||
+ | FIXME : ajouter d' | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
---- | ---- | ||
Ligne 124: | Ligne 128: | ||
* Conversion en nombre (donnée numérique créée à partir d'une chaîne de caractères) : int(" | * Conversion en nombre (donnée numérique créée à partir d'une chaîne de caractères) : int(" | ||
* Pour la concaténation d'une liste de chaîne, la fonction join est plus adaptée que " | * Pour la concaténation d'une liste de chaîne, la fonction join est plus adaptée que " | ||
- | < | + | < |
strings = [' | strings = [' | ||
text = '' | text = '' | ||
Ligne 145: | Ligne 149: | ||
string.printable # = digits + ascii_letters + punctuation + whitespace | string.printable # = digits + ascii_letters + punctuation + whitespace | ||
</ | </ | ||
+ | |||
+ | === Modifications, | ||
+ | <code python> | ||
+ | s.capitalize() # captilizes (first character becomes uppercase) the string | ||
+ | s.lower() # all characters become lowercase | ||
+ | s.casefold() # more rigorous lowercase (languages other than English are covered) | ||
+ | s.upper() # all characters become uppercase | ||
+ | |||
+ | s.count(sub) # count occurences of substring sub in s | ||
+ | s.count(sub, | ||
+ | s.count(sub, | ||
+ | s.find(sub) # returns index of first occurence of substring sub in s, return -1 if not found | ||
+ | s.find(sub, start) # returns index of first occurence of substring sub starting from start position in string s, returns -1 if not found | ||
+ | s.find(sub, start, end) # returns index of first occurence of substring sub from start to end — 1 position in string s, return -1 if not found | ||
+ | s.index(sub) # returns index of first occurence of substring sub in s, raises error if not found | ||
+ | s.index(sub, | ||
+ | s.index(sub, | ||
+ | len(str) # returns length of string | ||
+ | |||
+ | s.startswith(prefix) # checks if s starts with prefix | ||
+ | s.startswith(prefix, | ||
+ | s.startswith(prefix, | ||
+ | s.endswith(suffix) # checks if s ends with suffix | ||
+ | s.endswith(suffix, | ||
+ | s.endswith(suffix, | ||
+ | s.isalnum() # checks if string is alphanumeric | ||
+ | s.isalpha() # checks if string contains only alphabets | ||
+ | s.isnumeric() # checks if string contains only numbers | ||
+ | s.islower() # checks if all alphabets in string s are lowercase | ||
+ | s.isupper() # checks if all alphabets in string s are uppercase | ||
+ | s.isspace() # checks if s is a space character | ||
+ | |||
+ | s.replace(old, | ||
+ | s.replace(old, | ||
+ | s.ljust(width) # puts width — len(s) spaces on the right side of string s | ||
+ | s.ljust(width, | ||
+ | s.rjust(width) # puts width — len(s) spaces on the left side of string s | ||
+ | s.rjust(width, | ||
+ | s.strip() # all spaces gone left and right both sides of string s | ||
+ | s.lstrip() # all spaces gone on left side of string s | ||
+ | s.rstrip() # all spaces gone on right side of string s | ||
+ | s.strip(k) # all substrings k gone left and right both sides of string s | ||
+ | s.lstrip(k) # all substrings k gone on left side of string s | ||
+ | s.rstrip(k) # all substrings k gone on right side of string s | ||
+ | s.split(‘, | ||
+ | s.split(‘:: | ||
+ | s.split(‘ ‘) # splits the string by ‘ ‘; returns a list | ||
+ | s.zfill(width) # adds width — len(s) zeros on the left side; if a +/- sign is there then zeros are added after it | ||
+ | |||
+ | s.join(l) # joins a list or string l with substring s | ||
+ | </ | ||
+ | |||
+ | === Applications à la détection de palindromes et anagrammes === | ||
+ | |||
+ | |||
+ | <code python string-palindrome-01.py> | ||
+ | # | ||
+ | # -*- 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, | ||
+ | qu'on le lise de gauche à droite ou de droite à gauche. | ||
+ | Source : https:// | ||
+ | """ | ||
+ | import unicodedata | ||
+ | |||
+ | def palindrome(string_to_check): | ||
+ | if string_to_check.lower().replace(' | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | def remove_accents(input_str): | ||
+ | """ | ||
+ | Les lettres accentuées viennent compliquer le problème... | ||
+ | cf. https:// | ||
+ | """ | ||
+ | nfkd_form = unicodedata.normalize(' | ||
+ | return u"" | ||
+ | |||
+ | |||
+ | string0 = 'Esope reste ici et se repose' | ||
+ | print(string0, | ||
+ | |||
+ | string1 = ' | ||
+ | print(string1, | ||
+ | |||
+ | print(string1, | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code python string-anagramme-00.py> | ||
+ | # | ||
+ | # -*- 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:// | ||
+ | """ | ||
+ | string1 = ' | ||
+ | string2 = ' | ||
+ | |||
+ | print(string1, | ||
+ | print(string2, | ||
+ | |||
+ | # exercice : utiliser cette fonction sorted() et appliquer les transformations | ||
+ | # de la fonction de vérification de palindromes pour détecter une anagramme. | ||
+ | |||
+ | </ | ||
+ | |||
=== Références === | === Références === | ||
Ligne 281: | Ligne 403: | ||
- | Consulter la [[https:// | + | * Consulter la [[https:// |
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
Des types non intégrés par défaut dans Python peuvent facilement être implémentés, | Des types non intégrés par défaut dans Python peuvent facilement être implémentés, | ||
Ligne 584: | Ligne 711: | ||
* Pour en savoir plus, consultez la page [[https:// | * Pour en savoir plus, consultez la page [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
+ | |||