Initial commit

This commit is contained in:
Pascal Engélibert 2025-02-27 00:41:31 +01:00
commit 778fe31c38
35 changed files with 1849 additions and 0 deletions

7
tuto/makefile Executable file
View file

@ -0,0 +1,7 @@
all: tutorial.pdf
tutorial.pdf: tutorial.html
wkhtmltopdf -s A4 --footer-center "[page] / [topage]" --footer-font-name "Libertinus Sans" --footer-font-size 11 --header-left "[subsection]" --header-right "[title] par Pascal Engélibert, 2025, CC BY-SA 4.0" --header-font-name "Libertinus Sans" --header-font-size 11 --margin-top 20mm --footer-spacing 7 --header-spacing 7 --margin-bottom 20mm tutorial.html tutorial.pdf
tutorial.html: tutorial.md tutorial-head.html
pandoc tutorial.md -o tutorial.html --self-contained -M title="Tutoriel Python" --highlight-style pygments -H tutorial-head.html || exit 1

33
tuto/tutorial-head.html Normal file
View file

@ -0,0 +1,33 @@
<style type="text/css">
body {
font-family: "Libertinus Sans";
max-width: 80% !important;
font-size: 13pt !important;
}
h2 {
border-bottom: 2pt solid black;
}
h3 {
border-bottom: 1pt solid black;
}
h4 {
border-bottom: 1pt solid #ccc;
}
div.sourceCode {
margin-left: -5px;
border-left: 1px dashed #ccc;
padding-left: 4px;
}
div.sourceCode code {
font-size: 10pt !important;
}
code {
font-family: "Fira Code" !important;
}
p code {
padding-left: 4px;
}
header {
margin-top: 1cm;
}
</style>

427
tuto/tutorial.html Normal file
View file

