{ "metadata": { }, "nbformat": 4, "nbformat_minor": 5, "cells": [ { "id": "metadata", "cell_type": "markdown", "source": "
Here we’ll learn some of the fundamentals of python and how to do basic maths in Python.
\n\n\nAgenda\nIn this tutorial, we will cover:
\n\n
\n- Python Fundamentals
\n\n
\n- Variables
\n
Any Python interpreter can be used as a calculator. We can do simple sums
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-1", "source": [ "3 + 5 - 4" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Multiplication
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-3", "source": [ "5 * 4" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Division
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-5", "source": [ "5 / 4" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Just like you’ve probably learned in maths courses, we can assign values to variables
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-7", "source": [ "x = 10\n", "y = 2.5\n", "z = 3" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">From now on, whenever we use x
or y
or z
, Python will substitute the value we assigned to it.\nYou’ll notice when we assign things to a variable, nothing is printed out. If we’ve assigned a value to a variable we can print it by doing:
You can print out multiple variables if you separate them with a comma ,
.
\n\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-11", "source": [ "x * (y + z)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">In Python, variable names:
\n\n
\n- can include letters, digits, and underscores
\n- cannot start with a digit
\n- are case sensitive
\nThis means that, for example:
\n\n
\n- \n
x
is a valid name- \n
weight0
is a valid variable name, whereas0weight
is not- \n
weight
andWeight
are different variables
What do you think the following will result in? The best way to find out is trying things for yourself. If it’s wrong, can you correct it?
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-13", "source": [ "xy" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Time to check what we’ve learned!
\n\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-15", "source": [ "# Test solutions here!\n", "# By the way, lines starting with a # are comment lines!\n", "# You can use that to take notes, and not affect how your code runes\n", "# People use it for documentation: explaining what the function does,\n", "# what a variable means, why they chose this or that algorithm." ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Exercise 0: Simple Equations\nGiven the equation:
\n\n\\[y = x * 921 + 534\\]\nWhat is the value of
\ny
whenx = 452
You can test out solutions in the box below this question.
\n\n👁 View solution
\n\n\nx = 452\ny = x * 921 + 534\n# Remember to print it out!\nprint(y)\n
416826
\n
A library is a collection of files (called modules) that contains functions for use by other programs. It may also contain data values (e.g., numerical constants) and other things. A library’s contents are supposed to be related, but there’s no way to enforce that. The Python standard library is an extensive suite of modules that comes with Python itself. Many additional libraries are available from PyPI (the Python Package Index).
\nA library is a collection of modules, but the terms are often used interchangeably, especially since many libraries only consist of a single module, so don’t worry if you mix them.
\nYou can use import
to load a library module into a program’s memory, then refer to things from the module as module_name.thing_name
. Python uses .
to mean “part of”. For example, using datetime
, one of the modules in the standard library:
\n\n\nFirst, you can use
\nhelp
to learn about the contents of a library module. it works just like help for a function.\nhelp(datetime)\n
You can import specific items from a library module to shorten programs. You can use
\nfrom ... import ...
to load only specific items from a library module. Then refer to them directly without library name as prefix.\nfrom datetime import datetime\n\ndatetime.now()\n
You can create an alias for a library module when importing it to shorten programs. Use
\nimport ... as ...
to give a library a short alias while importing it. Then refer to items in the library using that shortened name.\nfrom datetime import datetime as dt\n\ndt.now()\n
Let’s import the math module:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-19", "source": [ "import math" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">This imports the math module. If you ever don’t know what a function does, you can use the help()
command:
And you’ll see a list of the functions and properties available. Let’s try out one of those functions now:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-23", "source": [ "math.sqrt(9)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">When we import a module like import math
, we need to use that as a prefix. Imagine we had two different modules, math
and other_math
, and both have a sqrt
function. How would Python know which sqrt
function we wanted? So we use math.sqrt
to be explicit about which function we need.
You might also have done powers (e.g. 2 cubed, or \\(2^3\\)) in the past, too:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-25", "source": [ "math.pow(2, 3)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Above we computed 2 cubed, but how did we know what to write? We might have seen math.pow
in the help(math)
page above, but how did we know 2, 3
? The same way of course:
That would tell us that if we want 2 cubed, we need to write 2
and 3
. So let’s do some exercises now and practice some of our maths skills.
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-29", "source": [ "# Test solutions here!\n", "x =\n", "print(x)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Exercise 1: Basics\nPlease convert this function from an equation, into python code:
\n\n\\[x = 2^8\\]\n\n👁 View solution
\n\n\nx = math.pow(2, 8)\nprint(x)\n
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-31", "source": [ "# Test solutions here!" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Exercise 2: Averaging two values\nSee if you can find the average of these two values, using the math operations
\n\n
\n- 23484
\n- 12345
\nYou can find the average by summing, and dividing by 2:
\n\n👁 View solution
\n\n\n(23484 + 12345) / 2\n
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-33", "source": [ "# Test solutions here!" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Exercise 3: Round-trip\nPlease convert this function from an equation, into python code. Remember, you can assign values to variables, if you want to split this into multiple steps.
\n\n\\[x = \\sqrt{9^2}\\]\n\n👁 View solution
\n\n\ny = math.pow(9, 2)\nx = math.sqrt(y)\n
Or
\n\nx = math.sqrt(math.pow(9, 2))\n
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-35", "source": [ "# Test solutions here!" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Exercise 4: Pythagorean Theorem\nThe formula for a 90° triangle can be expressed as:
\n\n\\[a^2 + b^2 = c^2\\]\nOr, expressed in terms of c, \\(c = \\sqrt{a^2 + b^2}\\)
\nPlease convert this to python and find
\nc
when\na = 65\nb = 72\n
\n👁 View solution
\n\nc = 97
\n\nc = math.sqrt(math.pow(a, 2) + math.pow(b, 2))\n
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-37", "source": [ "# Test solutions here!\n", "root1 =\n", "root2 =\n", "print(root1)\n", "print(root2)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Exercise 5: Quadratic Roots\nWay back in algebra class, you might have been given a quadratic equation, something like:
\n\\(y = 2*x^2 + x - 1\\) and were told to find the roots of this function, using a complicated equation. So challenge time: reproduce this equation in Python:
\nGiven the following variables:
\na = 2\nb = 1\nc = -1
\nConvert the following formulas to Python, and find the answers
\n\\(\\dfrac{-b + \\sqrt{b^2 - 4ac}}{2a}\\) and \\(\\dfrac{-b - \\sqrt{b^2 - 4ac}}{2a}\\)
\nSince we’re checking your ability to write Python, not do math, it should give
\n-1
and0.5
to let you check your work. If you got those values, you got it right!Make sure you save each root as it’s own variable, and then print them out.
\n\n👁 View solution
\n\n\nroot1 = (-b + math.sqrt(math.pow(b, 2) - 4 * a * c))/(2 * a)\nroot2 = (-b - math.sqrt(math.pow(b, 2) - 4 * a * c))/(2 * a)\nprint(root1)\nprint(root2)\n