Compilateur LaTeX en ligne
www.cpierquet.fr
fichiers/testpythonsimple.tex
↓ télécharger
%!TEX pdflatex

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[margin=1.5cm]{geometry}
\usepackage[nominted]{ProfLycee}
\usepackage{scontents}
\tcbuselibrary{listingsutf8}
\lstdefinestyle{PLpython}{%
	language          = python,
	inputencoding     = utf8/latin1,
	basicstyle        = \ttfamily\footnotesize,
	keywordstyle      = \color{ForestGreen},
	keywordstyle      = [2]\color{magenta},
	commentstyle      = \color{gray}\itshape,
	stringstyle       = \color{red!75},
	numberstyle       = \scriptsize\ttfamily\color{Green},
	aboveskip        =  4pt,
	belowskip         = 1pt,
	tabsize           = 4,
	showstringspaces  = false,
	columns           = fullflexible,
	keepspaces        = true,
	breaklines        = true,
	breakatwhitespace = true
}

\tcbset{stylepythonlst/.style={%
		enhanced,boxrule=1.25pt,%
		leftupper=10pt,
		sharp corners=downhill,arc=12pt,
		before skip=12pt,after skip=12pt,%
		top=10pt,bottom=1mm,rightupper=4pt,%leftupper=25pt,
		attach boxed title to top right={yshift=-\tcboxedtitleheight},
		boxed title style={
			size=small,colback=ForestGreen!25,boxrule=1.25pt,
			colframe=Green,boxsep=1.25pt,
			sharp corners=downhill,
			arc=12pt,
			top=2pt,bottom=1pt,left=6pt,right=6pt
		},
		colframe=Green,colback=ForestGreen!5,%
		fonttitle=\color{ForestGreen}\itshape\ttfamily\footnotesize,
		title={\scriptsize\faPython}\:Code Python\vphantom{p},
		watermark text={\faPython},watermark opacity=0.175,watermark zoom=0.50,
		listing only,listing engine=listings,%
	}
}

\tcbset{stylepythonlstnolineos/.style={%
	stylepythonlst,
	listing options={style=PLpython,xleftmargin=0pt,numbers=none}
	}
}

\tcbset{stylepythonlstlineos/.style={%
	stylepythonlst,
	listing options={style=PLpython,xleftmargin=16pt,numbers=left}
	}
}

\DeclareTCBListing{envcodepython}{ s O{\linewidth} m }{% * off pour numéros de lignes + {largeur} + <options tcbox>
	\IfBooleanTF{#1}{stylepythonlstnolineos}{stylepythonlstlineos},
	width=#2,#3
}

\NewTCBInputListing{\envcodepythonfichier}{ s O{\linewidth} m m }{
	\IfBooleanTF{#1}{stylepythonlstnolineos}{stylepythonlstlineos},
	width=#2,#3,
	listing file={#4}}

\begin{document}

\part*{Test environnement \og simple \fg{} Python}

\begin{tcblisting}{colback=red!5!white,colframe=red!75!black,listing options={style=tcblatex,tabsize=4,basicstyle=\footnotesize\ttfamily}}
\begin{envcodepython}{}
#environnement par défaut
nb = int(input("Saisir un entier Python positif"))
if (nb %7 == 0) :
	print(f"{nb} est bien divisible par 7")
#endif

def f(x) :
	return x**2
\end{envcodepython}
\end{tcblisting}

\begin{tcblisting}{colback=red!5!white,colframe=red!75!black,listing options={style=tcblatex,tabsize=4,basicstyle=\small\ttfamily}}
\begin{envcodepython}*[0.5\linewidth]{flush right}
#largeur de 50%, sans numéro, et aligné à droite
nb = int(input("Saisir un entier Python positif"))
if (nb %7 == 0) :
	print(f"{nb} est bien divisible par 7")
#endif

def f(x) :
	return x**2
\end{envcodepython}
\end{tcblisting}

\begin{scontents}[overwrite,write-env=script1.py]
# Calcul de la factorielle en langage Python
def factorielle(x):
	if x < 2:
		return 1
	else:
		return x * factorielle(x-1)
\end{scontents}

\envcodepythonfichier*[12cm]{}{script1.py}

\end{document}