This presentation is a part of the COP2271C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce Freshmen students to both the process of software development and to the Python language.
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
A video of Dr. Anderson using these slides is available on YouTube at:
https://www.youtube.com/watch?x-yt-ts=1421914688&v=sYduT4ni9r8&x-yt-cl=84503534&feature=player_embedded
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
An Introduction To Python - IF Statement, Relational Operators
1. An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #5:
IF Statement,
Relational Operators
2. 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
3. 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
4. Example Of Using IF
Is room
too
cold?
Set room
temperature to
85 degrees
Set room
temperature to
72 degrees
Yes No
6. Room Temperature Python Code
thermostatTemperature = 0
if measuredTemp < 70 :
thermostatTemperature = 85
else:
thermostatTemperature = 78
7. Compound IF Statements
thermostat 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
8. Sample IF Problem
The variables fuelAmount and fuelCapacity
hold the actual amount of fuel and the
size 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
9. 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
10. 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
11. 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
12. 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
13. 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 as
thermostatTemperature = 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
16. 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
17. 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
18. What’s In Your Python Toolbox?
print() math strings I/O IF/Else
19. What We Covered Today
1. If / Else
2. Relational Operators
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
20. 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/
Editor's Notes
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.