Chap_8 Cheat Sheet

A Programming › Python cheat sheet in English and français (French) by Theo666

Les modules sont des programmes Python qui contiennent des fonctions que l'on est amené à réutiliser souvent (on les appelle aussi bibliothèques ou libraries)

2,533 views 795 downloads 25 Sep 2023 History

Module

In Python, a module is a file containing Python defini­tions and statem­ents. It can define functions, classes, and variables, and can also include runnable code. Grouping related code into a module makes the code easier to understand and use, and it also makes the code logically organized.

Common Modules

math
fonctions et constantes mathém­atiques de base
sys
intera­ction avec l'in­ter­préteur Python, passage d'ar­guments
os
dialogue avec le système d'ex­plo­itation
random
génération de nombres aléatoires
time
accès à l'heure de l'or­din­ateur et aux fonctions gérant le temps.
urlib
récupé­ration de données sur internet depuis Python
Tkinter
interface python avec Tk. Création d'objets graphiques
re
gestion des expres­sions régulières
Numpy
manipu­lations de vecteurs et de matrices, algèbre linéaire
Biopython
recherche dans les banques de données biolog­iques, manipu­lation de séquences ou de structures de biomol­écules
matplotlib
eprése­nta­tions graphiques : courbes, nuages de points, diagrammes en bâtons...
pandas
analyse de données

Load & usage

# Method 1
import module 
import module as xx
module.function()

# Method 2
from module import function
from module import function1, function2
function()

# Method 3
from module import *
function()

# to unload a module
del module

# to get help  
help(module)
/ text # to find the key word
Q # exit
To avoid confusion between functions or constants with the same name from different modules, it is advisable to load the module by calling its name while using a function or a constant (Method 1) specially for the module math

module Random

random.ra­ndom()
a random float uniformly in the half-open range 0.0 <= X < 1.01
random.ra­ndi­nt(a,b)
It returns a random integer from the specified range [a, b], inclusive
random.ch­oic­e(seq)
returns a random element from the non-empty sequence seq
random.sh­uff­le(x)
randomly shuffles the elements of the list x in place
random.ra­ndr­ang­e(s­tart, stop, step)
returns a randomly selected element from the specified range
random.ch­oic­es(seq, weight = c() , k =)
It returns a list with the randomly selected element from the specified sequence
choice­(seq) & choices (seq, weight, k) The sequence can be a string, a range, a list, a tuple, or any other kind of sequence

common functions

- Le module sys pour récupérer les arguments passés à un script Python. python file.py xx xx dans un shell sys.argv dans le py fircher renvoie une liste des arguments y compris le nom du script lui-même sys.ar­gv[0]. accéder aux arguments avec sys.ar­gv[1], sys.ar­gv[2]

- Le module os gère l'in­terface avec le système d'ex­plo­ita­tion.
La fonction os.pat­h.e­xists() vérifie la présence d'un fichier sur le disque dur. Il renvoie True ou False
La fonction sys.ex­it(­"­mes­sag­e") est pour quitter le programme. Il est possible de renvoyer une message dans le shell
La fonction os.get­cwd() renvoie le répertoire (sous forme de chemin complet) depuis le directoire du travail
os.get­dir() same to ls in Linux and dir() in R

- time.s­leep()
- math.s­qrt()

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.