SlideShare a Scribd company logo
1 of 20
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 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
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
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 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
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/

More Related Content

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
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
  • 5. Syntax Of Python IF Statement
  • 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
  • 14. Relational Operators Equality Testing Requires 2 “=“ Note: relational operators have a lower precedence than arithmetic operators
  • 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

  1. 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.