Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 2

No change in size, 15:29, 12 September 2019
PART 1 - Using IF Statements
</source>What happened when you ran this code? It is important to note a couple of things with the IF statement:<ul><li>When the expression in an IF statement '''evaluates to True''', it runs the code that is indented underneath it. In this case, we can use the boolean value "True" to make this happen, or test to see if an expression is evaluated to true or false.</li><li>However, if the expression in an IF statement evaluates to '''False''', then it will not run the code indented underneath it. Any code not indented under the IF statement will perform normally as the main program and is NOT associated with the control flow statement.</li><li>Indentation means to start a line with spaces or tabs before your text. Using '''indentation''' will direct the script what code should be run as part of the IF statement and which code should be run regardless. Also, using indentation makes it easier for a programmer to identify Control Flow statements. From this point on, be VERY careful and consistent with indentation because it will affect how your code works. </li></ul>
<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|4 spaces|While python allows some flexibility with your indentation - please don't be creative with it. Always use 4 spaces for each new block. There will be exceptions later on, but start with this now. You may find it helpful to configure your editor to insert for spaces instead of a tab when you press the tab key.}}</blockquote>
<ol><li value='3'>Try the following 3 4 lines, indenting the second and third lines, but NOT the fourth line:<source lang="python">
if False:
print('This first print statement will never run')
1,760
edits