Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
2465 lines (2464 sloc) 58.5 KB
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "r7V5qyNMbTXA"
},
"source": [
"# Quiz 1 Review"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "aVxwLmWhbaFo"
},
"source": [
"## Learning Objectives\n",
"\n",
"### 1.1\n",
"- Define integers, strings, tuples, lists, and dictionaries\n",
"- Demonstrate arithmetic operations and string operations\n",
"- Learn to use basic Jupyter Notebook features\n",
"\n",
"### 1.2\n",
"- Explore `Python` control flow and conditional programming. \n",
"- Apply `if, else` conditional statements.\n",
"- Combine control flow and conditional statements to solve the classic \"FizzBuzz\" code challenge.\n",
"- Demonstrate error-handling using `try, except` statements.\n",
"\n",
"### 1.3\n",
"- Review basic programming concepts (control flow, iteration, datatypes)\n",
"- Create functions\n",
"- Lambda functions\n",
"- Introduce `NumPy`\n",
"\n",
"### 1.4\n",
"- While loops\n",
"- For loops\n",
"- Looping through dictionaries\n",
"- Mapping/filtering (lists & dicts)\n",
"- List comprehensions\n",
"\n",
"### 1.5\n",
"- Define experiment, outcome, event, and sample space.\n",
"- Calculate the union and intersection of sets.\n",
"- Apply six probability rules.\n",
"- Import packages into Python.\n",
"- Solve probability problems using simulations.\n",
"\n",
"### 1.6\n",
"- Define distribution and random variable.\n",
"- Describe the difference between discrete and continuous random variables.\n",
"- Understand the difference between probability mass functions and cumulative density functions.\n",
"- Give examples of the following distributions: Discrete Uniform, Bernoulli, Binomial, and Poisson.\n",
"\n",
"### 1.7\n",
"- Give examples of the following distributions: Continuous Uniform, Exponential, Normal.\n",
"- Describe why the Normal distribution is seen everywhere.\n",
"- State the Central Limit Theorem."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "GKo_cjbFds3z"
},
"source": [
"# Data Types\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "at8ItFbWfJnu"
},
"source": [
"## Numeric\n",
"\n",
"For now, we can think of our two main numeric types as `int` and `float`. In Python 3, these numbers should behave identically when doing mathematical operations, but some functions specifically require an `int` or a `float`. The big difference: `float`s will always have a number after the decimal (the integer `2` will be `2.0` as a `float`.)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 610,
"status": "ok",
"timestamp": 1533227849783,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "yR_mqNeUfIn8",
"outputId": "8223ffdb-76ea-4d4f-a8ac-8da9d4bdb241"
},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(2)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 357,
"status": "ok",
"timestamp": 1533227852096,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "fQ_Yv61sfDN3",
"outputId": "983bff50-49b5-422d-845d-9b3d3492e84c"
},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(2.0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "tCk0rPBugaOv"
},
"source": [
"## Numeric operators\n",
"\n",
"| Symbol | Operation |\n",
"| --- | --- |\n",
"| + | addition |\n",
"| - | subtraction |\n",
"| * | multiplication |\n",
"| / | division |\n",
"| ** | exponentiation |\n",
"| // | floor division |\n",
"| % | modulo (remainder) |\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 306,
"status": "ok",
"timestamp": 1533228343000,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "hfaeZtktgZXP",
"outputId": "6ad888ec-0e00-48d3-ab29-dd737bd72cf2"
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 ** 2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 329,
"status": "ok",
"timestamp": 1533228352501,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "wNxC5hyNh--L",
"outputId": "94a49599-d79c-4ae0-827f-8fb61a7a42f9"
},
"outputs": [
{
"data": {
"text/plain": [
"4.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2.0 ** 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 322,
"status": "ok",
"timestamp": 1533227875463,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "7X_uBUoAgI1I",
"outputId": "6274eaed-1a7e-4dfc-82c2-68c8bf17c51e"
},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1/2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 339,
"status": "ok",
"timestamp": 1533227885333,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "StS_UpfRgNZ8",
"outputId": "76ecc92f-05c8-4e58-f271-643961447f1f"
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2/2"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 373,
"status": "ok",
"timestamp": 1533227897751,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "gCMOMkFfgQVT",
"outputId": "7e9c3f3b-00a8-42d3-aadd-681234ca2159"
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2//2"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 416,
"status": "ok",
"timestamp": 1533227909254,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "5g_RRib_gR7q",
"outputId": "50eca1ea-ada4-497f-97aa-c2dc9c029e5c"
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1//2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 364,
"status": "ok",
"timestamp": 1533228376591,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "cuAi6KCMiE0J",
"outputId": "5959eec5-2c9d-465c-da3b-07ffe480a7d6"
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"12 % 3"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 559,
"status": "ok",
"timestamp": 1533228385892,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "w1MEr4VViHCQ",
"outputId": "ced9495f-e8ae-480d-d552-d8f486ab8832"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"12 % 5"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "_T1d8tLWiNPN"
},
"source": [
"## Strings\n",
"\n",
"Strings are used for text/words in Python (but we can also cast numbers or puncuation as strings for certain operations).\n",
"\n",
"We can use single or double quotes to define strings (`\"\"` or `''`).\n",
"\n",
"The `str()` function will cast other datatypes to string.\n",
"\n",
"Strings are immutable iterables."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "S31W83g1i5f9"
},
"outputs": [],
"source": [
"name = 'Teacher McTeacherson'"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "w3FI_naqi_NW"
},
"outputs": [],
"source": [
"student = \"Teacher's Pet\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "U7FWveBiv64n"
},
"outputs": [],
"source": [
"triple_quotes = \"\"\"She said, \"It's hers.\" \"\"\" "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "nwn9uG0UjIOc"
},
"outputs": [],
"source": [
"the_number_two = 2"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "fCWly4hMjN4k"
},
"outputs": [],
"source": [
"the_string_two = str(the_number_two)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 484,
"status": "ok",
"timestamp": 1533248760686,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "nQR6TaB2jk6Q",
"outputId": "d905d279-282b-4fca-bcf8-597119ef105d"
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(the_string_two)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Xq8HCI74jpFs"
},
"source": [
"## String operators\n",
"\n",
"Strings have many methods associated with them. Amongst these:\n",
"\n",
"| Method | Effect |\n",
"| --- | --- |\n",
"| `join` | concatenates passed string iterable using string as separator |\n",
"| `find` | return lowest index of searched string |\n",
"| `count` | returns the count of passed string |\n",
"| `split` | split string on white space (or passed string arg) |\n",
"| `strip` | returns string without leading/trailing whitespace |\n",
"\n",
"In addition, strings will behave like lists in many cases.\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "ZBq5XKHmmeaf"
},
"outputs": [],
"source": [
"the = 'The'\n",
"ball = 'ball'\n",
"bounces = 'bounces'"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 322,
"status": "ok",
"timestamp": 1533249310023,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "kWFYjpPfm0Jr",
"outputId": "0f474fa6-3908-49a6-9831-7dc07bdc6e4e"
},
"outputs": [
{
"data": {
"text/plain": [
"'The ball bounces'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence = ' '.join([the, ball, bounces])\n",
"sentence"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 422,
"status": "ok",
"timestamp": 1533249348766,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "RjQAneqwyAaa",
"outputId": "3fc176f9-312c-426e-c654-596136fe9f22"
},
"outputs": [
{
"data": {
"text/plain": [
"'c-o-n-c-a-t-e-n-a-t-e'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'-'.join('concatenate')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 329,
"status": "ok",
"timestamp": 1533249385942,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "9N18obJRm96a",
"outputId": "aa1a42e1-bb22-4be9-ed64-58bc3c2f886a"
},
"outputs": [
{
"data": {
"text/plain": [
"['The', 'ball', 'bounces']"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence.split()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 358,
"status": "ok",
"timestamp": 1533249433493,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "dm2GMnEunAUo",
"outputId": "b36f6ba5-21e4-4f72-e8ff-969abe5be1e2"
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence.find('b')"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 419,
"status": "ok",
"timestamp": 1533249533245,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "fDAjIUMGnZzR",
"outputId": "9a1b071e-dcfe-4f29-d525-e3f5dfd3a056"
},
"outputs": [
{
"data": {
"text/plain": [
"'b'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence[4]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 518,
"status": "ok",
"timestamp": 1533249556654,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "vOOuHUemnc7W",
"outputId": "ba958895-c3af-46c2-c605-d425e08379cd"
},
"outputs": [
{
"data": {
"text/plain": [
"'ball'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence[4:8]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 414,
"status": "ok",
"timestamp": 1533249562005,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "Vpi2OGtQnGDd",
"outputId": "a0c1b6df-130b-4b5d-c2c7-7d022abb2208"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence.count('b')"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 305,
"status": "ok",
"timestamp": 1533249582077,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "FX9Z_W9unJy9",
"outputId": "e05c9024-e94e-4d16-a538-8e79c840a1c8"
},
"outputs": [
{
"data": {
"text/plain": [
"' The ball bounces '"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dirty_sentence = ' ' + sentence + ' '\n",
"dirty_sentence"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 455,
"status": "ok",
"timestamp": 1533249590592,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "NRWyTQ5_nTpN",
"outputId": "71f7add0-16df-4bc3-9bf3-d10ed7ef553c"
},
"outputs": [
{
"data": {
"text/plain": [
"'The ball bounces'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dirty_sentence.strip()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "yJ54OHZFcDNu"
},
"source": [
"## Iterables\n",
"\n",
"While iterables are *sometimes* interchangeable, it's important to note slight differences between them, as well as default behaviors.\n",
"\n",
"### Lists \n",
"\n",
"* Mutable\n",
"* Indexed with numbers\n",
"* Ordered\n",
"* Can contain multiple types, as well as redundant values\n",
"\n",
"### Tuples\n",
"\n",
"* Immutable\n",
"* Indexed with numbers\n",
"* Ordered\n",
"* Can contain multiple types, as well as redundant values\n",
"\n",
"### Dictionaries\n",
"\n",
"* Unordered\n",
"* `values` accessed by `keys`\n",
"* `keys` must be unique\n",
"* `values` are mutable, but `keys` are not\n",
"\n",
"### Sets\n",
"* Mutable\n",
"* Cannot be indexed\n",
"* Unordered\n",
"* All elements are unique\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "q6rwgV-lw1Qs"
},
"outputs": [],
"source": [
"reds_list = ['red', 'maroon', 'brick', 'pink']"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 292,
"status": "ok",
"timestamp": 1533232335869,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "UTZ9LUptxKIa",
"outputId": "3c995a88-bc06-48a0-9b91-ffc463e61ed5"
},
"outputs": [
{
"data": {
"text/plain": [
"'brick'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reds_list[2]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "cJnAEMFLxNDg"
},
"outputs": [],
"source": [
"reds_list[2] = 'burgandy'"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 86
},
"colab_type": "code",
"executionInfo": {
"elapsed": 388,
"status": "ok",
"timestamp": 1533232540178,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "eRsMdKX8xUbW",
"outputId": "e10f4bd4-ce18-41a2-aabf-574d7c90c890"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 red\n",
"1 maroon\n",
"2 burgandy\n",
"3 pink\n"
]
}
],
"source": [
"for i, red in enumerate(reds_list):\n",
" print(i, red)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "f5m15gFPxi6b"
},
"outputs": [],
"source": [
"blue_tuple = ('blue', 'cerulean', 'teal', 'navy')"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 330,
"status": "ok",
"timestamp": 1533232478330,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "e0Rixo4QxuKb",
"outputId": "52cc0ef2-0ea0-4e6d-c2db-764f9c490ae7"
},
"outputs": [
{
"data": {
"text/plain": [
"'teal'"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"blue_tuple[2]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 358,
"status": "ok",
"timestamp": 1533232517683,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "PuQ5sBcDxxlA",
"outputId": "e1a8bfb3-e488-481c-f04a-ef4f3beabea9"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tuples are immutable\n"
]
}
],
"source": [
"try:\n",
" blue_tuple[2] = 'sky'\n",
"except:\n",
" print('Tuples are immutable')"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 86
},
"colab_type": "code",
"executionInfo": {
"elapsed": 438,
"status": "ok",
"timestamp": 1533232550744,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "s0qqm-oXx7Ml",
"outputId": "a9ad046c-3608-4ae1-b6f8-12ff1c24088f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 blue\n",
"1 cerulean\n",
"4 teal\n",
"9 navy\n"
]
}
],
"source": [
"for i, blue in enumerate(blue_tuple):\n",
" print(i**2, blue)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 332,
"status": "ok",
"timestamp": 1533232628656,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "NVTb_E42yLgA",
"outputId": "5acb2cac-583b-458d-d494-9830e71b4e56"
},
"outputs": [
{
"data": {
"text/plain": [
"{'forest', 'green', 'lime', 'safety green'}"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"green_set = {'green', 'lime', 'forest', 'green', 'safety green'}\n",
"green_set"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 69
},
"colab_type": "code",
"executionInfo": {
"elapsed": 321,
"status": "ok",
"timestamp": 1533232722091,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "5Sd7FUhvyEbD",
"outputId": "e393c10b-0752-4673-d7d6-4231dc71687f"
},
"outputs": [
{
"data": {
"text/plain": [
"{'red': ['red', 'maroon', 'burgandy', 'pink'],\n",
" 'blue': ('blue', 'cerulean', 'teal', 'navy'),\n",
" 'green': {'forest', 'green', 'lime', 'safety green'}}"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"color_dict = {'red': reds_list, 'blue': blue_tuple, 'green': green_set}\n",
"color_dict"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 379,
"status": "ok",
"timestamp": 1533232768348,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "caUEDjXGyts5",
"outputId": "673dd8a3-68ef-4718-d8b1-99e675718349"
},
"outputs": [
{
"data": {
"text/plain": [
"{'red': 4, 'blue': 4, 'green': 4}"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"for key in color_dict:\n",
" color_dict[key] = len(color_dict[key])\n",
" \n",
"color_dict"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "tQ_6wypvuWgg"
},
"source": [
"## Booleans/Logical operators\n",
"\n",
"| Operator | Purpose |\n",
"| --- | --- |\n",
"| `==` | equality |\n",
"| `!=` | inequality |\n",
"| `<` | less than |\n",
"| `>` | greater than |\n",
"| `<=` | less than or equal |\n",
"| `>=` | greater than or equal |\n",
"| `and` | boolean and |\n",
"| `&` | bitwise and |\n",
"| `or` | boolean or |\n",
"| <code>&#124;</code> | bitwise or |\n",
"| `not` | boolean not |\n",
"| `~` | bitwise not |\n",
"| `is`| equality |\n",
"| `is not` | inequality |\n",
"| `in` | is in iterable |\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "LdWeOuX8qRWQ"
},
"source": [
"## Control flow & Iteration\n",
"\n",
"`if`/`elif`/`else`\n",
"* `if` checks to see if a condition is satisfied\n",
"* `else` will always run if the condition for a preceding `if` is not satisfied\n",
"* `elif` combines `else` and `if`: it evaluates if a condition is satisifed if a preceding `if` is not satisfied\n",
"\n",
"`for` loops\n",
"* Does something a number of times while looping through an iterable\n",
"* Often combined with a `range` call to specify how many times a thing should be done\n",
"* `enumerate` allows us to capture a count simtaneously as we iterate through our iterable\n",
"* We can use tuple unpacking to capture multiple variable on each pass through an iterable\n",
"\n",
"`while` loops\n",
"* Checks to see if a condition is satisfied\n",
"* Will continue to repeat contained code until the condition is satisfied\n",
"\n",
"`try`/`except`\n",
"* `try` contains code that we hope to run\n",
"* `except` will run if the try code throws an error\n",
"* `finally` will always run after either the `try` or `except` completes (used less often) "
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "0ShgmpGvsUv9"
},
"source": [
"## Defining functions\n",
"\n",
"We use the `def` keyword to begin a function definition.\n",
"\n",
"This is followed by the name of the function, and arguments, then we do something, and `return` it."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "lXxdHNJYbSis"
},
"outputs": [],
"source": [
"def sample_function(arg1, arg2='default'):\n",
" sum_of_args = arg1 + arg2\n",
" return sum_of_args, arg1, arg2"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "0St2zrzc7w1i"
},
"outputs": [],
"source": [
"sentence, word1, word2 = sample_function('thank ', 'you')"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 351,
"status": "ok",
"timestamp": 1533252127023,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "Etw-kNcf79_9",
"outputId": "66434a1b-8bce-49eb-adcb-ab67b75fe885"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"thank you\n"
]
}
],
"source": [
"print(sentence)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "svRdRaQs9jF7"
},
"source": [
"## Quick demo on multiple assignment"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "aCXl9vb48t_M"
},
"outputs": [],
"source": [
"a, b = 1, 2"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 389,
"status": "ok",
"timestamp": 1533252174352,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "H_jjzczb8x0J",
"outputId": "8d012102-d9a4-4d74-f8f4-7fb5793954de"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 1\n"
]
}
],
"source": [
"a, b = b, a\n",
"print(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 796,
"status": "ok",
"timestamp": 1533252315621,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "9rVLVTdM9BuC",
"outputId": "869e2d41-8173-4352-aaba-bfe3fb0796ae"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 1\n"
]
}
],
"source": [
"a = 1\n",
"b = 2\n",
"temp_a = a\n",
"a = b\n",
"b = temp_a\n",
"print(a, b)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "R5Sob8YJs5ZE"
},
"source": [
"## Lambda functions\n",
"\n",
"`lambda` functions allow us to define throwaway functions inline with our code. We can assign `lambda` functions to variable names. They will often be useful in list comprehensions or for mapping and filtering."
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "q9d2VCa2tHVx"
},
"outputs": [],
"source": [
"square_this = lambda x: x**2"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 350,
"status": "ok",
"timestamp": 1533252537670,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "O-cwITqftSpB",
"outputId": "787de6b5-0c40-479f-90b7-7ac7732d558a"
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square_this(2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "BlN_SYm1tWVP"
},
"source": [
"## List comprehensions\n",
"\n",
"Allow us to unpack `for` loops into an iterable in a single line of code, applying operations as we go through the interable. Make for tight, neat code."
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "At9FKt7mtVSg"
},
"outputs": [],
"source": [
"animals = ['bear', 'dog', 'cat', 'men']"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 326,
"status": "ok",
"timestamp": 1533252728593,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "hTvcrGkmt8D5",
"outputId": "93d5b00d-26a5-4db6-9654-12721b3486c0"
},
"outputs": [
{
"data": {
"text/plain": [
"['BEAR', 'DOG', 'CAT', 'MEN']"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"animals_upper = [animal.upper() for animal in animals]\n",
"animals_upper"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 341,
"status": "ok",
"timestamp": 1533252914325,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "iv--ZHE6_mRY",
"outputId": "8ae2c95f-1b96-484d-99f1-705ba7bb48c9"
},
"outputs": [
{
"data": {
"text/plain": [
"['b', 'e', 'a', 'r', 'd', 'o', 'g', 'c', 'a', 't', 'm', 'e', 'n']"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[letter for animal in animals for letter in animal]"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 420,
"status": "ok",
"timestamp": 1533253172609,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "WSZxyLxWAi-v",
"outputId": "388ca836-da47-4b0a-bc5d-6b0297d4147d"
},
"outputs": [
{
"data": {
"text/plain": [
"['b', 'e', 'a', 'r', 'd', 'o', 'g', 'c', 'a', 't', 'm', 'e', 'n']"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"letters = []\n",
"for animal in animals:\n",
" for letter in animal:\n",
" letters += letter\n",
" \n",
"letters"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 376,
"status": "ok",
"timestamp": 1533252740044,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "-BJO6YPduEhX",
"outputId": "f46d64e0-e4de-49dc-d0a5-252d169f1b24"
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"first_10_numbers_squared = [square_this(i) for i in range(10)]\n",
"first_10_numbers_squared"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 470,
"status": "ok",
"timestamp": 1533252770940,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "WibOFfWUzBWL",
"outputId": "6e82b43b-a490-4834-86d3-5e7d7b8cef8d"
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 4, 16, 36, 64]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square_evens = [x**2 for x in range(10) if x%2 == 0]\n",
"square_evens"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
},
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 583,
"status": "ok",
"timestamp": 1533232955971,
"user": {
"displayName": "Douglas Strodtman",
"photoUrl": "//lh3.googleusercontent.com/-CAdRA0A1uf0/AAAAAAAAAAI/AAAAAAAAAFk/4SrxglU1qhw/s50-c-k-no/photo.jpg",
"userId": "101538511209866748847"
},
"user_tz": 420
},
"id": "awm0GdVGzN6e",
"outputId": "83c1a8b4-7ca3-4443-976a-1c26d6fe784c"
},
"outputs": [
{
"data": {
"text/plain": [
"{1: 1, 3: 9, 5: 25, 7: 49, 9: 81}"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square_odds_dict = {x:x**2 for x in range(10) if x%2 != 0}\n",
"square_odds_dict"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "p1NcoBv11qIO"
},
"source": [
"1.5\n",
"- Define experiment, outcome, event, and sample space.\n",
"- Calculate the union and intersection of sets.\n",
"- Apply six probability rules.\n",
"- Import packages into Python.\n",
"- Solve probability problems using simulations."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "oJoJ7Rbt11cL"
},
"source": [
"**Experiment** : \n",
"- _A proceedure that can be repeated infinitely many times and has a well defined set of outcomes._\n",
"\t\t\tThey can be repeated or conducted in the same manor by anyone assuming said individual has the resources required.\n",
"\t\t\tA \"Well Defined set of outcomes\" indicates expected outcome of the experiment is know. There are random chance and special cases, but for the most part the scientist understands the general results or information that is supposed to come from said experiment.\n",
" \n",
" Example : Rolling a 6-sided die once. The experiment parameters are know and this can be conducted by anyone with a (fair) 6-sided die. The experimentor also understands that the outcome can be 1, 2, 3, 4, 5 or 6 and each of them is equally likely. \n",
"\n",
"**Outcome** : _The results of an experiment. In the coin flipping example this iseigher Heads or Tails. In the dice rolling example this is any of the numbers $1, 2, 3, 4, 5, 6$._\n",
" \n",
"\n",
"\n",
"\n",
"**Event** : Any collection of possible outcomes for an experiment. \n",
"\n",
" Example: If my experiment is to roll a die 3 times in a row some of the possible events are {1,1,5}, {3,5,2} or {1,6,1}\n",
"\n",
"\n",
"\n",
"**Sample Space** : The collection of ALL possible outcomes of an experiment. \n",
"\n",
"**Random Variable** : Functions that depend on the outcomes of experiments. For example we could roll two dice and add the results. The resultant single number, ranging from $2$ to $12$ would be a random variable. Typically random variables will be some, probably arithmetical, function applied to the outcome of the experiment."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "7uZ2i9UEl5Qa"
},
"source": [
"## Probability\n",
"\n",
"The (_frequentist_) probability of some event $A$ is given by:\n",
"\n",
"$$\n",
"P(A) = \\lim_{n \\rightarrow \\infty} \\frac{\\text{number of times A occurs}}{n}\n",
"$$\n",
"\n",
"We can estimate $P(A)$ by running some large number of trials and seeing how frequently $A$ occurs."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "-IOpPQ5vmNtf"
},
"source": [
"## Discrete vs. Continuous random variables\n",
"\n",
"### Discrete\n",
"* All outcomes of a random variable could be counted.\n",
"* The distribution of probabilities of each **specific** outcome is called the **probability mass function (pmf)**.\n",
"\n",
"### Continuous\n",
"* Have outcomes that are uncountable (or infinite).\n",
"* The probabilities of **ranges of values** are calculated as areas under the **probability density function (pdf)**."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "js2m-7oxobCB"
},
"source": [
"## PDF vs. CDF\n",
"\n",
"To calculate the **cumulative density function (CDF)**, start at the minimum possible outcome, then add the probability of each outcome. Thus, each point on a cumulative distribution function represents the probability that a random variable is less than or equal to that value.\n",
"\n",
"The difference in the height of the CDF between any two points on the x axis represents the probability that a random variable is in that range of values.\n",
"\n",
"$pdf$ is given as $f(x) = P(X = x)$\n",
"\n",
"$cdf$ is given as $F(x) = P(X \\leq x)$\n",
"\n",
"**For the purposes of this course, there are few distinctions that need to be remembered between PDF vs PMF or CDF vs CMF**. Density functions are for continuous variables, while mass functions are for discrete.\n",
"\n",
"**However**, at any exact point in a continuous distribution there is zero likelihood of observing that exact value. Hence the differing names: one represents the probability of a range of values, while the other represents the probability of specific outcomes."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "u2_ExE7FnYCb"
},
"source": [
"## Discrete Distributions\n",
"\n",
"### Discrete uniform distribution\n",
"Used when we have a **discrete set of outcomes** and **each outcome is equally likely**.\n",
"* all of the outcomes have the same probability.\n",
"* probability histogram is uniform (flat).\n",
" * Example:\n",
" * sample space $= \\{1, 2, \\dots, n\\}$\n",
" * pmf: $f(x) = 1/n$ for each $x$ in the sample space.\n",
" * cdf: $F(x) = x/n$ for each $x$ in the sample space.\n",
"\n",
"### Bernoulli distribution\n",
"- When your outcome is binary (i.e., two outcomes, say, `1 = success` and `0 = failure`)\n",
"- When there is a constant probability of success $p$.\n",
" - Example\n",
" - sample space = $\\{0, 1\\}$\n",
" - pmf: $f(0) = 1-p$ and $f(1) = p$ for some number $0\\le p \\le 1$. If you're thinking of coin flipping, the coin is fair then $p=\\frac{1}{2}$.\n",
" - cdf: \n",
" \\begin{align*}\n",
" F(x) &= 0 \\textrm{ if } x \\le 0,\\\\\n",
" F(x) &= 1-p \\textrm{ if } 0\\le x\\le 1, \\\\\n",
" F(x) &= 1 \\textrm{ if } x\\ge 1\n",
" \\end{align*}\n",
"### Binomial distribution\n",
"- when you have fixed $n$ independent Bernoulli trials.\n",
"\n",
"More explicitly:\n",
"\n",
"- when you have fixed $n$ trials,\n",
"- each trial is independent of one another,\n",
"- when you have a constant probability of success $p$, and\n",
"- when you have a binary outcome.\n",
" - Example\n",
" - sample space: length $n$ sequence of $\\{0,1\\}$ for the bernoulli trial, as in the example above \n",
" - pmf : $f(k) = {n \\choose k}p^k(1-p)^{n-k}$\n",
" - cdf: $F(k) = \\sum\\limits_{i=0}^k f(k) = \\sum\\limits_{i=0}^k {n \\choose k}p^k(1-p)^{n-k}$\n",
"\n",
"\n",
"### Poisson distribution\n",
"- when the number of successes is is a non-negative integer,\n",
"- when events occur independently,\n",
"- when the rate at which events occur is constant,\n",
"- when two events cannot occur at exactly the same instant, and\n",
"- the probability of an event occurring in an interval is proportional to the length of the interval. \n",
" - Example, with mean $\\lambda$\n",
" - sample space: all non-negative integers $\\{0, 1, 2, 3, \\dots\\}$ \n",
" - pmf : $f(k) = \\frac{\\lambda^ke^{-\\lambda}}{k!}$\n",
" - cdf: $F(k) =\\sum\\limits_{i=0}^k f(k) = \\sum\\limits_{i=0}^k \\frac{\\lambda^ke^{-\\lambda}}{k!}$\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "UtxxztcGmNHX"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "w_jc3pL2l629"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "jHp32VDmna4r"
},
"source": [
"## Continuous Distributions\n",
"\n",
"- Uniform Distribution on $[a,b]$\n",
" - pdf: $f(x) =\\frac{1}{b-a}$ for $a\\le x \\le b$\n",
" - cdf: $F(x) = \\frac{x-a}{b-a}$ for $a\\le x \\le b$\n",
" - Just like in the discrete case, this distribution is used when all outcomes are equally likely. \n",
" \n",
"- Exponential Distribution with mean $\\frac{1}{\\lambda}$\n",
" - pdf: $f(x) = \\lambda e^{-\\lambda x}$ for $x \\ge 0,$ otherwise $f(x) = 0$.\n",
" - cdf: $F(x) = 1-e^{-\\lambda x}$ for $x \\ge 0$, otherwise $F(x) = 0$.\n",
" - Often used in cases when larger numbers occur with lower probaility than smaller ones. This distribution has the memorylessness property -- this is why it is used to describe things like the amount of time waiting for an event to occur (like waiting for a bus).\n",
" \n",
"- Normal Distribution with mean $\\mu$ and standard deviation $\\sigma$\n",
" - pdf: $f(x)=\\frac{1}{\\sqrt{2\\pi\\sigma^2}}e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}$\n",
" - cdf: $F(x)= \\int_\\infty^x \\frac{1}{\\sqrt{2\\pi\\sigma^2}}e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}$\n",
" - The infamous 'bell curve'. Very common to see used for describing outcomes of measurements of numerical quantities sampled from real life populations including hegiht, weight, etc.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab_type": "code",
"id": "W9t8VYwkndVQ"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"default_view": {},
"name": "01_quiz_review.ipynb",
"provenance": [],
"version": "0.3.2",
"views": {}
},
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}