Boston Python Workshop 8/Friday/Tutorial: Difference between revisions

Content added Content deleted
imported>Jesstess
imported>Jesstess
Line 558: Line 558:
====more choices: <code>if</code> and <code>else</code>====
====more choices: <code>if</code> and <code>else</code>====


You can use the <code>else</code> keyword to execute code when the <code>if</code> expression isn't True. Try this:
<b><code>if</code></b> let's you execute some code only if a condition is <code>True</code>. What if you want to execute different code if a condition is <code>False</code>?

Use the <b><code>else</code></b> keyword, together with <code>if</code>, to execute different code when the <code>if</code> condition isn't <code>True</code>. Try this:


<pre>
<pre>
Line 569: Line 571:
</pre>
</pre>


Like with <code>if</code>, the code block under the <code>else</code> statement must be indented so Python knows that it is a part of the <code>else</code> block.
Like with <code>if</code>, the code block under the <code>else</code> condition must be indented so Python knows that it is a part of the <code>else</code> block.


====compound conditionals: <code>and</code> and <code>or</code>====
====compound conditionals: <code>and</code> and <code>or</code>====