Anonymous user
Boston Python Workshop 8/Friday/Tutorial: Difference between revisions
Boston Python Workshop 8/Friday/Tutorial (view source)
Revision as of 23:05, 12 July 2013
, 10 years ago→more choices: if and else
imported>Jesstess |
|||
(9 intermediate revisions by 2 users not shown) | |||
Line 281:
fish = "humuhumunukunukuapuaʻa"
name_length = len(fish)
fish + " is a Hawaiian fish whose name is " + str(name_length) + " characters long."
</pre>
Line 558:
====more choices: <code>if</code> and <code>else</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>
Line 569 ⟶ 571:
</pre>
Like with <code>if</code>, the code block under the <code>else</code>
====compound conditionals: <code>and</code> and <code>or</code>====
You can check multiple expressions together using the <b><code>and</code></b> and <b><code>or</code></b> keywords. If two expressions are joined by an <code>and</code>, they <b>both</b> have to be <code>True</code> for the overall expression to be <code>True</code>. If two expressions are joined by an <code>or</code>, as long as <b>at least one</b> is <code>True</code>, the overall expression is <code>True</code>.
Try typing these out and see what you get:
Line 617 ⟶ 619:
====even more choices: <code>elif</code>====
If you
<pre>
Line 630 ⟶ 632:
</pre>
You don't have to have an <code>else</code> block, if you don't need it. That just means there isn't default code to execute when none of the <code>if</code> or <code>elif</code> conditions are <code>True</code>:
<pre>
|