-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
309 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,298 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "raw", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"format:\n", | ||
" html:\n", | ||
" code-fold: false\n", | ||
"execute:\n", | ||
" cache: true\n", | ||
"---" | ||
], | ||
"id": "5471fbfa" | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Python Operators\n", | ||
"\n", | ||
"\n", | ||
"We have seen examples of arithmetic operators, and the double equals sign, but let's cover a wide range of operators in Python.\n", | ||
"\n", | ||
"The [Python Operators page from W3 Schools](https://www.w3schools.com/python/python_operators.asp) provides an excellent overview. Contents copied below for reference.\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"## Arithmetic Operators\n", | ||
"\n", | ||
"![Arithmetic Operators. Source: [W3 Schools](https://www.w3schools.com/python/python_operators.asp)](../../images/operators-arithmetic.png)\n", | ||
"\n", | ||
"Examples (with numbers):\n" | ||
], | ||
"id": "eca1ae91" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"print(2 + 10)\n", | ||
"print(2 - 10)\n", | ||
"print(2 * 10)\n", | ||
"print(2 / 10)" | ||
], | ||
"id": "c3180287", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Comparison Operators\n", | ||
"\n", | ||
"![Comparison Operators. Source: [W3 Schools](https://www.w3schools.com/python/python_operators.asp)](../../images/operators-comparison.png)\n" | ||
], | ||
"id": "99e3a734" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"print(2 == 10)\n", | ||
"print(2 != 10)\n", | ||
"\n", | ||
"print(2 < 10)\n", | ||
"print(2 <= 10)\n", | ||
"\n", | ||
"print(2 > 10)\n", | ||
"print(2 >= 10)" | ||
], | ||
"id": "9f21a4ea", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Assignment Operators\n", | ||
"\n", | ||
"![Assignment Operators. Source: [W3 Schools](https://www.w3schools.com/python/python_operators.asp)](../../images/operators-assignment.png)\n", | ||
"\n", | ||
"\n", | ||
"Examples:\n" | ||
], | ||
"id": "573dd29e" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"x = 5\n", | ||
"print(x)\n", | ||
"\n", | ||
"x = x + 1\n", | ||
"print(x)\n", | ||
"\n", | ||
"x += 1\n", | ||
"print(x)" | ||
], | ||
"id": "b22d17cb", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Membership Operators\n", | ||
"\n", | ||
"![Membership Operators. Source: [W3 Schools](https://www.w3schools.com/python/python_operators.asp)](../../images/operators-membership.png)\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"Examples (with strings):\n" | ||
], | ||
"id": "9fd696f6" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"print(\"H\" in \"Hello World\")\n", | ||
"print(\"H\" not in \"Hello World\")" | ||
], | ||
"id": "7d928c7e", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Examples (with lists):\n" | ||
], | ||
"id": "0828e571" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"print(5 in [1,2,5])\n", | ||
"print(5 not in [1,2,5])" | ||
], | ||
"id": "10f1f03d", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Logical Operators\n", | ||
"\n", | ||
"![Logical Operators. Source: [W3 Schools](https://www.w3schools.com/python/python_operators.asp)](../../images/operators-logical.png)\n", | ||
"\n", | ||
"\n", | ||
"Examples (for compound expressions):\n" | ||
], | ||
"id": "4caf5a2a" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"x = 5\n", | ||
"\n", | ||
"print(x > 0 and x > 20)\n", | ||
"print(x > 0 or x > 20)" | ||
], | ||
"id": "9cb21788", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The `and` operator will return `True` if BOTH sides are true.\n" | ||
], | ||
"id": "127b27e2" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"print(True and True)\n", | ||
"print(True and False)\n", | ||
"print(False and True)\n", | ||
"print(False and False)" | ||
], | ||
"id": "424a1d0a", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The `or` operator will return `True` if EITHER side is true.\n" | ||
], | ||
"id": "61e35461" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"print(True or True)\n", | ||
"print(True or False)\n", | ||
"print(False or True)\n", | ||
"print(False or False)" | ||
], | ||
"id": "60ea2fb1", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Truthiness\n", | ||
"\n", | ||
"\n", | ||
"Another way of using the `or` operator is within the concept of \"truthiness\".\n", | ||
"\n", | ||
"In this context, the `or` operator will return the first \"truthy\" value.\n", | ||
"\n", | ||
"\n", | ||
"Values that are considered \"truthy\" are `True`, or any other object that is present or non-empty, or non-blank.\n", | ||
"\n", | ||
"Values that are considered \"falsy\" are `False`, `None`, empty string (`\"\"`), empty list (`[]`), etc.\n" | ||
], | ||
"id": "457776e1" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"0 or \"\" or [] or False or None or 4 or \"Hello World\" or True" | ||
], | ||
"id": "c4810d05", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"In this case, we see `4` returned because it is the first truthy value.\n", | ||
"\n", | ||
"In practice, we usually use \"truthiness\" to set a value if it is not null:\n" | ||
], | ||
"id": "e380c7c3" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"original = \"My Old Value\"\n", | ||
"new = original or \"My New Value\"\n", | ||
"print(new)" | ||
], | ||
"id": "8e5890ca", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": {}, | ||
"source": [ | ||
"original = None\n", | ||
"new = original or \"My New Value\"\n", | ||
"print(new)" | ||
], | ||
"id": "b9fb1b0e", | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"name": "python3", | ||
"language": "python", | ||
"display_name": "Python 3 (ipykernel)" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |