Skip to content

Instantly share code, notes, and snippets.

@gwbischof
Created April 1, 2021 16:25
Show Gist options
  • Select an option

  • Save gwbischof/a1ba62a9424e2f27f95fc7b6addd0470 to your computer and use it in GitHub Desktop.

Select an option

Save gwbischof/a1ba62a9424e2f27f95fc7b6addd0470 to your computer and use it in GitHub Desktop.
Python teaching material1
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# What is a list?\n",
"\n",
"A list in python is an object just like the list in our normal life.\n",
"\n",
"A list in python is also a Data Structure. A structure that we can organize our data in.\n",
"\n",
"Just like the lists that we are familiar with, we can:\n",
"\n",
" 1. Add an item to the end of the list.\n",
" 2. Add an item to the middle of the list, but this might require rewriting the items after the new item to make room for the new item.\n",
" 3. Delete an item from the end of the list.\n",
" 4. Delete an item from the middle of the list. This might require rewriting of the list so that there isn't a gap between list items.\n",
" 5. Sort the list.\n",
" 6. Access an item at any position.\n",
" 7. Access a range of items."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"# A list can be created like this.\n",
"# This is a empty list.\n",
"a = []\n",
"\n",
"\n",
"# Create a list with some numbers in it.\n",
"b = [1,2,3]\n",
"\n",
"\n",
"# Create a list with string in it. (A string is a term for text data)\n",
"# Strings need to be surrounded by quotation marks. (Single or Double quotes work interchangably)\n",
"# The convention is to use single quotes for data, \n",
"# and double quotes for something that someone will read (like a sentence)\n",
"c = ['a', 'b', 'c', 'd']\n",
"\n",
"\n",
"# Create a list with mixed data types.\n",
"# You can stick any type of data type or object in a list.\n",
"# The last item of list `d` is `a`. Because `a` does not have quotation marks, it is not a string. \n",
"# Instead, it refers to the empty list `a` that was created previously.\n",
"d = [1, 'a', 3.3, \"Good Morning.\", a]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1]\n"
]
}
],
"source": [
"# What can you do with a list?\n",
"# Let's start with a new empty list.\n",
"a = []\n",
"\n",
"\n",
"# Try typeing a, then a dot, then press tab to autocomplete. \n",
"# This will tell you all of the methods and attributes available to a list.\n",
"# If you type a question mark after the method name it will tell you some information about that method.\n",
"a.append?\n",
"\n",
"\n",
"# A method is an operation that can be performed on the list.\n",
"# To use a method, you need to \"call\" it by placing a pair of parenthesis after it.\n",
"a.append(1)\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"# Accessing a list.\n",
"# Let's start with a new list with some integers in it.\n",
"a = [1, 1, 2, 5]\n",
"\n",
"# To access an item in the list use the square brackets, \n",
"# inside the square brackets write the index of the item you want.\n",
"# For example: \n",
"print(a[1])\n",
"\n",
"# What will print out if we do the following?\n",
"# print(a[2])\n",
"# What does this mean about where the index starts?"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3]\n",
"[1, 2, 3]\n",
"[1, 2, 3, 4, 5]\n",
"[1, 2, 3, 4, 5]\n",
"[1, 3, 5]\n"
]
}
],
"source": [
"# Slicing a list.\n",
"# Let's make a new list.\n",
"a = [1, 2, 3, 4, 5]\n",
"\n",
"\n",
"# The list can be sliced by putting the starting index, than a colon, then the ending index in the square brackets.\n",
"print(a[1:3])\n",
"# We see that it prints elements 1, and 2, but not 3. This means that the second index is not inclusive.\n",
"\n",
"\n",
"# We leave out the first index to slice from the start of the list.\n",
"print(a[:3])\n",
"\n",
"\n",
"# Or leave out the second index to slice until the end of the list.\n",
"print(a[:])\n",
"\n",
"\n",
"# You can leave out both indexes also.\n",
"print(a[:])\n",
"\n",
"\n",
"# You can also take every nth item, by adding another colon and then the step size.\n",
"print(a[0:5:2])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"source": [
"# The For loop.\n",
"# We access each item in the list sequentially by using a for loop.\n",
"# All that you need to type is:\n",
"\n",
"a = [1,2,3,4,5]\n",
"for item in a:\n",
" print(item)\n",
" \n",
" \n",
"for thing in a:\n",
" print(thing)\n",
" \n",
"# In the first loop we call each element \"item\", in the second loop we call each element \"thing\".\n",
"# We pick the name we want to call each element when we write the loop. You can put any name that you want there.\n",
"\n",
"# Loops are super useful!\n",
"# They are used very frequently.\n",
"\n",
"# Loops and lists are universal and appear in all programming languages. \n",
"# Although the code might be worded a little differently.\n",
"# Most concepts in programming are universal to all programming languages.\n",
"# So if you learn them in python, you understand them in any language.\n",
"# It is like learning music theory. The theory that you learn works for any musical instrument."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is 1\n"
]
}
],
"source": [
"# The If statement.\n",
"# Below is a simple if statement.\n",
"# If the condition evaluates to True, then the indented code is run.\n",
"# The \"condition\" is the code following the \"if\", and before the \":\"\n",
"# The condition needs to evaluate to True or False.\n",
"# == means to check if the two values are equal.\n",
"# < is less than.\n",
"# <= is less than or equal to.\n",
"# != means not equal.\n",
"# You can google other comparison operators.\n",
"\n",
"a = 1\n",
"if a == 1:\n",
" print(\"a is 1\")\n",
" \n",
"b = 1\n",
"if b == 2:\n",
" print(\"b is 2\")"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b is not 2\n"
]
}
],
"source": [
"# If-else\n",
"# In the previous example nothing printed about b.\n",
"# This is because b was not 2, therefore the indented code did not run.\n",
"# We can add an \"else\" to the if statement. \n",
"# Any code that is put in the else section will be run if the condition evalueates to false.\n",
"\n",
"b = 1\n",
"if b == 2:\n",
" print(\"b is 2\")\n",
"else:\n",
" print(\"b is not 2\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lets solve a problem combining the tools we just learned.\n",
"\n",
"Write some code to print all of the even numbers in a list."
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# You can check if a number is even like this:\n",
"b = 2\n",
"print(b % 2 == 0)\n",
"# The % is the modulo operator, it returns the remainder of the division of b / 2.\n",
"# So if b is even, and you divide it by 2, the remained will always be 0. \n",
"# Therefore the previous statement will always evaluate to True when b is even.\n"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"# Here is your list:\n",
"a = [1, 2 ,3 , 4, 4, 6, 3, 2, 1, 5, 7, 6, 9, 0, 5, 10, 3, 5, 7, 6, 4]\n",
"\n",
"# Print all of the even numbers in this list.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment