{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.16 Boolean Operators `and`, `or` and `not` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We use the conditional operators `==`, `!=`, `<`, `<=`, `>`, and `>=` to form simple condition expressions such as `score >= 90`. **Boolean operators** `and`, `or`, and `not` allow us to form more complex conditional expressions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean Operator `and`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gender = 'Female'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "age = 60" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if gender == 'Female' and age >= 65:\n", " print('Senior female')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Redundant parentheses help clarify the `if` statement for human readers:\n", "####
`if (gender == 'Female) and (age >= 65):`
`(gender == 'Female) and (age >= 65)`
\n", "#### and it happens that `(gender == 'Female)` is false, then the entire expression must be false. (Why?) In that case, Python doesn't bother to evaluate `(age >= 65)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Similarly, when when Python evaluates the expression \n", "####
`(semester_average >= 90) or (final_exam >= 90)`