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
  • Boolean Operators
  • Logical Operators: AND, OR and NOT
  • Arithmetic Operators: Greater than, less than, equal to

Was this helpful?

  1. Module 2
  2. Programming Logic

Logical Operators

PreviousLoopsNextConditionality

Last updated 5 years ago

Was this helpful?

Boolean Operators

Operations like addition and subtraction return a number when applied to mathematical expressions - in a similar way, boolean operators return either true or false when applied. They are useful in setting conditions to perform certain operations (as we will see later in this chapter). We will focus on three main boolean logical operators: AND, OR and NOT.

Logical Operators: AND, OR and NOT

Imagine that there are three different stacks of sandwiches as follows:

  1. Peanut Butter and Jelly

  2. Peanut Butter or Jelly

  3. Peanut Butter, not Jelly

This means that if you pick a sandwich from Stack 1, it will contain both Peanut Butter and Jelly. If you pick it from Stack 2, depending on the sandwich it can contain either only Peanut Butter or only Jelly. The last stack has only Peanut Butter sandwiches - no sandwiches in this stack have Jelly.

Boolean operators work in the exact same way. The diagram below helps demonstrate how we can use these operators to look for the sandwich we want.

Arithmetic Operators: Greater than, less than, equal to

Arithmetic Operators are straightforward. In fact, you might have already seen them in mathematics. They are used to compare values rather than statements. For example, is 2 greater than 3? The answer is obviously false. In Python we can evaluate these statements in the following ways:

>>>print(2>=3) 
False
>>>print(3>=2)
True
>>>print((4*5) < (2*3))
False
>>>print(4==4) 
True 
>>>print("b">"a")
True

If you ever run into problems with arithmetic operators, here is a handy cheat sheet!

Source: https://sru.libguides.com/c.php?g=531883&p=3898529
Source: https://introcs.cs.princeton.edu/java/11cheatsheet/