Changes

Jump to: navigation, search

OPS435 Python Lab 2

80 bytes added, 13:26, 1 August 2017
INVESTIGATION 2: USING LOGIC STATEMENTS
:'''Perform the following steps'''
:#Open an ipython3 shell:<sourcelang="bash">
ipython3
</source>
:#Let's create an if statement from the ipython3 shell<br>Issue the following 2 lines, indenting the second line (you may need to press ENTER a second time for statement to execute):<sourcelang="python">
if True:
print('This print is apart of the if statement')
<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>
:#Issue the following 3 lines, indenting the second and third lines, but NOT the fourth line:<sourcelang="python">
if False:
print('This first print statement will never run')
print('This print statement will run')
</source>What do you notice?<br><br>So far, you have been using only the '''Boolean values True or False''' for your IF statements. Although this can be useful, it can be more practical to state a condition to test in order to render a '''True''' or '''False''' Boolean value to be used with control flow statements (referred to as: '''Boolean Logic''').<br><br>
:#let's create an IF statement that runs under specific conditions. Issue the following code below:<sourcelang="python">
password = input('Please enter a secret password')
if password == 'P@ssw0rd':
print('You have succesfully used the right password')
</source>What happened? In the above example, you are making a comparison between the value you entered via the '''input()''' function, which in turn, was saved into the '''password''' variable. The IF statement is using that variable (called password), followed by '''==''' which represents '''identical to''', followed by the string ''' 'P@ssw0rd' '''. Never use '''=''' to 'compare values since this is used to store the value into a variable and may not allow IF statement to properly test the condition! Also note that a '''space is used to separate arguments with the IF statement'''. The IF statement tests that condition to see if it is '''True or False'''. If that condition is '''True''', it will run the code indented below. On the other hand, if that condition is '''False''', it will not run the code. Try experimenting with different combinations of passwords.<br><br>If statements can also be used to compare numbers. We can do this by using comparison operators (such as: '''==''', '''!=''', '''>''', '''>=''', '''<''', '''<='''), but we can also use functions. The function '''len()''' can be used to return the number of characters in a word (plus other features). length of words and other variables. We can also use the '''len()''' function to give us the number of argumuents provided to our script by using 'len(sys.argv)' and it should return a number. Below we are also using '!='. Which stands for not eqal to. <br><br>
:#Issue the following lines of code:<sourcelang="python">
import sys
Usage: lab2d.py name age
</source>
:::3. Download the checking script and check your work. Enter the following commands from the bash shell.<sourcelang="bash">
cd ~/ops435/lab2/
pwd #confirm that you are in the right directory

Navigation menu