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

imported>Jesstess
 
(6 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>====
 
<b><code>if</code></b> let'slets 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:
Line 619:
====even more choices: <code>elif</code>====
 
If you haveneed to execute code conditional based on more than two cases, you can use the <b><code>elif</code></b> keyword to check more cases. You can have as many <code>elif</code> cases as you want; Python will go down the code checking each <code>elif</code> until it finds a <code>True</code> condition or reaches the default <code>else</code> block.
 
<pre>
Line 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>
Anonymous user