{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"toc_visible":true,"authorship_tag":"ABX9TyMOnhY533UuabFUWuVq3sHr"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Operators"],"metadata":{"id":"vlvs0vpiyY2z"}},{"cell_type":"markdown","source":["## Arithmetic Operators\n","\n","Most programming languages allow you to perform arithmetic operations and mathematical calculations using **arithmetic operators**. Common arithmetic operations (with Python examples) include:\n","\n","
Name | Python Syntax | Python Example | Description |
\n"," Addition | + | 5 + 6 | Adds values |
\n"," Subtraction | - | 5 - 6 | Subtracts values |
\n"," Multiplication | * | 5 * 6 | Multiples values |
\n"," Integer division | // | 5 // 6 | Divides values, integers (whole numbers) |
\n"," Float division | / | 5 / 6 | Divides values, floating point numbers (decimal values) |
\n"," Modulo | % | 5 % 6 | Returns or retrieves remainder (whole number) from division operation |
\n"," Exponent | ** | 5 ** 6 | Calculates exponent |
\n","
\n"," \n","For more on arithmetic operators in Python:\n","- [W3Schools, Python Arithmetic Operators](https://www.w3schools.com/python/gloss_python_arithmetic_operators.asp)\n","\n","We can run arithmetic operations directly in the console or in a script."],"metadata":{"id":"u9px3H-c6R95"}},{"cell_type":"code","source":["# sample arithmetic operation\n","5 + 7"],"metadata":{"id":"5u6s-nSw6SlE"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["But we can also assign the output of an arithmetic operation to a variable.\n"],"metadata":{"id":"1uLE34yB6TYe"}},{"cell_type":"code","source":["# sample arithmetic operation where output is assigned to variable\n","x = 5 + 7\n","\n","# show variable value\n","print(x)"],"metadata":{"id":"DGyO-ghG6UDv"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["
\n","\n","Python follows the PEDMAS order of operations: parenthesis, exponents, multiplication, division, addition, subtraction.\n","- *When in doubt, use parenthesis!*\n","\n","For more background on arithmetic operators:\n","- [Elements of Computing I \"Python Intro\" lab](https://github.com/kwaldenphd/python-intro#arithmetic-operators)\n","\n","## Comparison Operators\n","\n","Relational operators, also called comparison operators, allow us to run `True/False` tests on specific conditions, focusing on the relationship(s) between two or more values.\n","\n","Relational operators or comparison operators in Python:\n","\n","Name | Syntax | Example | Description |
\n"," Equal | == | x == y | Tests if values are equal |
\n"," Not equal | != | x != y | Tests if values are not equal |
\n"," Greater than | > | x > y | Tests is a value is greater than another |
\n"," Less than | < | x < y | Tests is a value is less than another |
\n"," Greater than or equal to | >= | x >= y | Tests if a value is greater than or equal to another |
\n"," Less than or equal to | <= | x <= y | Tests if a value is less than or equal to another |
\n","
\n"," \n","For more on comparison operators in Python:\n","- [W3Schools, Python Comparison Operators](https://www.w3schools.com/python/gloss_python_comparison_operators.asp)\n","\n","- \"In mathematics and mathematical logic, Boolean algebra is the branch of algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0\" ([Wikipedia](https://en.wikipedia.org/wiki/Boolean_algebra))\n","\n","We've talked previously about the Boolean data type. The underlying `True`/`False` logic lets us test for specific conditions and then specify how our code will execute based on the truth value for those conditional statements.\n","\n","- \"You can evaluate any expression in Python, and get one of two answers, `True` or `False`. When you compare two values, the expression is evaluated and Python returns the Boolean answer\" ([W3Schools, Python Booleans](https://www.w3schools.com/python/python_booleans.asp))"],"metadata":{"id":"qEkriNZm6Wpv"}},{"cell_type":"markdown","source":["### Boolean Logic & Comparison Operators\n"," \n","We can use comparison operators with a `print()` statement to test whether a particular statement is `True` or `False`.\n"," \n","A couple Python examples:"],"metadata":{"id":"0nYH8NWt6h2K"}},{"cell_type":"code","source":["print(4 == 5) # returns false\n","print(6 < 10) # returns true"],"metadata":{"id":"BuUMWMq46ijc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# comparison operator using variables\n","x = 5\n","y = 6\n","\n","print(x > y) # returns false"],"metadata":{"id":"y-rMBc2O6jf0"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### Comparison Operators & String Objects\n","\n","We can probably make intuitive sense of how these comparison operators would work for numeric values. But what about text characters or string objects?\n"," \n","Most programming languages treat the 26 characters in the English-language alphabet (`a-z`) as sequential values, where `a` is less than `b`, which is less than `c`, etc.\n","\n","When working with string objects that represent words or characters, comparison operators indicate positionality in the English-language alphabet.\n","\n","A few examples in Python:"],"metadata":{"id":"4bdZ5bPE6kfC"}},{"cell_type":"code","source":["print(\"apple\" > \"banana\") # returns false\n","\n","print(\"Ohio State\" > \"Notre Dame\") # returns true"],"metadata":{"id":"YtVgQA7N6lVy"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### Logical Operators\n","\n","We can also compare or relate multiple conditions using logical operators: `and`, `or`, `not`\n","\n","- \"A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.\" (Busbee and Braunschweig, \"[Logical Operators](https://press.rebus.community/programmingfundamentals/chapter/logical-operators/)\") \n","\t \n","For example, `5 < 6 and 6 < 7` evaluates whether `5 < 6` and `6 < 7` are true. This statement would return `True`.\n","\n","Another example: `5 < 6 or 6 > 7` evaluates whether `5 < 6` or `6 > 7` is true. This statement would return `True`.\n","\n","Logical operators in Python:\n"," \n","Name | Syntax | Example | Description |
\n"," And | and | x == y and y != z | Returns True if both statements are true |
\n"," Or | or | x != y or y == z | Returns True if one of the statements is true |
\n"," Not | not | not(x == y and y != z) | Reverses the result, returns False if the result is true |
\n","
\n"," "],"metadata":{"id":"el3nlMh16n3m"}},{"cell_type":"code","source":["# example using logical operators\n","print(5 < 6 and 6 < 7) # returns true because both statements are true\n","\n","print(5 < 6 or 6 > 7) # returns true because at least one statement is true\n","\n","print(not(5 < 6 and 6 < 7)) # returns false, inverse of initial true"],"metadata":{"id":"0rs7MTad6o1R"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## Additional Resources\n","\n","For more background on comparison/relational and logical operators in Python:\n","- [Elements of Computing I \"Comparison Operators & Conditional Statements\" lab](https://github.com/kwaldenphd/python-conditional-statements)"],"metadata":{"id":"lb9nPgTRywDi"}}]}