{ "cells": [ { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "\n", " \n", "\n", "*This notebook contains an excerpt from the [Python Programming and Numerical Methods - A Guide for Engineers and Scientists](https://www.elsevier.com/books/python-programming-and-numerical-methods/kong/978-0-12-819549-9), the content is also available at [Berkeley Python Numerical Methods](https://pythonnumericalmethods.berkeley.edu/notebooks/Index.html).*\n", "\n", "*The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please consider supporting the work on [Elsevier](https://www.elsevier.com/books/python-programming-and-numerical-methods/kong/978-0-12-819549-9) or [Amazon](https://www.amazon.com/Python-Programming-Numerical-Methods-Scientists/dp/0128195495/ref=sr_1_1?dchild=1&keywords=Python+Programming+and+Numerical+Methods+-+A+Guide+for+Engineers+and+Scientists&qid=1604761352&sr=8-1)!*" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "\n", "< [1.4 Introduction to Jupyter Notebook](chapter01.04-Introduction-to-Jupyter-Notebook.ipynb) | [Contents](Index.ipynb) | [Chapter 1 Summary and Problems](chapter01.06-Summary-and-Problems.ipynb) >" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# Logical Expressions and Operators" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "A **logical expression** is a statement that can either be true or false. For example, \$a < b\$ is a logical expression. It can be true or false depending on what values of \$a\$ and \$b\$ are given. Note that this differs from a **mathematical expression** which denotes a truth statement. In the previous example, the mathematical expression \$a < b\$ means that \$a\$ is less than \$b\$, and values of \$a\$ and \$b\$ where \$a \\ge b\$ are not permitted. Logical expressions form the basis of computing, so for the purposes of this book, all statements are assumed to be logical rather than mathematical unless otherwise indicated.\n", "\n", "In Python, a logical expression that is true will compute to the value \"True\". A false expression will compute to the value \"False\". This is a new data type we come across - **boolean**, which has the built-in values `True` and `False`. For the purpose of this book, \"True\" is equivalent to 1, and \"False\" is equivalent to 0. Distinguishing between the numbers 1 and 0 and the logical values \"True\" and \"False\" is beyond the scope of this book, but it is covered in more advanced books on computing. Logical expressions are used to pose questions to Python. For example, \"\$3 < 4\$\" is equivalent to, \"Is 3 less than 4?\" Since this statement is true, Python will compute it as 1. However, \$3 > 4\$ is false, therefore Python will compute it as 0.\n", "\n", "**Comparison operators** compare the value of two numbers, and they are used to build logical expressions. Python reserves the symbols \$>, >=, <, <=, !=, ==\$, to denote \"greater than\", \"greater than or equal\", \"less than\", \"less than or equal\", \"not equal\", and \"equal\", respectively. Let's start with an example a = 4, b = 2, and see the following table:\n", "\n", "| Operator | Description | Example | Results | \n", "| :---: | :---: | :---: | :---: |\n", "| > | greather than | a > b | True|\n", "| >= | greater than or equal | a >= b | True|\n", "| < | less than | a < b | False| \n", "| <= | \"less than or equal | a <= b | False|\n", "| != | not equal | a != b | True| \n", "| == | equal | a == b | False| \n" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**TRY IT!** Compute the logical expression for \"Is 5 equal to 4?\" and \"Is 2 smaller than 3?\"" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 == 4" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 < 3" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**Logical operators** are operations between two logical expressions that, for the sake of discussion, we call \$P\$ and \$Q\$. The fundamental logical operators we will use herein are **and**, **or**, and **not**.\n", "\n", "| Operator | Description | Example | Results | \n", "| :---: | :---: | :---: | :---: |\n", "| and | greather than | P and Q | True if both P and Q are True
False otherwise|\n", "| or | greater than or equal | P or Q | True if either P or Q is True
False otherwise|\n", "| not | less than | not P | True if P is False