An Introduction To Python - IF Statement, Relational Operators
-
Upload
blue-elephant-consulting -
Category
Documents
-
view
176 -
download
3
Transcript of An Introduction To Python - IF Statement, Relational Operators
An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #5:
IF Statement,
Relational Operators
Decisions!
• One of the essential features of computer programs is their ability to make decisions
• Example: what a car does at a stoplight depending on the color of the light.
• A program can take different actions depending on inputs and other circumstances.
Image Credit: www.xconomy.com
Say Hello To The “IF” Statement
• In Python, an IF statement is used to implement a decision.
• When a condition is fulfilled, one set of statements is executed. Otherwise, another set of statements is executed
Image Credit: www.clipartpanda.com
Example Of Using IF
Is room too
cold?
Set room temperature to
85 degrees
Set room temperature to
72 degrees
Yes No
Syntax Of Python IF Statement
Room Temperature Python Code
thermostatTemperature = 0
if measuredTemp < 70 :
thermostatTemperature = 85
else:
thermostatTemperature = 78
Compound IF Statementsthermostat Temperature = 0
if measuredTemp < 70 :
thermostatTemperature = 85
firePlace = 1
hotChocolate = 1
print(“The room will be heated”)
else :
thermostatTemperature = 78
print(“The room will be cooled”)
Statement
Block
Statement
Block
Sample IF Problem
The variables fuelAmount and fuelCapacityhold the actual amount of fuel and thesize of the fuel tank of a vehicle.
If less than 10 percent is remaining in the tank, a status light should show a red color; otherwise it shows a green color.
Simulate this process by printing out either "red" or "green"
Image Credit: www.pubzi.com
Tabs
• Blockstructured code has the property that nested statements are indented by one or more levels:
if totalSales > 100.0 :
discount = totalSales * 0.05
totalSales = totalSales − discount
print("You received a discount of $%.2f" % discount)
else :
diff = 100.0 − totalSales
if diff < 10.0 :
print("purchase our item of the day & you can receive a 5% discount.")
else :
print("You need to spend $%.2f more to receive a 5% discount." % diff)
Image Credit: www.clipartpanda.com
Tabs
• Python requires blockstructured code as part of its syntax. The alignment of statements within a Python program specifies which statements are part of a given statement block.
• How do you move the cursor from the leftmost column to the appropriate indentation level? A perfectly reasonable strategy is to hit the space bar a sufficient number of times.
• With most editors, you can use the Tab key instead. A tab moves the cursor to the next indentation level. Some editors even have an option to fill in the tabs automatically
Image Credit: imgarcade.com
Coding Best Practice:Avoid Duplicates In Branches
• Look to see whether you duplicate code in each branch. If so, move it out of the if statement. Here is an example of such duplication:
if measuredTemp < 70 :thermostatTemperature = 85
print(“New Room Temperature:", thermostatTemperature )else :
thermostatTemperature = 78
print(“New Room Temperature:", thermostatTemperature )
Image Credit: www.canstockphoto.com
Coding Best Practice:Avoid Duplication In Branches
• The output statement is exactly the same in both branches. This is not an error—the program will run correctly. However, you can simplify the program by moving the duplicated statement, like this:
if measuredTemp < 70 :thermostatTemperature = 85
else :thermostatTemperature = 78
print(“New Room Temperature:", thermostatTemperature )
Image Credit: www.canstockphoto.com
Conditional Expressions• Python has a conditional operator of the form:
value 1 if condition else value2
• The value of that expression is either value1 if the condition is true or value2 if it is false. For example, we can compute the actual temperature number asthermostatTemperature = 85 if measuredTemp < 72 else 78
which is equivalent as:if measuredTemp < 72 :
thermostatTemperature = 85
else :thermostatTemperature = 78
• Note that a conditional expression is a single statement that must be contained on a single line or continued to the next line.
• Also note that a colon is not needed because a conditional expression is not a compound statement
Image Credit: www.fotosearch.com
Relational Operators
Equality
Testing
Requires
2 “=“
Note: relational operators have a lower precedence than arithmetic operators
Examples Of Relational Operators
The Python ATM Machine
Image Credit: www.canstockphoto.com
Create software that will provide an ATM user
with the proper change for any dollar amount
up to $200.
Example: Run the code for $200 and for $19
ATM Machine Algorithm
• is the amount > $100, then provide a $100 bill & subtract $100
• is the amount > $100, then provide a $100 bill & subtract $100
• is the amount > $20, then provide a $20 bill & subtract $20 [repeat 4 times]
• is the amount > $10, then provide a $10 bill and subtract $10
• is the amount > $5, then provide a $5 bill and subtract $5
• provide the remaining amount in single $1 bills
Image Credit: www.gograph.com
What’s In Your Python Toolbox?
print() math strings I/O IF/Else
What We Covered Today
1. If / Else
2. Relational Operators
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
What We’ll Be Covering Next Time
1. Nested Branches
2. Multiple Alternatives
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/