@ -0,0 +1,427 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Tutoriel Python</title>
<style>
html {
line-height: 1.5;
font-family: Georgia, serif;
font-size: 20px;
color: #1a1a1a;
background-color: #fdfdfd;
}
body {
margin: 0 auto;
max-width: 36em;
padding-left: 50px;
padding-right: 50px;
padding-top: 50px;
padding-bottom: 50px;
hyphens: auto;
overflow-wrap: break-word;
text-rendering: optimizeLegibility;
font-kerning: normal;
}
@media (max-width: 600px) {
body {
font-size: 0.9em;
padding: 1em;
}
h1 {
font-size: 1.8em;
}
}
@media print {
body {
background-color: transparent;
color: black;
font-size: 12pt;
}
p, h2, h3 {
orphans: 3;
widows: 3;
}
h2, h3, h4 {
page-break-after: avoid;
}
}
p {
margin: 1em 0;
}
a {
color: #1a1a1a;
}
a:visited {
color: #1a1a1a;
}
img {
max-width: 100%;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 1.4em;
}
h5, h6 {
font-size: 1em;
font-style: italic;
}
h6 {
font-weight: normal;
}
ol, ul {
padding-left: 1.7em;
margin-top: 1em;
}
li > ol, li > ul {
margin-top: 0;
}
blockquote {
margin: 1em 0 1em 1.7em;
padding-left: 1em;
border-left: 2px solid #e6e6e6;
color: #606060;
}
code {
font-family: Menlo, Monaco, 'Lucida Console', Consolas, monospace;
font-size: 85%;
margin: 0;
}
pre {
margin: 1em 0;
overflow: auto;
}
pre code {
padding: 0;
overflow: visible;
overflow-wrap: normal;
}
.sourceCode {
background-color: transparent;
overflow: visible;
}
hr {
background-color: #1a1a1a;
border: none;
height: 1px;
margin: 1em 0;
}
table {
margin: 1em 0;
border-collapse: collapse;
width: 100%;
overflow-x: auto;
display: block;
font-variant-numeric: lining-nums tabular-nums;
}
table caption {
margin-bottom: 0.75em;
}
tbody {
margin-top: 0.5em;
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
}
th {
border-top: 1px solid #1a1a1a;
padding: 0.25em 0.5em 0.25em 0.5em;
}
td {
padding: 0.125em 0.5em 0.25em 0.5em;
}
header {
margin-bottom: 4em;
text-align: center;
}
#TOC li {
list-style: none;
}
#TOC ul {
padding-left: 1.3em;
}
#TOC > ul {
padding-left: 0;
}
#TOC a:not(:hover) {
text-decoration: none;
}
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<style type="text/css">
body {
font-family: "Libertinus Sans";
max-width: 80% !important;
font-size: 13pt !important;
}
h2 {
border-bottom: 2pt solid black;
}
h3 {
border-bottom: 1pt solid black;
}
h4 {
border-bottom: 1pt solid #ccc;
}
div.sourceCode {
margin-left: -5px;
border-left: 1px dashed #ccc;
padding-left: 4px;
}
div.sourceCode code {
font-size: 10pt !important;
}
code {
font-family: "Fira Code" !important;
}
p code {
padding-left: 4px;
}
header {
margin-top: 1cm;
}
</style>
</head>
<body>
<header id="title-block-header">
<h1 class="title">Tutoriel Python</h1>
</header>
<h1 id="tutoriel-python">Tutoriel Python</h1>
<h2 id="terminal">Terminal</h2>
<p>Ouvrir un terminal : CTRL+ALT+T ou le bouton noir en bas de
lécran.</p>
<p>Commandes :</p>
<ul>
<li><code>ls</code> : <strong>l</strong>i<strong>s</strong>ter les
fichiers et dossiers</li>
<li><code>cd mondossier</code> : <strong>c</strong>hanger de
<strong>d</strong>ossier</li>
<li><code>cd ..</code> : aller dans le dossier parent</li>
<li><code>mkdir mondossier</code> : créer le dossier</li>
<li><code>python3 monfichier.py</code> : lancer le fichier Python</li>
<li><code>mv ancien nouveau</code> : déplacer ou renommer un fichier ou
dossier</li>
<li><code>cp ancien nouveau</code> : copier un fichier</li>
</ul>
<p>Raccourcis clavier :</p>
<ul>
<li><code>CTRL+C</code> : arrêter la commande</li>
<li><code>Tab</code> : compléter automatiquement le nom de fichier</li>
<li>flèche haut/bas : historique des commandes</li>
</ul>
<h2 id="valeurs-variables-affichage">Valeurs, variables, affichage</h2>
<p>Afficher des valeurs avec <code>print</code> :</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="dv">42</span>)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="dv">10</span>, <span class="dv">20</span>, <span class="dv">30</span>)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;On peut écrire du texte.&quot;</span>)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Voici un nombre :&quot;</span>, <span class="dv">123</span>, <span class="st">&quot;.&quot;</span>)</span></code></pre></div>
<p>Utiliser des variables :</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>annee <span class="op">=</span> <span class="dv">2025</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Nous sommes en&quot;</span>, annee)</span></code></pre></div>
<p>Faire des calculs :</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>annee <span class="op">=</span> <span class="dv">2025</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>futur <span class="op">=</span> annee <span class="op">+</span> <span class="dv">10</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Dans 10 ans, nous serons en&quot;</span>, futur)</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>futur <span class="op">=</span> futur <span class="op">+</span> <span class="dv">5</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Dans 15 ans, nous serons en&quot;</span>, futur)</span></code></pre></div>
<p>Il y a dautres opérateurs :</p>
<ul>
<li><code>+</code> : addition</li>
<li><code>-</code> : soustraction</li>
<li><code>*</code> : multiplication</li>
<li><code>/</code> : division</li>
<li><code>%</code> : modulo (reste de la division)</li>
<li><code>**</code> : puissance</li>
</ul>
<h2 id="faire-des-choix">Faire des choix</h2>
<p>Si (if) une valeur est vraie, alors je fais quelque chose :</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="va">True</span>:</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;True signifie vrai&quot;</span>)</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;Donc ce code est toujours exécuté&quot;</span>)</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="va">False</span>:</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;False signifie faux&quot;</span>)</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;Donc ce code n&#39;est jamais exécuté&quot;</span>)</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a>prix <span class="op">=</span> <span class="dv">150</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> prix <span class="op">&gt;</span> <span class="dv">100</span>:</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;C&#39;est trop cher !&quot;</span>)</span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a> prix <span class="op">=</span> <span class="dv">100</span></span></code></pre></div>
<p>Il y a dautres comparateurs :</p>
<ul>
<li><code>==</code> : égal</li>
<li><code>!=</code> : différent</li>
<li><code>&lt;</code> : inférieur</li>
<li><code>&gt;</code> : supérieur</li>
<li><code>&lt;=</code> : inférieur ou égal</li>
<li><code>&gt;=</code> : supérieur ou égal</li>
</ul>
<p>Sinon (else), je fais autre chose :</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="dv">6</span><span class="op">*</span><span class="dv">7</span> <span class="op">==</span> <span class="dv">42</span>:</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;bien !&quot;</span>)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span>:</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;oups&quot;</span>)</span></code></pre></div>
<p>On peut aussi enchaîner plusieurs else if :</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> nombre <span class="op">&gt;</span> <span class="dv">0</span>:</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;le nombre est strictement positif&quot;</span>)</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="cf">elif</span> nombre <span class="op">&lt;</span> <span class="dv">0</span>:</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;le nombre est strictement négatif&quot;</span>)</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span>:</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;le nombre est nul&quot;</span>)</span></code></pre></div>
<h2 id="boucle-for">Boucle for</h2>
<p>On peut exécuter un code un nombre donné de fois :</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Je vais compter de 0 à 9 !&quot;</span>)</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">10</span>):</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(i)</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Je vais compter de 1 à 10 !&quot;</span>)</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">11</span>):</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(i)</span></code></pre></div>
<h2 id="listes">Listes</h2>
<p>Une liste est plusieurs valeurs entre crochets.</p>
<p>On peut ajouter un élément à la fin avec <code>append</code>, et
coller deux listes avec <code>+</code>.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>fruits <span class="op">=</span> []</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;C&#39;est vide :&quot;</span>, fruits)</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>fruits.append(<span class="st">&quot;pomme&quot;</span>)</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>fruits.append(<span class="st">&quot;kiwi&quot;</span>)</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>fruits <span class="op">=</span> fruits <span class="op">+</span> [<span class="st">&quot;abricot&quot;</span>, <span class="st">&quot;orange&quot;</span>]</span></code></pre></div>
<p>La boucle <code>for</code> exécute un code pour chaque élément de la
liste, dans lordre.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> fruit <span class="kw">in</span> fruits:</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;Ceci est un fruit :&quot;</span>, fruit)</span></code></pre></div>
<p>On peut lire un élément donné du tableau (la numérotation commence à
0) :</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="bu">file</span> <span class="op">=</span> [<span class="st">&quot;Toto&quot;</span>, <span class="st">&quot;Titi&quot;</span>, <span class="st">&quot;Tata&quot;</span>, <span class="st">&quot;Tutu&quot;</span>]</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;La deuxième personne dans la file est&quot;</span>, <span class="bu">file</span>[<span class="dv">1</span>])</span></code></pre></div>
<p>On peut changer la valeur dun élément de la liste :</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="bu">file</span>[<span class="dv">2</span>] <span class="op">=</span> <span class="st">&quot;Anne Onyme&quot;</span></span></code></pre></div>
<p>On peut aussi faire des listes de listes :</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>stock <span class="op">=</span> [</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> [<span class="st">&quot;patates&quot;</span>, <span class="dv">52</span>],</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> [<span class="st">&quot;poireaux&quot;</span>, <span class="dv">20</span>],</span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> [<span class="st">&quot;carottes&quot;</span>, <span class="dv">40</span>]</span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a>]</span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> element <span class="kw">in</span> stock:</span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;Nombre de&quot;</span>, element[<span class="dv">0</span>], <span class="st">&quot;:&quot;</span>, element[<span class="dv">1</span>])</span></code></pre></div>
<p>Pour avoir à la fois lindice (le numéro) de lélément, et lélément
:</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>planetes <span class="op">=</span> [<span class="st">&quot;Mercure&quot;</span>, <span class="st">&quot;Vénus&quot;</span>, <span class="st">&quot;Terre&quot;</span>, <span class="st">&quot;Mars&quot;</span>]</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="bu">len</span>(planetes)):</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">&quot;La planète numéro&quot;</span>, i<span class="op">+</span><span class="dv">1</span>, <span class="st">&quot;se nomme&quot;</span>, planetes[i])</span></code></pre></div>
<p>On peut retirer un élément avec <code>pop</code>.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>planetes <span class="op">=</span> [<span class="st">&quot;Lune&quot;</span>, <span class="st">&quot;Jupiter&quot;</span>, <span class="st">&quot;Saturne&quot;</span>]</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Oups, la Lune n&#39;est pas une planète.</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Je retire&quot;</span>, planetes.pop(<span class="dv">0</span>), <span class="st">&quot;de la liste.&quot;</span>)</span></code></pre></div>
<p>On peut agrandir une liste en répétant un élément un nombre donné de
fois :</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>zeros <span class="op">=</span> [<span class="dv">0</span>] <span class="op">*</span> <span class="dv">1000</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">&quot;Mille zéros !&quot;</span>, zeros)</span></code></pre></div>
<h2 id="fonctions-mathématiques">Fonctions mathématiques</h2>
<ul>
<li><code>max(a, b)</code> : donne le plus grand nombre entre a et
b</li>
<li><code>min(a, b)</code> : donne le plus petit nombre entre a et
b</li>
<li><code>max(liste)</code> : donne la valeur maximum de la liste</li>
<li><code>min(liste)</code> : donne la valeur minimum de la liste</li>
<li><code>sum(liste)</code> : donne la somme de tous les éléments de la
liste</li>
<li><code>int(nombre)</code> : partie entière (enlève les chiffres après
la virgule)</li>
<li><code>round(nombre)</code> : arrondit au plus proche</li>
<li><code>abs(nombre)</code> : valeur absolue (distance à zéro)</li>
</ul>
<p>Pour avoir des fonctions plus avancées, il faut importer le module
<code>math</code> au début du fichier :</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> math</span></code></pre></div>
<ul>
<li><code>math.pi</code> : 3.1415926…</li>
<li><code>math.sin(x)</code> : sinus</li>
<li><code>math.cos(x)</code> : cosinus</li>
<li><code>math.floor(x)</code> : arrondit à linférieur</li>
<li><code>math.ceil(x)</code> : arrondit au supérieur</li>
</ul>
<h2 id="aléatoire">Aléatoire</h2>
<p>Il faut importer le module <code>random</code> au début du fichier
:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span></code></pre></div>
<ul>
<li><code>random.random()</code> : nombre (à virgule) aléatoire entre 0
et 1</li>
<li><code>random.randint(100, 200)</code> : nombre (entier) aléatoire
entre 100 (inclus) et 200 (inclus)</li>
<li><code>random.choice(liste)</code> : choisit un élément au hasard
dans la liste</li>
</ul>
<h2 id="fonctions-anonymes">Fonctions anonymes</h2>
</body>
</html>

