{ "metadata": { }, "nbformat": 4, "nbformat_minor": 5, "cells": [ { "id": "metadata", "cell_type": "markdown", "source": "
\n\n# Python - Math\n\nby [Helena Rasche](https://training.galaxyproject.org/hall-of-fame/hexylena/), [Donny Vrins](https://training.galaxyproject.org/hall-of-fame/dirowa/), [Bazante Sanders](https://training.galaxyproject.org/hall-of-fame/bazante1/)\n\nCC-BY licensed content from the [Galaxy Training Network](https://training.galaxyproject.org/)\n\n**Objectives**\n\n- How do I do math in Python?\n\n**Objectives**\n\n- Understand the fundamentals of object assignment and math in python and can write simple statements and execute calcualtions in order to be able to summarize the results of calculations and classify valid and invalid statements.\n- Translate some known math functions (e.g. euclidean distance, root algorithm) into python to transfer concepts from mathematics lessons directly into Python.\n\n**Time Estimation: 30M**\n
\n", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-0", "source": "

Here we’ll learn some of the fundamentals of python and how to do basic maths in Python.

\n
\n
Agenda
\n

In this tutorial, we will cover:

\n
    \n
  1. Python Fundamentals
      \n
    1. Variables
    2. \n
    \n
  2. \n
\n
\n

Python Fundamentals

\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": [ "> " ], "id": "" } } }, { "id": "cell-2", "source": "

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": [ "> " ], "id": "" } } }, { "id": "cell-4", "source": "

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": [ "> " ], "id": "" } } }, { "id": "cell-6", "source": "

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": [ "> " ], "id": "" } } }, { "id": "cell-8", "source": "

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:

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-9", "source": [ "print(x)\n", "print(y, z)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-10", "source": "

You can print out multiple variables if you separate them with a comma ,.

\n
\n
\n

In Python, variable names:

\n\n

This means that, for example:

\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": [ "> " ], "id": "" } } }, { "id": "cell-12", "source": "

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": [ "> " ], "id": "" } } }, { "id": "cell-14", "source": "

Time to check what we’ve learned!

\n
\n
Question: Exercise 0: Simple Equations
\n

Given the equation:

\n\n\\[y = x * 921 + 534\\]\n

What is the value of y when x = 452

\n

You can test out solutions in the box below this question.

\n
👁 View solution\n
\n
x = 452\ny = x * 921 + 534\n# Remember to print it out!\nprint(y)\n
\n

416826

\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": [ "> " ], "id": "" } } }, { "id": "cell-16", "source": "

Libraries

\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).

\n

Libraries and modules

\n

A 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.

\n

A program must import a library module before using it.

\n

You 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", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-17", "source": [ "import datetime\n", "\n", "# it is currently\n", "datetime.datetime.now()" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-18", "source": "
\n
\n

First, you can use help to learn about the contents of a library module. it works just like help for a function.

\n
help(datetime)\n
\n

You can import specific items from a library module to shorten programs. You can use from ... import ... to load only specific items from a library module. Then refer to them directly without library name as prefix.

\n
from datetime import datetime\n\ndatetime.now()\n
\n

You can create an alias for a library module when importing it to shorten programs. Use import ... as ... to give a library a short alias while importing it. Then refer to items in the library using that shortened name.

\n
from datetime import datetime as dt\n\ndt.now()\n
\n
\n

Math Module

\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": [ "> " ], "id": "" } } }, { "id": "cell-20", "source": "

This imports the math module. If you ever don’t know what a function does, you can use the help() command:

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-21", "source": [ "help(math)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-22", "source": "

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": [ "> " ], "id": "" } } }, { "id": "cell-24", "source": "
👁 View solution\n
\n

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.

\n
\n

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": [ "> " ], "id": "" } } }, { "id": "cell-26", "source": "

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:

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-27", "source": [ "help(math.pow)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-28", "source": "

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
Question: Exercise 1: Basics
\n

Please convert this function from an equation, into python code:

\n\n\\[x = 2^8\\]\n
👁 View solution\n
\n
x = math.pow(2, 8)\nprint(x)\n
\n
\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": [ "> " ], "id": "" } } }, { "id": "cell-30", "source": "
\n
Question: Exercise 2: Averaging two values
\n

See if you can find the average of these two values, using the math operations

\n\n

You can find the average by summing, and dividing by 2:

\n
👁 View solution\n
\n
(23484 + 12345) / 2\n
\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": [ "> " ], "id": "" } } }, { "id": "cell-32", "source": "
\n
Question: Exercise 3: Round-trip
\n

Please 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
👁 View solution\n
\n
y = math.pow(9, 2)\nx = math.sqrt(y)\n
\n

Or

\n
x = math.sqrt(math.pow(9, 2))\n
\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": [ "> " ], "id": "" } } }, { "id": "cell-34", "source": "
\n
Question: Exercise 4: Pythagorean Theorem
\n

The formula for a 90° triangle can be expressed as:

\n\n\\[a^2 + b^2 = c^2\\]\n

Or, expressed in terms of c, \\(c = \\sqrt{a^2 + b^2}\\)

\n

Please convert this to python and find c when

\n
a = 65\nb = 72\n
\n
👁 View solution\n
\n

c = 97

\n
c = math.sqrt(math.pow(a, 2) + math.pow(b, 2))\n
\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": [ "> " ], "id": "" } } }, { "id": "cell-36", "source": "
\n
Question: Exercise 5: Quadratic Roots
\n

Way 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:

\n

Given the following variables:

\n

a = 2\nb = 1\nc = -1

\n

Convert 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}\\)

\n

Since we’re checking your ability to write Python, not do math, it should give -1 and 0.5 to let you check your work. If you got those values, you got it right!

\n

Make sure you save each root as it’s own variable, and then print them out.

\n
👁 View solution\n
\n
root1 = (-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
\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": [ "> " ], "id": "" } } }, { "id": "cell-38", "source": "\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "cell_type": "markdown", "id": "final-ending-cell", "metadata": { "editable": false, "collapsed": false }, "source": [ "# Key Points\n\n", "- Converting mathematics equation to python is pretty easy!\n", "- In real life you'll occasionally need to do this, either converting from a formula you read in a paper, or a description of an algorithm, into code that you'll re-use.\n", "- Did you forget how a particular module or function works? Try `help()`\n", "\n# Congratulations on successfully completing this tutorial!\n\n", "Please [fill out the feedback on the GTN website](https://training.galaxyproject.org/training-material/topics/data-science/tutorials/python-math/tutorial.html#feedback) and check there for further resources!\n" ] } ] }