 In this video I'm going to show you how to use your Google account to use Python to do mathematics. You won't have to install anything. Did you know that Python, the computer language that is so famous, is great at doing all the math in your textbook? Python can do algebra, trigonometry, calculus, linear algebra and much much more. Now using Python can bring learning and doing mathematics to life. It allows you to experiment with the work in your textbook so that you develop a much richer understanding of the work. You can use Python to verify your results and you can use Python as a very sophisticated calculator to do all the tedious calculations for you. And all of this is free of charge. Today we're going to talk about setting up Python online using Google Drive. You don't have to install anything. As long as you have access to Google Drive, you are good to go. Fortunately, most of us have used Google Docs and Google Sheets and we already have a Google account. Just in case you don't, please go ahead and look online how to create a Gmail account so that you have access to Google Drive. So in broad and basic terms, how does Python work? Well, Python is a computer language. We write Python code which a computer can read and execute. Now Python is a multi-use language. We can use Python to create programs and apps and even games. We can use it to create websites and services. Python is arguably the number one language for data analysis and it is certainly the most used language in machine learning and artificial intelligence. So as a computer language, the code consists of English-looking words that we call keywords. They form the language. Now by putting the keywords in a certain order, using the syntax of the language, we get Python to do useful things. The reserved keywords are what constitutes the language. Just like the word Python is an English word. We have that word print, that's P-R-I-N-T print, is a reserved keyword in Python called a function. We can use the print function to print something to the screen. Now one of the reasons for the success of Python and other open source languages is that anyone can add new reserved keywords and syntax to the language, thereby extending the capabilities of the language. Now individuals or groups who add such functionality usually do it for a very specific reason, mostly to solve a new problem. Such a new extension is put together in a package. One such package is SMPI which is short for symbolic Python. By adding SMPI to Python, we get many new functions, classes and so on that extend the capabilities of Python to mathematics. That is to say symbolic mathematics. The SMPI package allows us to solve for X, do symbolic integration and differentiation and so much more. There's also the NumPy package which is short for numerical Python. The added functionality at NumPy allows us to do numerical mathematics. When we launch Python, we automatically get all the basic Python keywords and syntax. Now to use one of the many hundreds of additional packages requires us to first install those packages and then to import them into the active Python session. While installation only happens once and then by the way it isn't even required when using Google Drive since Google already downloaded many of the packages to their own system, when you install Python to your local system which I'll cover in a different video, you have to both download and install that package and every time you want to use that package you have to import it. So the installation only happens once but every time you run Python and you want to use that package you've got it imported. Well Google has done that all for you. It has downloaded and installed many many packages so we don't have to worry about that. All we're going to do is import it every time that we want to use it. Now this is very easy, very simple. The import keyword in Python is used to import packages. Now before we go any further let's set up our Google Drive so that we can use Python to do mathematics. So here we are in our web browser I am using Google Chrome and I've logged into my Google Drive so that will very simply be the url drive.google.com and as long as you have a gmail account you're going to have access to Google Drive. Now most of us have used at least Google Docs before we could say new and if we go down there's Google Docs and so let's open one. As mentioned very familiar to most of us so let's go back to our Google Drive and again new and if we go down to more we see we could do Google drawings, Google My Maps, Google Sites, Google Apps Script and Google Jamboard but we want something more. I'm going to click on connect more apps. In the search bar I'm going to start typing colab and we see that little one there, colabratory. Let's click on that. We see at the moment of this recording it's been downloaded it seems 55 million times so let's click on that and click on install and finally continue. Now it will ask you to sign into your Google account you can go ahead and do that and then finally make Google Colab the default app for files that it can open. I'm going to say okay and done. We can close up this Google Workspace marketplace and if we now go to new more we see Google Colab. So most of us is called a Google Colab instead of using its full name and this is it. This is our coding environment. To write Python code you need a development environment an IDE place where we type our Google Code. Google Colab is based on a project called Jupiter notebooks or Jupiter lab but this is provided by Google and everything runs on Google servers and it's absolutely free of charge. So I'm just going to close these release notes so that we have a bit more space and there we go. This is our coding environment. So let's explore a bit. First of all right at the top you can see at the moment this file which is very much like a Google doc it's called untitled 0.ipynb. That's a very old sort of extension it's stood for ipython notebooks just to show that this is actually all about Python. You can actually do other languages as well but we're going to concentrate on Python. So I can very quickly just highlight that section delete it and let's call this one demo. So this is now demo.ipynb and it's going to be saved directly to my Google Drive. So the first two things that I really want you to have a look at is these two buttons here it says code and it says text. Now this whole notebook is based on cells you're just going to create one cell after the other cell and it can either be a code cell in which we're going to type Python code or it can be a normal text cell and we see there's already one cell here available to us and that is going to be a normal coding cell but if I hover in the middle I can see code and text pop up and if I'm right below that code cell I can see code and text so anyway above and below I can insert either a new code cell or a new text cell. So let's go right to the top and I'm just going to click on text and that gives me a text cell and look that's open just above that default code cell that was already there and it says double click or enter to edit so let's double click on that and there you go I've got a cell in which I can write normal text so I'm just going to write this as a sentence there we go this is a sentence and I can now just click outside of that perhaps just in the next cell and lo and behold there we go this is a sentence as written as if this is a google doc I can just write normal normal English sentence so let me double click on it again which opens it up so that we can edit it so maybe I wanted to say sentence I'm going to highlight sentence and perhaps I want to click bold and now the sentence word is going to be in bold but look at that that's a little bit of markup language I see two star symbols in front of sentence and behind sentence and if you do that we're going to see that it is bold by the way look across here on the right hand side you can see a little preview of the text that you are typing so let's click out of the cell and now you can see that sentence is in bold so let's just go up and up again so right at the top so I'm not doing these sequentially so I'm going right at the top and I'm going to enter a new text cell let's double click on it so that we can edit it and you see these two little t's here it stands for toggle headings and that's going to determine how big the font is so that we can have a title a subtitle and a subsubtitle so let's click it once and you see it does some boilerplate text for us so there's a single pound symbol or hashtag symbol then a space and then it says new section if I get out of that cell you see there at very large that is the largest font that we can have so let's typically go back there and maybe we'll call this let's replace that with title so that might be the title let's go in between these two let's add another text cell there and this time I'm just going to do it manually so I'm going to do two hashtag symbols and a space and I'm going to call this subtitle by the way all I did was hold down shift and hit return or shift and enter and that gave me an execution of that cell and so you can see title is big and subtitle is a little bit smaller and then the normal this is a sentence that's just a normal paragraph so you can well imagine that this can be used just like a google doc or microsoft word you can just create a normal document as long as everything you do goes inside of a text cell and again you can hover between any of these text cells right in the middle and you can add either a code cell or a text cell so let's do the following I'm going to do another text cell and let's make it two hashtag symbols by the way you can go all the way to six hashtag symbols or pound symbols and that'll give you the smallest sub sub sub subtitle sort of it looks a bit odd when it gets that small anyway and I'm going to do package I'm going to write package import and I'm going to hold down shift and return or shift and enter or I can just click out anyway outside of this text cell and there we go so this section is going to be about package imports as it stands a kernel of python has been activated on the side of google that's where your python is going to run and you can see that happening right at the top there when you hit connect actually as soon as you'd connect that python runtime is going to start so let's click on connect and you see it is connecting at the moment the little buttons go green and there we go we see ram usage and disk usage a version of python is now running on google servers and I have access to it right here in my google colab notebook so let's just type some python code by the way I'm going to be right here and it's in a code cell so yeah I'm going to write python code so I'm just going to say two space plus two now you don't need the spaces but you know it looks a little bit neater when we do have spaces and see that little play button so when you are in a code cell you can hit the little play button you can also just hold down shift and hit return just as with a text cell so let's hit that little button it was very quick and we see the code was executed and we get the result of the python code right there two plus two is indeed four now this is not what we're here for though we want to extend the language and that's what we spoke about in the beginning so let's add a new code cell and the one package we spoke about is some pie and I also mentioned that there's a keyword called import so let's write some python code import space some pie with two y's because that stands for symbolic python and as soon as I execute this we will see the little button run we'll see a little check mark and we see it took about one second to do and some pie is now imported everything that's inside of the some pie package is now available in this python session so what google did behind the scenes on their own servers they downloaded some pie so it's on their systems but every time I want to use it I have to import it so if you had python on your own system you would have had to install some pie once but then every time you write some python code you would have to import it you don't have to install it again but you do have to import it now this was just one way to use import and if I want to use anything that's now inside of some pie I have to reference some pie so let's type some some pie code so I'm going to say some pie dot in it underscore printing and I'm going to do open parenthesis close parenthesis by the way there'll be some auto completion so as soon as I type the open parenthesis the close parenthesis was already put there for me now let's have a look at this anything in python that has a keyword directly followed by an open and closing parenthesis is called a function and a function takes some input which we actually put inside of those parentheses and the function does something with what you put inside the parentheses and gives you some output now some functions like the in it underscore printing doesn't actually need anything to be given to that function we don't actually pass and what we call an argument inside of those parentheses we can just execute this function what this function actually does it allows mathematical type setting to the screen so when I do some mathematics with python and the result gets printed out it actually looks like your textbook it looks like the mathematics in your textbook what happens behind the scenes is that it uses language called la tech which is method we use for mathematical type setting so I'm going to execute this code cell by holding down shift and hitting return or enter or remember you can just hit the play button now that function is executed but just have a look at it I could not just use the function in it underscore printing I had to say some pie dot in it underscore printing open close parentheses anything that I want to use that's inside of some pie I will have to say some pie dot and whatever that function or class or keyword might be that lives inside of some pie but does not part of the larger base python now there's another way for us to do import and that's to import from the package exactly that which we want now there's another way to do importing and that's to import exactly what you want so I'm going to import two things from some pie I'm going to say from some pie so now I've got a new keyword here instead of import I now have from so I'm going to say from some pie now I'm going to say import and I'm going to list exactly what I want to import from some pie and I want for instance the sign function which is this s i n and I want the symbol pie which is this p y so now I've imported something directly that I can use directly I don't have to say some pie dot sign or some pie dot pie so let's execute this line of code I'm going to hold down shift hit return or enter and there we go I can now type directly the sign of pie so sign as a function inside of some pie I'm extending what python can do by these functions that live inside of python but because I imported that function directly by using the from some pie import I can now just use it directly I don't have to say some pie dot sign I can just simply use sign same with pie I can just simply type the symbol pie I don't have to say some pie dot pie and so let's execute this and you see that the sign of pie is actually zero we see the result there if I did not do the from some pie import those specific functions and keywords I would have had to say some pie dot sign and I would have had to say some pie dot pie now because I've imported some pie this is also going to work and we see the result is also zero so those are two ways that we can do the import either just say import some pie but then I really have to say some pie dot some pie dot some pie dot any time I want to use something inside of that package or I could just do the import of the things that I would need so the sign function the symbol pie or the number pie the irrational number pie now let's look at another way to do this I'm going to say import some pie as so there's a new keyword as and it's usually something like sym I can use an abbreviation let's call the namespace abbreviation so let's hit shift and return shift and enter and now I can simply say something like sym dot let's do the cosine function which is this c os cosine and let's do pie now remember pie I can use directly because I've already imported it as such and let's hit shift and return shift and enter and I see that the cosine of pie is indeed negative one but I did not have to write out the whole word some pie so that I have to say some pie dot some pie dot now I can just say sym dot there's just a little bit shorter and you can use any abbreviation you want really don't use reserved python keywords though and do remember that there are some conventions that most of us stick to for instance numpy would just be import numpy as np that's just a common one and there's many other common ones for all these packages now some people like these abbreviations and some do not does it really take that much longer to write some pie instead of sym some would argue yes because yes it does take longer and others might argue that it's just a little bit lazy now the last way that I want to show you to import a package is dangerous I'm going to show it to you but I never use it I'm going to say from sympy import star and what that's going to do it's going to import everything from sympy just as we did here with import sympy but I don't have to use the sympy dot sympy dot or if I use the namespace abbreviation sym that I have to use sym dot sym dot sym dot I can just use everything that's inside of sympy without having to go to those lengths of typing the word dot sympy dot or sym dot now that might sound great until you get to the point where you import more than one package and both sets of people who develop those packages use the same function names one of the most famous problems would be if I were to say import the following package import math so I've imported math now that's a package it's actually part of python itself but you actually you have to import it to get access to it so I can say math dot sign of math dot pi that's a different set of code behind the scenes math dot sign and sympy dot sign they're both going to do this trigonometric function called the sine function but they do it in very different ways so let's do this math dot sign and math dot pi and now we get a numerical approximation of zero we actually see 1.2246 etc times 10 to the power negative 16 which is this round of error because that just represents zero it's a numerical approximation so the sine function inside of the math package is actually going to do numerical computation whereas the sine function inside of sympy is going to do symbolic calculations that's a big difference and really when we want to do symbolic mathematics we have to use the sympy package it is great for symbolic calculations but look at this now there's a sine function syn in the math package and there's an syn function inside of the sympy package so if I just said from sympy import star and from math import star and if I then use the sine function syn which one is going to use the one from the math package or the one from the sympy package and that can lead to some conflict and you'll have to know how to deal with those so it's best never to do from package import star rather use the whole word or rather use a namespace abbreviation or import directly those functions that you want to use and then be careful not to import the functions directly from two packages and those two functions have the exact same name from these two different packages so be very careful there so there we go we've seen how to set up our google drive so that we have access to google colab which as you can see is very much like a google doc it's just another app that runs and I can do normal typesetting normal english sentences but I can also write code and then remember by the way in between any of these code cells I can add another code cell or another text cell beautiful and we've also seen how to import a package in different ways now the ones that we are going to work with are sympy we'll also work with a math package and we'll also work with numpy the numerical python package but you've seen how to import these and how the different methods can be used for the import of what's inside of those packages remember that by importing packages we want to extend the functionality of python and we can use python to do our mathematics and that's what we're going to do