formations/python/python3/fr/_build/html/structures.html

325 lines
19 KiB
HTML
Raw Normal View History

2018-08-21 10:25:44 +02:00
<!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 &#8212; 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 dappels" 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>&nbsp; &nbsp; 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 dappels"
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> &raquo;
</ul>
</div>
<p>&nbsp;</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">&gt;&gt;&gt; </span><span class="k">if</span> <span class="s2">&quot;a&quot;</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">&quot;a&quot;</span>
<span class="gp">...</span>
<span class="go">a</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="s2">&quot;&quot;</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">&quot;hell&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </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">&quot;un&quot;</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">&quot;deux&quot;</span>
<span class="gp">...</span>
<span class="go">deux</span>
<span class="go">&gt;&gt;&gt;</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">&gt;&gt;&gt; </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">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="mi">10</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt;</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">&gt;&gt;&gt; </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">&quot;&quot;&quot;fonction qui printe&quot;&quot;&quot;</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">x</span>
<span class="gp">...</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>il ny a que des fonctions (et pas de procédures):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&quot;écrit quelque chose dans un fichier&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ma_func</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">ma_func</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">)</span>
<span class="go">None</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>est équivalent à :</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&quot;écrit quelque chose dans un fichier&quot;</span>
<span class="gp">... </span> <span class="k">return</span> <span class="kc">None</span>
</pre></div>
</div>
<ul>
<li><p class="first">arité dune 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">&gt;&gt;&gt; </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">&quot;arguments : &quot;</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">&quot;argumments nommés&quot;</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">&gt;&gt;&gt; </span><span class="n">ma_fonction</span><span class="p">(</span><span class="s2">&quot;toto&quot;</span><span class="p">,</span> <span class="s2">&quot;titi&quot;</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 : (&#39;toto&#39;, &#39;titi&#39;)</span>
<span class="go">argumments nommés {&#39;tata&#39;: 3, &#39;tutu&#39;: 2}</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<ul class="simple">
<li>signature dune fonction : ça peut renvoyer nimporte quoi (tout ce quon 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 dune fonction en plusieurs étape
à laide dun générateur</dd>
<dt id="term-return">return</dt>
<dd>résultat dune fonction</dd>
</dl>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt;</span>
</pre></div>
</div>
<ul class="simple">
<li>espaces de nommage à lintérieur dune fonction :</li>
</ul>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">toto</span><span class="p">(</span><span class="s2">&quot;sdfsdf&quot;</span><span class="p">)</span>
<span class="go">{&#39;x&#39;: &#39;sdfsdf&#39;}</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;titi&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="s2">&quot;toto&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">vars</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">{&#39;a&#39;: &#39;titi&#39;, &#39;b&#39;: &#39;toto&#39;}</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">&gt;&gt;&gt; </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">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&gt;</span><span class="mi">5</span><span class="p">]</span>
<span class="go">[6, 7, 8, 9]</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>les expressions générateurs</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">&lt;generator object &lt;genexpr&gt; at 0x7ff9efa77cd0&gt;</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt;</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">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="mi">1</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="s2">&quot;1&quot;</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">&quot;1&quot;</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> | &nbsp;
<!-- 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 dappels"
>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> &raquo;
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2015, cadoles (www.cadoles.com).
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
</div>
</body>
</html>