\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{thibautpe} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (thibautpe) /Subject (Python Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{thibautpe} via \textcolor{DarkBackground}{\uline{cheatography.com/194226/cs/40485/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}thibautpe \\ \uline{cheatography.com/thibautpe} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 22nd November, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Ligne de commande}} \tn % Row 0 \SetRowColor{LightBackground} Lancer python & python \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Sortir de python & CTRL+Z+ENTER \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Connaitre la version & python -{}-v \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} Lancer un programme python & python \textless{}nom\_du\_programme.py\textgreater{} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.60195 cm} x{1.60195 cm} x{1.3731 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{PIP (Package Manager)}} \tn % Row 0 \SetRowColor{LightBackground} Connaitre la version de PIP & pip -{}-version & \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Installation d'un paquet (dernière version) & pip install \textless{}pakage\textgreater{} & \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{Désinstallation} d'un paquet & pip uninstall \textless{}package\textgreater{} & \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} Lister les packages & pip list & \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} Lister les paquets (format \seqsplit{requirements.txt)} & pip freeze & \tn % Row Count 14 (+ 4) % Row 5 \SetRowColor{white} Informations sur un paquet {\emph{(notamment les dépendances)}} & pip show \textless{}package(s)\textgreater{} & pip show requests numpy pandas \tn % Row Count 19 (+ 5) % Row 6 \SetRowColor{LightBackground} Installation d'une version d'un paquet & pip install \textless{}pakage\textgreater{}==major.minor.patch & pip install \seqsplit{requests==2.1.0} \tn % Row Count 22 (+ 3) % Row 7 \SetRowColor{white} Installation d'une version d'un paquet (au sein d'une major) & pip install \textless{}pakage\textgreater{}\textasciitilde{}=major.minor & pip install requests\textasciitilde{}=2.2 \tn % Row Count 27 (+ 5) % Row 8 \SetRowColor{LightBackground} Installation d'une version d'un paquet (au sein d'une minor) & pip install \textless{}pakage\textgreater{}\textasciitilde{}=major.minor.patch & pip install requests\textasciitilde{}=2.1.0 \tn % Row Count 32 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.60195 cm} x{1.60195 cm} x{1.3731 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{PIP (Package Manager) (cont)}} \tn % Row 9 \SetRowColor{LightBackground} nstallation de la dernière version d'un paquet au delà d'une version & pip install \textless{}pakage\textgreater{}\textgreater{}major.minor.patch & pip install requests\textgreater{}2.5.0 \tn % Row Count 5 (+ 5) % Row 10 \SetRowColor{white} Installation de la dernière version d'un paquet entre 2 versions & pip install "\textless{}pakage\textgreater{}\textgreater{}major.minor.patch,\textless{}major.minor.patch" & pip install "requests\textgreater{}2.4.0,\textless{}2.6.0" \tn % Row Count 10 (+ 5) % Row 11 \SetRowColor{LightBackground} Suppression de tous les paquets & pip freeze \textgreater{} \seqsplit{requirements.txt} \{\{nl\}\}pip uninstall -r \seqsplit{requirements.txt} -y & \tn % Row Count 16 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.96811 cm} x{1.55618 cm} x{1.05271 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Import des paquets}} \tn % Row 0 \SetRowColor{LightBackground} Import d'un paquet & import \textless{}package\textgreater{} & import numpy \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Import d'un paquet (déconseillé pour les conflits) & from \textless{}package\textgreater{} import * & from numpy import * \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} Import d'un paquet avec alias & import \textless{}package\textgreater{} as \textless{}alias\textgreater{} & import numpy as np \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} Import d'un module & import \textless{}module\textgreater{} & import os \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} Import d'une fonction d'un module & from \textless{}module\textgreater{} import \textless{}fonction\textgreater{} & from os import getcwd \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} Lister les modules & \seqsplit{help('modules')} & \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} Aide d'un module & python -m \textless{}module\textgreater{} -{}-help & python -m venv -{}-help \tn % Row Count 19 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{Un module est du code exécutable et les définitions de classe/fonction contenus dans un fichier Python unique. \newline Un paquet (ou package) est une collection de modules regroupés logiquement dans un répertoire et partageant un fichier de configuration (\_\_init\_\_.py)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.53827 cm} x{2.43873 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Environnement virtuel}} \tn % Row 0 \SetRowColor{LightBackground} Aide du module venv & python -m venv -{}-help \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Création d'un environnement virtuel & python -m venv \textless{}environment name\textgreater{} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Activation de l'environnement virtuel (shell Windows) & \textless{}environment name\textgreater{}\textbackslash{}Scripts\textbackslash{}activate.bat \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} Sortir de l'environnement virtuel & deactivate \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{Le module venv est disponible à partir de la version 3.3}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Programmation orientée objet}} \tn % Row 0 \SetRowColor{LightBackground} Nom de Classe / Convention & Nom de classe avec majuscule. Si plusieurs \seqsplit{mots:CapitalizedCase} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Variable / Convention & Nom de variable en minuscule. Indentation supplémentaire de 4 espaces. \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} Méthode/ Convention & Nom de méthode en minuscule séparés par des traits de soulignement. Indentation supplémentaire de 4 espaces. \tn % Row Count 14 (+ 6) % Row 3 \SetRowColor{white} Constructeur / Convention & Nom en minuscule, commencent et se terminent par deux underscores (ou « dunders »). \tn % Row Count 19 (+ 5) % Row 4 \SetRowColor{LightBackground} Classe Python & class Rectangle:\{\{nl\}\} width = 3\{\{nl\}\} height = 2\{\{nl\}\} def calculate\_area(self):\{\{nl\}\} return self.width * self.height\{\{nl\}\} \tn % Row Count 27 (+ 8) % Row 5 \SetRowColor{white} Constructeur (\_\_init\_\_): & class Carre:\{\{nl\}\} length=0\{\{nl\}\} def \_\_init\_\_(self,length):\{\{nl\}\} self.length=length \tn % Row Count 33 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Programmation orientée objet (cont)}} \tn % Row 6 \SetRowColor{LightBackground} Constructeur (\_\_init\_\_): avec initialisation de variable & class Carre:\{\{nl\}\} length=0\{\{nl\}\} def \_\_init\_\_(self,length=5):\{\{nl\}\} self.length=length \tn % Row Count 6 (+ 6) % Row 7 \SetRowColor{white} Constructeur (\_\_init\_\_): avec paramètre optionnel & class Carre:\{\{nl\}\} length=0\{\{nl\}\} def \_\_init\_\_(self,length=5,color="red"):\{\{nl\}\} self.length=length\{\{nl\}\} self.color=color\{\{nl\}\} \tn % Row Count 14 (+ 8) % Row 8 \SetRowColor{LightBackground} Instanciation d'un objet (sans paramètre optionnel) & car=Carre(2) \{\{nl\}\} ou car=Carre(length=2) \tn % Row Count 17 (+ 3) % Row 9 \SetRowColor{white} Instanciation d'un objet (avec paramètre optionnel) & car2=Carre(1,"blue") \{\{nl\}\} car3=Carre(3,color="green") \tn % Row Count 20 (+ 3) % Row 10 \SetRowColor{LightBackground} Accéder à un attribut de l'objet (ici pour l'imprimer) & print(car2.color) \tn % Row Count 23 (+ 3) % Row 11 \SetRowColor{white} Modifier un attribut d'objet (l'utilisation d'une méthode , ici change\_length() est conseillée) & car2.length=5 \tn % Row Count 28 (+ 5) % Row 12 \SetRowColor{LightBackground} Attribut d'instance & class Bird::\{\{nl\}\} def \_\_init\_\_(self)::\{\{nl\}\} self.wings = 2:\{\{nl\}\} \tn % Row Count 32 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Programmation orientée objet (cont)}} \tn % Row 13 \SetRowColor{LightBackground} Attribut de classe & class Bird: \{\{nl\}\} names = ("mouette", "pigeon", "moineau", "hirrondelle")\{\{nl\}\} positions = \{\}\{\{nl\}\} def \_\_init\_\_(self, name):\{\{nl\}\} self.position = 1, 2 \tn % Row Count 9 (+ 9) % Row 14 \SetRowColor{white} Méthode de classe & @classmethod\{\{nl\}\} def find\_bird(cls, position):\{\{nl\}\} if position in cls.positions:\{\{nl\}\} return f"On a trouvé un \{cls.positions{[}position{]}\} !" \{\{nl\}\} return "On a rien trouvé..." \tn % Row Count 20 (+ 11) % Row 15 \SetRowColor{LightBackground} Accéder à une méthode d'un objet & area = \seqsplit{car2.calculate\_area()} \tn % Row Count 22 (+ 2) % Row 16 \SetRowColor{white} Méthode Statique & class Bird: \{\{nl\}\} @staticmethod\{\{nl\}\} def get\_definition():\{nl\}\} \tn % Row Count 26 (+ 4) % Row 17 \SetRowColor{LightBackground} Méthode vide (implémentation à faire ultérieuremen) & def empty\_method(self, length): \{\{nl\}\} pass \tn % Row Count 29 (+ 3) % Row 18 \SetRowColor{white} Héritage & class Film: \{\{nl\}\} {[}...{]}\{\{nl\}\} class FilmCassette(Film): \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Programmation orientée objet (cont)}} \tn % Row 19 \SetRowColor{LightBackground} Appeler une méthode de la classe parente & super()\{\{nl\}\} Exemple: \seqsplit{super().\_\_init\_\_(name)} \tn % Row Count 3 (+ 3) % Row 20 \SetRowColor{white} Exception. Levée & message = (f"This Function only supports halving even numbers. Received: \{number\}" ) \{\{nl\}\} raise Exception(message) \tn % Row Count 10 (+ 7) % Row 21 \SetRowColor{LightBackground} Exception. Gestion & try: \{\{nl\}\} return (after\_value / initial\_value) * 100 \{\{nl\}\} except ZeroDivisionError: \{\{nl\}\} return 0\{\{nl\}\} except Exception as error: \{\{nl\}\} print("Uh oh, unexpected error occurred!") \{\{nl\}\} raise error \tn % Row Count 21 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Liens (copy)}} \tn % Row 0 \SetRowColor{LightBackground} Index des paquets python & https://pypi.org/ \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Python en ligne & \seqsplit{https://colab.research.google.com/?utm\_source=scs-index} \tn % Row Count 5 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Tests unitaires}} \tn % Row 0 \SetRowColor{LightBackground} Exemple de méthode à tester & def \seqsplit{to\_absolute(number):} \{\{nl\}\} """ \{\{nl\}\} Return the absolute value\{\{nl\}\} :param number: Initial number \{\{nl\}\} :return: The absolute value \{\{nl\}\} """ \{\{nl\}\} if number \textless{}= 0: \{\{nl\}\} return -number \{\{nl\}\} return number \tn % Row Count 13 (+ 13) % Row 1 \SetRowColor{white} Ecriture d'un doctest & def \seqsplit{to\_absolute(number):} \{\{nl\}\} """ \{\{nl\}\} \textgreater{}\textgreater{}\textgreater{} to\_absolute(3) \{\{nl\}\} 3 \{\{nl\}\} """ \{\{nl\}\} if number \textless{}= 0: \{\{nl\}\} return -number \{\{nl\}\} return number \tn % Row Count 22 (+ 9) % Row 2 \SetRowColor{LightBackground} Lancement d'un doctest & python -m doctest \textless{}nom du fichier\textgreater{} \tn % Row Count 24 (+ 2) % Row 3 \SetRowColor{white} Lancement d'un doctest (avec traces) & python -m doctest -v \textless{}nom du module\textgreater{} \tn % Row Count 26 (+ 2) % Row 4 \SetRowColor{LightBackground} Librairie Pytest / Installation & pip install -U pytest \tn % Row Count 28 (+ 2) % Row 5 \SetRowColor{white} Librairie Pytest / Implémentation d'un test dans une classe test.py & from source import reverse\_str \{\{nl\}\}def \seqsplit{test\_should\_reverse\_string():} \{\{nl\}\}assert reverse\_str('abc') == 'cba' \tn % Row Count 34 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Tests unitaires (cont)}} \tn % Row 6 \SetRowColor{LightBackground} Librairie Pytest / Lancement de la classe de test & pytest test.py \tn % Row Count 3 (+ 3) % Row 7 \SetRowColor{white} Librairie Pytest / Lancement de la classe de test en mode verbeux & pytest -v test.py \tn % Row Count 7 (+ 4) % Row 8 \SetRowColor{LightBackground} Librairie Pytest / Lancement d'une méthode de la classe de test & pytest -v \seqsplit{test.py::test\_should\_reverse\_string} \tn % Row Count 11 (+ 4) % Row 9 \SetRowColor{white} Librairie Pytest / Lancement des classes de test commençant ou terminant par test & pytest \tn % Row Count 16 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}