Skip to content

Commit

Permalink
Add list indexing bits to Py Syntax Crash Course
Browse files Browse the repository at this point in the history
  • Loading branch information
zstumgoren committed Nov 2, 2023
1 parent e2f1e02 commit 572236a
Showing 1 changed file with 129 additions and 3 deletions.
132 changes: 129 additions & 3 deletions content/python_syntax_crash_course.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "1343b952-6390-4e6e-9644-8559879bdf08",
"metadata": {},
Expand Down Expand Up @@ -572,7 +571,7 @@
"id": "4a96f463-3889-4866-9bf4-dff0788479a2",
"metadata": {},
"source": [
"Then, you can [add data](https://www.w3schools.com/python/python_lists_add.asp) to the end of the list."
"Then, you can [add data](https://www.w3schools.com/python/python_lists_add.asp) to the end of the list. Items will can be found in the order you added them to the list. In the example below, the first item will be `1`, the second `2`, and so on."
]
},
{
Expand All @@ -588,6 +587,133 @@
"numbers"
]
},
{
"cell_type": "markdown",
"id": "fade6375-2d2e-4006-92a0-69d488ce974b",
"metadata": {},
"source": [
"You can pluck values from a list using their position. We can \"index\" into a certain position to grab an item by using square brackets `[]`.\n",
"\n",
"For example, to grab the number `1` in the first position:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0ec394a-92a6-4d43-8784-052f0ded093e",
"metadata": {},
"outputs": [],
"source": [
"numbers[1] # err...what now?"
]
},
{
"cell_type": "markdown",
"id": "a247aa2e-e8c1-4c37-9501-092c45778ac8",
"metadata": {},
"source": [
"Hold up! Ok, so we were expecting `1` to be at index position `1`. Alas, that's not how many programming languages, Python included, tend to count items in a series. Instead, Python starts counting at the number `0`. So the first item in a list is at index position `0`, the second at position `1`, and so on. Yes, it can be confusing at first, but we promise you'll get used to it. Armed with that knowledge, we can grab the numbers we're after:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87166299-fa51-4d4c-ba52-0258ea03dbe2",
"metadata": {},
"outputs": [],
"source": [
"numbers[0] # Get 1 from index position 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b99bb9a9-14a2-44fc-83ac-812db2d9c98c",
"metadata": {},
"outputs": [],
"source": [
"numbers[1] # Get 2 from index position 1"
]
},
{
"cell_type": "markdown",
"id": "2a1aed89-b6ea-49b9-bfa9-51125c021354",
"metadata": {},
"source": [
"You can access a _range_ of numbers in a list using the square brackets with a colon separator (`[start:end]`). The first number on the left of the colon tells Python which index position to start grabbing numbers. The second number tells Python where to stop.\n",
"\n",
"> IMPORTANT: The end number in the range is *non-inclusive*, meaning Python will go up to but not include the number in that index position. \n",
"\n",
"Here's how to grab the first two numbers:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7cd5c28-3556-42fc-b908-6a546d022e6d",
"metadata": {},
"outputs": [],
"source": [
"numbers[0:2] # This should give us the first and second numbers. Remember, the number after the colon is non-inclusive!"
]
},
{
"cell_type": "markdown",
"id": "fe7c83de-2f7e-451c-9991-aad58880f274",
"metadata": {},
"source": [
"If you omit the end number, you can grab everything from a certain index through the end of the list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69c45980-d97f-4e06-a1a3-ef81fa4dbbe0",
"metadata": {},
"outputs": [],
"source": [
"numbers[1:] # Get all numbers in the list, starting at the second position"
]
},
{
"cell_type": "markdown",
"id": "d0e65c6e-f0c6-4ca7-809b-734606548293",
"metadata": {},
"source": [
"You can even grab numbers going back from the end of the list. Since we can't have a negative zero, in this case, we *do* start counting index positions from -1."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0edf1099-a735-4f17-87f9-b141337ab34b",
"metadata": {},
"outputs": [],
"source": [
"numbers[-1] # Get the last number"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77f0e5bd-1184-4caa-9f71-1529cbe3cdb1",
"metadata": {},
"outputs": [],
"source": [
"numbers[-2:] # Get the last two numbers"
]
},
{
"cell_type": "markdown",
"id": "f7da37ea-146d-4827-b401-de9879158ece",
"metadata": {},
"source": [
"Lists have a bunch more useful functionality that we're not covering here, so we encourage you to learn more by visiting the below resources:\n",
"\n",
"- [W3Schools - Python Lists](https://www.w3schools.com/python/python_lists.asp)\n",
"- [Automate the Boring Stuff - Chapter 4 on Lists](https://automatetheboringstuff.com/chapter4/)"
]
},
{
"cell_type": "markdown",
"id": "b82b8a7f-a03d-4760-8a07-b812d85c4ba2",
Expand Down Expand Up @@ -773,7 +899,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.4"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 572236a

Please sign in to comment.