325 lines
19 KiB
HTML
325 lines
19 KiB
HTML
|
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
<title>Structures de contrôle et fonctions — Documentation Formation Python 1</title>
|
||
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
|
||
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
||
<script type="text/javascript">
|
||
var DOCUMENTATION_OPTIONS = {
|
||
URL_ROOT: './',
|
||
VERSION: '1',
|
||
COLLAPSE_INDEX: false,
|
||
FILE_SUFFIX: '.html',
|
||
HAS_SOURCE: true,
|
||
SOURCELINK_SUFFIX: '.txt'
|
||
};
|
||
</script>
|
||
<script type="text/javascript" src="_static/jquery.js"></script>
|
||
<script type="text/javascript" src="_static/underscore.js"></script>
|
||
<script type="text/javascript" src="_static/doctools.js"></script>
|
||
<script type="text/javascript" src="_static/translations.js"></script>
|
||
<link rel="index" title="Index" href="genindex.html" />
|
||
<link rel="search" title="Recherche" href="search.html" />
|
||
<link rel="next" title="Tests unitaires et pile d’appels" href="testsunitaires.html" />
|
||
<link rel="prev" title="Typage, types de base" href="type.html" />
|
||
</head>
|
||
<body>
|
||
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
|
||
<table><tr><td>
|
||
<img src="_static/sphinx.png" alt="logo" />
|
||
</td><td>
|
||
<h1> Programmation python</h1>
|
||
</td></tr></table>
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="genindex.html" title="Index général"
|
||
accesskey="I">index</a></li>
|
||
<li class="right" >
|
||
<a href="py-modindex.html" title="Index des modules Python"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="testsunitaires.html" title="Tests unitaires et pile d’appels"
|
||
accesskey="N">suivant</a> |</li>
|
||
<li class="right" >
|
||
<a href="type.html" title="Typage, types de base"
|
||
accesskey="P">précédent</a> |</li>
|
||
<a href="index.html">Programmation python </a> »
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<p> </p>
|
||
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<div class="section" id="structures-de-controle-et-fonctions">
|
||
<h1>Structures de contrôle et fonctions<a class="headerlink" href="#structures-de-controle-et-fonctions" title="Lien permanent vers ce titre">¶</a></h1>
|
||
<ul class="simple">
|
||
<li>tests</li>
|
||
</ul>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">if</span> <span class="s2">"a"</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">"a"</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">a</span>
|
||
<span class="gp">>>> </span><span class="k">if</span> <span class="s2">""</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">"hell"</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="mi">2</span>
|
||
<span class="gp">>>> </span><span class="k">if</span> <span class="n">a</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">"un"</span>
|
||
<span class="gp">... </span><span class="k">else</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">"deux"</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">deux</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="first admonition-title">Important</p>
|
||
<p class="last">les types de base ont tous une valeur booléenne</p>
|
||
</div>
|
||
<ul class="simple">
|
||
<li>itérations</li>
|
||
</ul>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="n">i</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">0</span>
|
||
<span class="go">1</span>
|
||
<span class="go">2</span>
|
||
<span class="go">3</span>
|
||
<span class="go">4</span>
|
||
<span class="go">5</span>
|
||
<span class="go">6</span>
|
||
<span class="go">7</span>
|
||
<span class="go">8</span>
|
||
<span class="go">9</span>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </span><span class="n">l</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">l</span>
|
||
<span class="go">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span>
|
||
</pre></div>
|
||
</div>
|
||
<ul class="simple">
|
||
<li>tant que</li>
|
||
</ul>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">i</span> <span class="o">=</span> <span class="mi">10</span>
|
||
<span class="gp">>>> </span><span class="k">while</span> <span class="n">i</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="n">i</span>
|
||
<span class="gp">... </span> <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span><span class="mi">1</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">10</span>
|
||
<span class="go">9</span>
|
||
<span class="go">8</span>
|
||
<span class="go">7</span>
|
||
<span class="go">6</span>
|
||
<span class="go">5</span>
|
||
<span class="go">4</span>
|
||
<span class="go">3</span>
|
||
<span class="go">2</span>
|
||
<span class="go">1</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="section" id="fonctions">
|
||
<h2>fonctions<a class="headerlink" href="#fonctions" title="Lien permanent vers ce titre">¶</a></h2>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">blabla</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="sd">"""fonction qui printe"""</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="n">x</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>il n’y a que des fonctions (et pas de procédures):</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">ma_func</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="s2">"écrit quelque chose dans un fichier"</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">ma_func</span><span class="p">(</span><span class="s2">"a"</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span> <span class="n">ma_func</span><span class="p">(</span><span class="s2">"a"</span><span class="p">)</span>
|
||
<span class="go">None</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>est équivalent à :</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">ma_func</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="s2">"écrit quelque chose dans un fichier"</span>
|
||
<span class="gp">... </span> <span class="k">return</span> <span class="kc">None</span>
|
||
</pre></div>
|
||
</div>
|
||
<ul>
|
||
<li><p class="first">arité d’une fonction:</p>
|
||
<blockquote>
|
||
<div><ul class="simple">
|
||
<li>paramètre non nommé</li>
|
||
<li>paramètre nommé</li>
|
||
</ul>
|
||
</div></blockquote>
|
||
</li>
|
||
</ul>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">ma_fonction</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">"arguments : "</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">"argumments nommés"</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">ma_fonction</span><span class="p">(</span><span class="s2">"toto"</span><span class="p">,</span> <span class="s2">"titi"</span><span class="p">,</span> <span class="n">tutu</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">tata</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
|
||
<span class="go">arguments : ('toto', 'titi')</span>
|
||
<span class="go">argumments nommés {'tata': 3, 'tutu': 2}</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<ul class="simple">
|
||
<li>signature d’une fonction : ça peut renvoyer n’importe quoi (tout ce qu’on veut)</li>
|
||
</ul>
|
||
<p><a class="reference internal" href="#term-return"><span class="xref std std-term">return</span></a> ou <a class="reference internal" href="#term-yield"><span class="xref std std-term">yield</span></a> ?</p>
|
||
<dl class="glossary docutils">
|
||
<dt id="term-yield">yield</dt>
|
||
<dd>permet de renvoyer le résultat d’une fonction en plusieurs étape
|
||
à l’aide d’un générateur</dd>
|
||
<dt id="term-return">return</dt>
|
||
<dd>résultat d’une fonction</dd>
|
||
</dl>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">ma_fonction</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="k">yield</span> <span class="n">i</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ma_fonction</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="n">i</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">0</span>
|
||
<span class="go">1</span>
|
||
<span class="go">2</span>
|
||
<span class="go">3</span>
|
||
<span class="go">4</span>
|
||
<span class="go">5</span>
|
||
<span class="go">6</span>
|
||
<span class="go">7</span>
|
||
<span class="go">8</span>
|
||
<span class="go">9</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<ul class="simple">
|
||
<li>espaces de nommage à l’intérieur d’une fonction :</li>
|
||
</ul>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">toto</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="nb">vars</span><span class="p">()</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">toto</span><span class="p">(</span><span class="s2">"sdfsdf"</span><span class="p">)</span>
|
||
<span class="go">{'x': 'sdfsdf'}</span>
|
||
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span><span class="k">pass</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s2">"titi"</span>
|
||
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="s2">"toto"</span>
|
||
<span class="gp">>>> </span><span class="nb">vars</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
||
<span class="go">{'a': 'titi', 'b': 'toto'}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>puisque tout est objet en python, <code class="docutils literal"><span class="pre">vars(mon_objet)</span></code> est équivalent à
|
||
<code class="docutils literal"><span class="pre">mon_objet.__dict__</span></code></p>
|
||
<ul class="simple">
|
||
<li>générateurs et compréhension de liste</li>
|
||
</ul>
|
||
<p>les compréhensions de listes permettent de générer de nouvelles listes</p>
|
||
<p>exemple :</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">[</span> <span class="mi">2</span><span class="o">*</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)]</span>
|
||
<span class="go">[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span><span class="o">></span><span class="mi">5</span><span class="p">]</span>
|
||
<span class="go">[6, 7, 8, 9]</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">8</span><span class="p">)]</span>
|
||
<span class="go">[(2, 4), (2, 5), (2, 6), (2, 7), (3, 4), (3, 5), (3, 6), (3, 7), (4, 4), (4, 5), (4, 6), (4, 7)]</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>les expressions générateurs</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">expr</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
|
||
<span class="gp">>>> </span><span class="n">expr</span>
|
||
<span class="go"><generator object <genexpr> at 0x7ff9efa77cd0></span>
|
||
<span class="gp">>>> </span><span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">expr</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="nb">print</span> <span class="n">e</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">0</span>
|
||
<span class="go">2</span>
|
||
<span class="go">4</span>
|
||
<span class="go">6</span>
|
||
<span class="go">8</span>
|
||
<span class="go">10</span>
|
||
<span class="go">12</span>
|
||
<span class="go">14</span>
|
||
<span class="go">16</span>
|
||
<span class="go">18</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="le-polymorphisme-parametrique">
|
||
<h2>le polymorphisme paramétrique<a class="headerlink" href="#le-polymorphisme-parametrique" title="Lien permanent vers ce titre">¶</a></h2>
|
||
<p>polymorphisme exemple de contexte :
|
||
la fonction print</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span> <span class="mi">1</span>
|
||
<span class="go">1</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span> <span class="s2">"1"</span>
|
||
<span class="go">1</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition-todo admonition" id="index-0">
|
||
<p class="first admonition-title">À faire</p>
|
||
<p class="last"><code class="docutils literal"><span class="pre">print</span> <span class="pre">1</span></code> et <code class="docutils literal"><span class="pre">print</span> <span class="pre">"1"</span></code> renvoient le même résultat. Pourquoi ?</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
|
||
<a href="search.html"> Recherche</a> |
|
||
<!-- a href="genindex.html"> Genindex</a-->
|
||
|
||
</div>
|
||
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="genindex.html" title="Index général"
|
||
>index</a></li>
|
||
<li class="right" >
|
||
<a href="py-modindex.html" title="Index des modules Python"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="testsunitaires.html" title="Tests unitaires et pile d’appels"
|
||
>suivant</a> |</li>
|
||
<li class="right" >
|
||
<a href="type.html" title="Typage, types de base"
|
||
>précédent</a> |</li>
|
||
<a href="index.html">Programmation python </a> »
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer" role="contentinfo">
|
||
© Copyright 2015, cadoles (www.cadoles.com).
|
||
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
|
||
</div>
|
||
</body>
|
||
</html> |