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. Module 2
  2. Programming Logic

Conditionality

Conditionals are statements that specify what actions to perform when a given condition is true or false. Why is this important? It helps us tell computers to execute certain actions only when certain conditions are met. But why is this necessary? In a phrase:

"Computers are stupid."

Another way to look at this is if you have butter but no bread, you will not be able to make a sandwich - you are aware of this intuitively but a computer is not. Therefore, we need to write and execute statements that replicate our intuition i.e. make a sandwich only when you have both bread and butter.

How would the situation above look as a conditional?

have_butter = True
have_bread = True
if (have_butter and have_bread): #condition
    print("I made a sandwich :D") #action
else: 
    print("No sandwich :(") #other action

Let's look at the conditional above a little more closely.

  • The suite follows an if-else structure, which is the most common one. Simply interpreted, it means that if this condition, then do that action, else do a different action.

  • The first if statement has two True values with an and operator. The and operator will keep evaluating a statement up until the first false value, and return it.

  • This idea is known as short-circuiting and is extremely useful when we want to evaluate a certain set of conditions.

The rest of this section focuses on conditional statements. If interested about short-circuiting, you can find more information in the appendix.

All conditionals follow the if-else structure demonstrated above, but the structure can be tweaked depending on the problem we are solving. Examples below show more complex conditional suites.

age = 41
if age < 10: 
    print("Oh, you're a baby!") 
elif age >= 10 and age<=35:
    print("Still young!")
else: 
    print("Keep going, still young at heart :)")

Can you guess what the above code prints when the variable age is assigned to 41?

Alternatively, if you find yourself writing conditional statements that depend on the output of some other condition, you could nest the conditional statements - an example is shown below:

temp = 75 
if temp<90 and temp>70:
    if temp<80: 
        print("It's not too bad")
    else: 
        print("A little on the high side")
elif temp<110: 
    print("It's hot")
else: 
    print("It's hot!!!!") 
PreviousLogical OperatorsNextIntroduction to Probability

Last updated 5 years ago

Was this helpful?