Opportunity Through Data Textbook
  • Opportunity Through Data Textbook
  • Introduction
    • What is Data Science?
    • Introduction to Data Science: Exploratory Musical Analysis
  • Module 1
    • Introduction to Programming
      • The Command Line
      • Installing Programs
      • Python and the Command Line
      • Jupyter Notebook
    • Introduction to Python
      • Building Blocks of Python - Data Types and Variables
      • Functions
      • Formatting and Syntax
    • Math Review
      • Variables and Functions
      • Intro to Graphs
  • Module 2
    • Data Structures
      • Lists
      • Dictionaries
      • Tables
    • Programming Logic
      • Loops
      • Logical Operators
      • Conditionality
  • Module 3
    • Introduction to Probability
      • Probability and Sampling
    • Introduction to Statistics
      • Mean & Variance
      • Causality & Randomness
  • Module 4
    • Packages
    • Intro to NumPy
      • NumPy (continued)
  • Module 5
    • Introduction to Pandas
      • Introduction to Dataframes
      • Groupby and Join
    • Working with Data
    • Data Visualization
      • Matplotlib
      • Introduction to Data Visualization
  • Appendix
    • Table Utilities
    • Area of More Complicated Shapes
    • Introduction to Counting
    • Slope and Distance
    • Short Circuiting
    • Linear Regression
    • Glossary
  • Extension: Classification
    • Classification
    • Test Sets and Training Sets
    • Nearest Neighbors
  • Extension: Introduction to SQL
    • Introduction to SQL
    • Table Operations
      • Tables and Queries
      • Joins
  • Extension: Central Limit Theorem
    • Overview
    • Probability Distributions
      • Bernoulli Distribution
      • Uniform Distribution (Discrete)
      • Random Variables, Expectation, Variance
      • Discrete and Continuous Distributions
      • Uniform Distribution (Continuous)
      • Normal Distribution
    • Central Limit Theorem in Action
    • Confidence Intervals
  • Extension: Object-Oriented Programming
    • Object-Oriented Programming
      • Classes
      • Instantiation
      • Dot Notation
      • Mutability
  • Extension: Introduction to Excel
    • Introduction to Excel
      • Terminology and Interface
      • Getting Started with Analysis and Charts
      • Basics of Manipulating Data
    • Additional Features in Excel
      • Macros
      • The Data Tab
      • Pivot Tables
Powered by GitBook
On this page

Was this helpful?

  1. Appendix

Short Circuiting

This section continues with the idea of short circuiting using boolean operators introduced in Module 4.

evaluate_and = True and False #returns False
#The statement below returns False, but does not evaluate the last True value. 
evaluate_and2 = True and False and True 

How would this look in practice?

have_pb = True
have_jelly = True 
have_bread = True 
have_butter = False
have_mayo = False

#This is a function in Python. You do not have to know what it is or how to write one. 
#For the sake of this exercise only, we will be using it to simulate a child asking
#asking for a sandwich. 
def child():
    return "I want a sandwich"
######

if (have_pb and have_jelly and have_bread):
    print("Make a PB and J sandwich")

The statement above will always print because all the variables have true values.

if (have_bread and have_butter and have_mayo):
    print("Here is a basic sandwich")
else: 
    print("I'm sad")
#The statement above will always print I'm sad, because have_butter and have_mayo are 
#false values. 

However, if we change the above and operator to or, we get the following:

if (have_bread or have_butter or have_mayo):
    print("I have bread")
else: 
    print("I'm sad")
#This will always print I have bread, because there is one true value (have_bread).
print(have_bread or child()) 

The above expression prints True because the first value is a true value and the expression short circuits and does not evaluate child(). Conversely:

print(have_butter or child())
#This prints "I want a sandwich" because a the operator evaluated until the first true
#statement because have_butter is false. 
print(have_pb and have_bread and have_butter and child())
#This prints False because the expression short-circuits and child() is not evaluated.

Let us look at more complex combinations of the and/or operators.

print((have_butter and have_bread) or child())
#This prints "I want a sandwich" because the and statement evaluates to False
#The or statement evaluates to child()

print((have_butter or have_bread or child()) and (have_butter or have_mayo))
#This will evaluate to False because the first expression evaluates to True and 
#the second to false because there is no true value. When joined by an and operator, 
#it becomes print(True and False), which we know prints False. 

If you are still slightly confused, here is a handy cheatsheet!

What would the following statements evaluate to?

def adult():
    return "I hate cake"
have_cupcake = True
have_icecream = False
have_cakepop = True
have_soda = False
  1. print(have_cupcake and have_icecream and adult())

  2. print(have_icecream or adult() or have_cupcake())

  3. print((have_cakepop and adult()) or (adult() and have_icecream))

Answers

  1. False

  2. True

  3. "I hate Cake"

PreviousSlope and DistanceNextLinear Regression

Last updated 5 years ago

Was this helpful?

Source: https://www.geeksforgeeks.org/short-circuiting-techniques-python/