{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Comparisons\n", "\n", "Boolean values most often arise from comparison operators. Python includes a variety of operators that compare values. For example, `3` is larger than `1 + 1`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 > 1 + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The value `True` indicates that the comparison is valid; Python has confirmed this simple fact about the relationship between `3` and `1+1`. The full set of common comparison operators are listed below.\n", "\n", "| Comparison | Operator | True example | False Example |\n", "|--------------------|----------|--------------|---------------|\n", "| Less than | < | 2 < 3 | 2 < 2 |\n", "| Greater than | > | 3>2 | 3>3 |\n", "| Less than or equal | <= | 2 <= 2 | 3 <= 2 |\n", "| Greater or equal | >= | 3 >= 3 | 2 >= 3 |\n", "| Equal | == | 3 == 3 | 3 == 2 |\n", "| Not equal | != | 3 != 2 | 2 != 2 |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An expression can contain multiple comparisons, and they all must hold in order for the whole expression to be `True`. For example, we can express that `1+1` is between `1` and `3` using the following expression." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 1 + 1 < 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The average of two numbers is always between the smaller number and the larger number. We express this relationship for the numbers `x` and `y` below. You can try different values of `x` and `y` to confirm this relationship." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 12\n", "y = 5\n", "min(x, y) <= (x+y)/2 <= max(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings can also be compared, and their order is alphabetical. A shorter string is less than a longer string that begins with the shorter string." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Dog\" > \"Catastrophe\" > \"Cat\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.12" } }, "nbformat": 4, "nbformat_minor": 2 }