Conditionals
Sometimes you want to perform a computation only if certain conditions are met.
For example, if you are trying to find out how long an air conditioner needs to run, you only want to count hours where the outside temperature is above the inside temperature.
Computer scientists call these conditionals. You might have seen IF/THEN statements, where if a condition is true then you do something.
Logic Functions
Our conditions may be complex.
If we want to do something when any of several conditions are true, we use OR.
If we want to do something only when all of several conditions are true, we use AND.
Conditionals in Spreadsheets
Excel and Google Sheets have several functions that implement IF/THEN behavior.
Google Sheets IF Google Sheets SUMIF Google Sheets COUNTIF
Conditionals in Python
Python has an IF/THEN statement, but for numerical computations, we often want to do something with an array of numbers.
In Numerical Python (NumPy), we can create arrays which do something like spreadsheet columns.
We can also use the Pandas library to create a spreadsheet-like object.
The both NumPy and Pandas have a where
function with
similar syntax to the spreadsheet IF function.
Try Jupyter
You can experiment with Python in your browser without installing anything using the Try Jupyter web application.