\n",
"Python for Critical Thinking \n",
"NOYCE Summer Workshop 2020 \n",
"Dr Matt Sunderland \n",
"2020-June-3\n",
"
\n",
"\n",
"# Jupyter Notebooks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A1)** Pressing **SHIFT+RETURN** runs a cell. \n",
"**(A2)** When you run a cell, Jupyter automatically displays the **last** output. \n",
"**(A3) Exercise.** Run the following cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# SHIFT+RETURN runs a cell. TRY IT\n",
"\n",
"1 + 2 + 3\n",
"100*5\n",
"50-3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A4)** Pressing **ESCAPE,r,y** clears the output of a cell. \n",
"**(A5)** If you want to display two things, you can **use a comma**, or use the `print` function, which we will cover in (A37) below. \n",
"**(A6) Exercise.** Run the following cell. Then clear the output."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# SHIFT+RETURN runs a cell. TRY IT\n",
"# ESCAPE,r,y clears the output. TRY IT\n",
"\n",
"25*3, 100-1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A7)** The toolbar's \"+\" button adds a cell. \n",
"**(A8)** The toolbar's scissors button deletes a cell. \n",
"**(A9) Exercise.** Add some cells below. Then delete some of them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A10)** You can change the mode of a cell from \"code\" to \"**markdown**\" by clicking the last option on the toolbar. \n",
"**(A11)** Run the markdown cell (press **SHIFT+RETURN**) when you're finished editing it. \n",
"**(A12)** To resume editing, **double click** the cell. \n",
"**(A13) Exercise.** Change the following cell to markdown mode, type something, run it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A14)** Markdown cells will typset math written in LaTeX if you put it between dollar signs:\n",
"\n",
"$x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python arithmetic `+`, `-`, `*`, `/`, `**`, `//`, `%`\n",
"\n",
"**(A15) Exercise.** What does each of the 7 operations below do? \n",
"**(A16) Exercise.** Do spaces matter, or is it just style?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"3 + 10*5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5**2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"27/10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"27//10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"27 % 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A17) Exercise.** Use `%` to show that 74 is an even number."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your answer here and type SHIFT+ENTER\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python `#` and `=`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A18)** Python will ignore the rest of a line after it sees `#`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This is a comment\n",
"1 + 1 # This is also a comment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A19)** Assign a value to a variable using the `=` symbol. \n",
"**(A20)** Variable names must start with a letter. \n",
"**(A21)** Jupyter does not automatically display anything when you assign a value. \n",
"You have to **call the variable again** to get it to display its value."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"b = 20\n",
"b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A22)** You can assign more than one variable at a time using `,`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x, y = 100, 500\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python strings and lists"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A23)** We have already learned two python types, `int` (integers) and `float` (decimals). \n",
"**(A24)** The `type` function returns the type of an object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(-10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(12.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A25)** Write code to determine the types of `10.0`, `10/4`, `'10'`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Type your answer below and press SHIFT+ENTER\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A26)** We can use `int` and `float` to as _functions_ to **change the type** of an object. We call this \"typecasting.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(12.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A27)** Another python type is string `str` . \n",
"Strings are enclosed in single quotes or double quotes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'abc'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"efg\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A28)** Yet another python type is `list` . \n",
"Lists are enclosed in square brackets. \n",
"Lists can contain objects of different types."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"['a','b',1,2,3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A29)** We can `sum` lists containing only integers and floats"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = [2,3,5]\n",
"sum(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A30)** We can call **an entry** of an `list` or a character of a `str` using square brackets. We sometimes call this indexing or subscripting.\n",
"**(A31) Warning.** Python indexes start at **ZERO**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 'word'\n",
"x[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = [2,3,5,7]\n",
"x[1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A32)** We can also index from the back using negative numbers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 'word'\n",
"x[-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Slicing strings and lists using `:`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A32)** We can \"slice\" a `str` or `list` using `[a:b]`, which will return the elements from #a up to but ***not*** including #b. \n",
"**(A33)** You can also leave out the `a` or `b` or both in the `[a:b]`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = [2,3,5,7,11,13,17]\n",
"x[1:3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x[1:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x[:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A34)** Lastly, you can include a \"stride\" `c` as in `[a:b:c]` . \n",
"To keep your code readable, we usually only use a stride by itself."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 'abcdefg'\n",
"x[::2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A35) Exercise.** Given a list or string `x`, how can we reverse the order using a \"stride\"?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 'star'\n",
"\n",
"# Type your answer below and press SHIFT+ENTER\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python `print`, `input`, `'''`, `f'`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A37)** To **\"print out\"** outputs whenever/wherever you want, use `print`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 'These are prime numbers:'\n",
"y = [2,3,5]\n",
"print(x)\n",
"print()\n",
"print(x,y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A38)** Inside a string, `\\n` means **linebreak**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('Here is a line\\nbreak')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A39)** Put a triple of quotes `'''` around your string if you want to include **multiple lines in your string**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = '''This\n",
"is\n",
" a\n",
" multiline\n",
"string'''\n",
"\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A40)** Putting an `f` immediately before the `'` when making a string creates a so-called **f-string**.\n",
"Inside an f-string, you can put code inside curley braces."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'The sqrt of 2 is {2**0.5}.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A41)** Use `input` to **ask the user to input** some text. We can then use that input in our code.\n",
"\n",
"_Note: that use inputs have type `str`; you have to add a line of code to typecast the input to say, `float`, if you want to do arithmetic on the input. See (A26)._"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = input('What is your name? ')\n",
"\n",
"print(f'''\n",
"\n",
"Pleased to meet you, {x}!\n",
"\n",
"''')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A42) Exercise.** Ask the user for a number, then print out a full sentence saying what the cube of that number is.\n",
"\n",
"_Hint: You will need to typecast the user input to `int` (or `float`). See (A26)._"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Type your answer below and press SHIFT+ENTER\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python `for` and `range`\n",
"**(A43) Exercise.** Run the following examples and make sure you understand the output."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in [2,3,5,7]:\n",
" print(i**2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(7,10):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(5):\n",
" print(i**3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sum(range(4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python `if` statements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A44)** The so-called **boolean operators** include `==`, `!=`, `>`, `>=`, `<`, `<=`, `in`, `not in`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"10 == 20"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"10 != 20"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5 in [2,3,5,7]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A45) Exercise.** What does each boolean operator `==`, `!=`, `<=`, `in` measure? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A46) Example.** An `if`, `elif`, ..., `elif`, `else` statement"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 11\n",
"\n",
"if x < 0:\n",
" print(f'{x} is negative')\n",
"elif x%2 == 0:\n",
" print(f'{x} is positive and even')\n",
"else:\n",
" print(f'{x} is positive and odd')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A47) Exercise.** Ask the user for a number and print out whether the number is positive, negative, or zero."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Type your answer below and press SHIFT+ENTER\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Making python functions\n",
"**(A48) Example.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return x**2\n",
"\n",
"f(7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Classwork assignment: Taxicab numbers\n",
"\n",
"**(A49) Assignment.** Write a function that will find all the taxicab numbers up to n.\n",
"\n",
"* See (A48) for making functions using `def`\n",
"* See (A43) for `for` and `range`\n",
"* See (A46) for `if` statements\n",
"* See (A44) for boolean tests such as `==` and `in`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Type your answer below and press SHIFT+ENTER\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Demonstration: Partition Function\n",
"\n",
"**(A50) Exercise.** Explain what happens when we execute `p(8)`. \n",
"\n",
"_Notice that the function **calls itself** in the line `for b in p(n-a):`. We call such functions \"recursive.\"_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def p(n):\n",
" ans = [[n]]\n",
" for a in range(1,n):\n",
" for partition in p(n - a):\n",
" if partition[-1] >= a:\n",
" ans = ans + [partition + [a]]\n",
" return ans\n",
"\n",
"\n",
"p(8)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Double click** here to see the answer.\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Optional: Python list comprehension\n",
"\n",
"**(A51) Optional: Example.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[a**2 for a in range(10)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A52) Optional: Exercise.** Why does the following code output `14`? What is the following code doing?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sum([a**2 for a in range(4)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Downloading **Anaconda** (free, open-source)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(A53) Homework.** Download Anaconda to your own computer from www.anaconda.com/products/individual.\n",
"After installing, open the program **Anaconda Navigator**, find JupyterLab, and click \"Launch\" to open a new Jupyter notebook."
]
}
],
"metadata": {
"author": "Dr Matt Sunderland",
"date": "2020-June-3",
"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.7.4"
},
"latex_metadata": {
"author": "Dr Matt Sunderland",
"date": "2020-June-3",
"title": "NOYCE Workshop 2020: Python for Critical Thinking"
},
"title": "NOYCE Workshop 2020: Python for Critical Thinking"
},
"nbformat": 4,
"nbformat_minor": 4
}