232
tuto/tutorial.md Normal file
View file

@ -0,0 +1,232 @@
# Tutoriel Python
## Terminal
Ouvrir un terminal : CTRL+ALT+T ou le bouton noir en bas de l'écran.
Commandes :
* `ls` : **l**i**s**ter les fichiers et dossiers
* `cd mondossier` : **c**hanger de **d**ossier
* `cd ..` : aller dans le dossier parent
* `mkdir mondossier` : créer le dossier
* `python3 monfichier.py` : lancer le fichier Python
* `mv ancien nouveau` : déplacer ou renommer un fichier ou dossier
* `cp ancien nouveau` : copier un fichier
Raccourcis clavier :
* `CTRL+C` : arrêter la commande
* `Tab` : compléter automatiquement le nom de fichier
* flèche haut/bas : historique des commandes
## Valeurs, variables, affichage
Afficher des valeurs avec `print` :
```python
print(42)
print(10, 20, 30)
print("On peut écrire du texte.")
print("Voici un nombre :", 123, ".")
```
Utiliser des variables :
```python
annee = 2025
print("Nous sommes en", annee)
```
Faire des calculs :
```python
annee = 2025
futur = annee + 10
print("Dans 10 ans, nous serons en", futur)
futur = futur + 5
print("Dans 15 ans, nous serons en", futur)
```
Il y a d'autres opérateurs :
* `+` : addition
* `-` : soustraction
* `*` : multiplication
* `/` : division
* `%` : modulo (reste de la division)
* `**` : puissance
## Faire des choix
Si (if) une valeur est vraie, alors je fais quelque chose :
```python
if True:
print("True signifie vrai")
print("Donc ce code est toujours exécuté")
if False:
print("False signifie faux")
print("Donc ce code n'est jamais exécuté")
prix = 150
if prix > 100:
print("C'est trop cher !")
prix = 100
```
Il y a d'autres comparateurs :
* `==` : égal
* `!=` : différent
* `<` : inférieur
* `>` : supérieur
* `<=` : inférieur ou égal
* `>=` : supérieur ou égal
Sinon (else), je fais autre chose :
```python
if 6*7 == 42:
print("bien !")
else:
print("oups")
```
On peut aussi enchaîner plusieurs else if :
```python
if nombre > 0:
print("le nombre est strictement positif")
elif nombre < 0:
print("le nombre est strictement négatif")
else:
print("le nombre est nul")
```
## Boucle for
On peut exécuter un code un nombre donné de fois :
```python
print("Je vais compter de 0 à 9 !")
for i in range(10):
print(i)
print("Je vais compter de 1 à 10 !")
for i in range(1, 11):
print(i)
```
## Listes
Une liste est plusieurs valeurs entre crochets.
On peut ajouter un élément à la fin avec `append`, et coller deux listes avec `+`.
```python
fruits = []
print("C'est vide :", fruits)
fruits.append("pomme")
fruits.append("kiwi")
fruits = fruits + ["abricot", "orange"]
```
La boucle `for` exécute un code pour chaque élément de la liste, dans l'ordre.
```python
for fruit in fruits:
print("Ceci est un fruit :", fruit)
```
On peut lire un élément donné du tableau (la numérotation commence à 0) :
```python
file = ["Toto", "Titi", "Tata", "Tutu"]
print("La deuxième personne dans la file est", file[1])
```
On peut changer la valeur d'un élément de la liste :
```python
file[2] = "Anne Onyme"
```
On peut aussi faire des listes de listes :
```python
stock = [
["patates", 52],
["poireaux", 20],
["carottes", 40]
]
for element in stock:
print("Nombre de", element[0], ":", element[1])
```
Pour avoir à la fois l'indice (le numéro) de l'élément, et l'élément :
```python
planetes = ["Mercure", "Vénus", "Terre", "Mars"]
for i in range(len(planetes)):
print("La planète numéro", i+1, "se nomme", planetes[i])
```
On peut retirer un élément avec `pop`.
```python
planetes = ["Lune", "Jupiter", "Saturne"]
# Oups, la Lune n'est pas une planète.
print("Je retire", planetes.pop(0), "de la liste.")
```
On peut agrandir une liste en répétant un élément un nombre donné de fois :
```python
zeros = [0] * 1000
print("Mille zéros !", zeros)
```
## Fonctions mathématiques
* `max(a, b)` : donne le plus grand nombre entre a et b
* `min(a, b)` : donne le plus petit nombre entre a et b
* `max(liste)` : donne la valeur maximum de la liste
* `min(liste)` : donne la valeur minimum de la liste
* `sum(liste)` : donne la somme de tous les éléments de la liste
* `int(nombre)` : partie entière (enlève les chiffres après la virgule)
* `round(nombre)` : arrondit au plus proche
* `abs(nombre)` : valeur absolue (distance à zéro)
Pour avoir des fonctions plus avancées, il faut importer le module `math` au début du fichier :
```python
import math
```
* `math.pi` : 3.1415926...
* `math.sin(x)` : sinus
* `math.cos(x)` : cosinus
* `math.floor(x)` : arrondit à l'inférieur
* `math.ceil(x)` : arrondit au supérieur
## Aléatoire
Il faut importer le module `random` au début du fichier :
```python
import random
```
* `random.random()` : nombre (à virgule) aléatoire entre 0 et 1
* `random.randint(100, 200)` : nombre (entier) aléatoire entre 100 (inclus) et 200 (inclus)
* `random.choice(liste)` : choisit un élément au hasard dans la liste
## Fonctions anonymes

BIN
tuto/tutorial.pdf Normal file

Binary file not shown.