How To – Learn (some) Python with Google Colab: Awakening The Coder In You

Why Python? / Why Google Colab?

“Googling” these questions you’ll get hundreds, if not thousands, of results. To save you some time in determining if Python is right for you and worth your time, I’ll share my perspective – without selling you anything or collecting your personal information. Python as a programing language is relatively easy to learn, has applicability to problems, and is a skill set desired by employers – especially if you are interested in the field of data science. These three attributes alone are worth your time in knowing a little python. I should mention that I hate snakes. I am also very aware it is easy to bite off more than a person can chew when it comes to new projects. As such you would think I would look to avoid Thanksgiving meal time and all things named after a snake. Like with a good diet and some fasting prior to a huge feast, taking on the task of learning a programing language is better done with some preparation. Python and Colab is a combination that can tame the hurtle of programing with some preparation and fundamental skills to build upon. The Google Colab and Python combination is one such beast that can be tamed and managed with tangible outcomes before “taking on more than you can chew”.

Google Colab can provide you with Python programing experience without the need to install software on your computer, and being Google, there are tons of resources for you to reference in your Python programing journey. Colab also has an interface that is easy to learn and Google will scale the number of GPUs necessary for processing. These last two attributes are probably the greatest under-rated assets Colab has to offer. Once you’ve mastered Colab’s interface you’ll probably want to graduate to a more intricate compiler, which in turn will have a more intricate interface.

Where To Start

If you already have a good account you are ready to go. If not then set up a free Gmail account, which will give you access to a Gdrive for saving your work in and access to Colab.

Once you have your Gmail set up, simply login into your email and then open up an additional tab for Colab. In the address bar go to the URL: https://colab.research.google.com/

Google is a little zealous with their intro page as it goes from “Getting Started” to “Data science” without really introducing you to a simple programing construct using Colab. As with all programing languages there is that initial “Hello world” statement we all learn. Google does have a solution for this with an introduction to Colab, Python, with the use a simple print statement. If you are not familiar with coding it is “worth your while” to run through the Google tutorial as it introduces you to functions, conditionals and iteration – all good concepts to know as you begin your Python journey.

Google has made the programing interface simple. To kick things off, simply go to “File” and select “New notebook” from the menu bar at the top of the webpage.

You will be presented with the coding interface. It doesn’t hurt to mouse over (or even click) all the options presented. I have found the “Find and replace” function (the magnifier) on the left hand side to be extremely useful.

Printing Text: “Hello world!”

To get us started with our “Hello world”, let’s name the first project. In the upper left hand corner of the webpage you’ll see the Colab logo to the right of that will be the Google Drive emblem; simply click on the file name “Untitled.ipynb” to change it (leave the “.ipynb”) to “Hello_world.ipynb”.

Now let’s use the code block to write our routines (it is the empty space to the right of the “play” button). In the code block simply type the following: print (“Hello world!”) and then press the play switch.

Demonstration of the print function using quotes to delineate characters.
Pressing the code block “play” switch runs the code and produces the output “Hello world!” exactly as it appears between the quotes.

Performing Math Calculations: 1 + 1 and more

The real power of programing begins with the manipulation of data. Let’s start with some simple math calculations as a demonstration. What could be easier than “1 + 1”. Again let’s write a simple equation using the answer to “1 + 1 ” equal to “x”, and we’ll want to see the answer so we’ll add a second line of code to print the value of “x”. Start by adding an empty block for the new code by pressing the “+ Code” link in the upper left of the browser, just under the main menu header – directly under the “File” option. In the new empty code block type the equation “x = 1 + 1” (without the quotes). Hit return to start a new line. Add the print statement “print(x)”. You can then press the play switch to see the output. That is all there is to it.

You can change the numbers and the math operator (the “+” sign) to any thing you want to perform different math calculations. Hierarchy of math functions apply in order of precedence, the use of braces is an accepted practice. An example would be 2 times the quantity of 4 plus 6 raised to the 2nd power. The star (or splat) is used as a multiplication symbol, and double star as the symbol for exponents; the equation would look like: x = 2 * (4 + 6)**2

You’ll find additional information and sources on math operators and math functions linked. Combining arithmetic operators with comparison, assignment, bitwise, logical, membership and identity operators adds even more tools to your learning tool box. We’ll incorporate some of these in future coding articles.

The “Coder” in you awakens …

With the basic math operations out of the way it will prove beneficial to consider manipulating numbers in various combinations. Let’s start with a sample problem; given a circle of radius “r = 2” and a rectangle with dimensions “x = 2” and “y = 3”, lets answer the questions – 1) which has the greatest area, and 2) which has the longest parameter?

To simplify this in the event we want to use the dimensions in different calculations, (or different dimensions for additional calculations) we don’t want to just put in the values for each dimension and pi, rather let’s assign a variable to each one. You already know this shorthand as algebra (and you didn’t think you’d ever use those 8th grade math skills again). Let’s make the following assignments:

  • r = 1.5
  • x = 2
  • y = 3
  • pi = 3.141592

We can also define our equations in such a way that if we change the value of one of the dimensions we don’t have to re-write the equation each time. Here we define generalized equations for the area and parameters for both a circle and rectangle:

  • area_circle = pi * r**2
  • circumference_circle = 2 * pi * r
  • area_rectangle = x * y
  • parameter_rectangle = (2 * x) + (2 * y)

Note, the use of “area_circle”, “circumference_circle”, “area_rectangle”, “parameter_rectangle” is arbitary – you could have just as easily used “areaC”, “cirC”, “areaR”, “paraR” or any such notation just so long as you know which variable is what in the bigger scheme of things (you do need to be mindful with the use of capital letters – punctuation matters when coding). Python coding affords the use of variable assignments to whatever you wish so long as it is not a “reserved” command.

Naturally we will need to see these values at some point, so let’s add some print statements as we did in the previous “print(x)”, except this time we’ll use multiple print statements and simply substitute the “x” for assignments for each dimension ( “area_circle”, “circumference_circle”, “area_rectangle”, “parameter_rectangle”).

  • print (area_circle)
  • print (area_rectangle)
  • print (circumference_circle)
  • print (parameter_rectangle)

Once you have added the print statements you can test your code. Your code should look something similar to:

Dressing up the output with labels

While the output is correct, it would be nice to make it more complete with some labels so we know which value goes with what dimension. Let’s build upon our print statement to include some labels with the output values. To do this all we have to do is add a label inside the parenthesis and separate it from our output by a comma.

  • print (“Area of the circle: “, area_circle)
  • print (“Area of the rectangle: “, area_rectangle)
  • print(“”)
  • print (“Circumference: “, circumference_circle)
  • print (“Parameter of the rectangle: “,parameter_rectangle)

An extra print was added with nothing inside the quotes to separate the area and parameter calculations. Run the code to see your output:

Decisions, decisions …

We’ll wrap up this initial programing lesson with some decision making. Much of the value in programing is not just in calculating a result, but returning “just” the information we need. Our original problem was to determine which of these two shapes had the largest area and the greater parameter. A little decision making in comparing the output values for the given dimensions we give us out final answer(s). Here we’ll introduce the “if” statement and the use of comparison operators. With the if statement we can compare two conditions, in this case we’ll use the “greater than” comparison operator to compare the two calculated areas and parameters individually. The format of the if statement is:

if <condition>:
code-A
else:
code-B

Be sure to “indent” the code as shown in the screen capture below, you can use the “tab” key or space bar to create you “indent”. Here we’ll merely compare the final dimension variables:

if area_circle > area_rectangle:  
print (“Area of the circle is greatest: “, area_circle)
else:  
print (“Area of the rectangle is greatest: “, area_rectangle)

and

if circumference_circle > parameter_rectangle:  
print (“Circumference: “, circumference_circle)
else:  
print (“Parameter of the rectangle: “,parameter_rectangle)

Your modified code should look something similar to the following:

So now, your code not only does the calculation, it also identifies which of the dimensions is greatest. As a beginning project this should provide you some with some stepping stones for analyzing additional data and the use of more complex calculations and decision making. The utility of knowing a little python can go a long way. You can compare your “Hello_ world.ipynb” to the one Google has here: Google Hello_world.ipynb If you have made it to the end of this article you can readily see you have far exceeded the expectations of Google’s own demo and can now move on to more expanded coding.

Conclusion(s)

Aside from meeting the desires of the world around us, python can also serve our personal needs. Learning a programing language has the ability improve the productivity of our lives if we have number crunching, data collection, or other programing aspirations. To keep this on a personal level, I’m not advocating that a stay at home mom has a need to specifically learn python, but it could prove useful if say you wanted collect some data, calculate some information, compare the data and even make some decisions based off of historical outcomes (aka machine learning).

Some anticipated future articles will include website scraping to collect some data, and use the aforementioned skills to build upon our python skill set. Our goal is to make some useful examples that may be applicable to your data skills journey, including some code concerning machine learning (ML). Much of this will be based on stock market data, but we will also consider other data sources as well – stay tuned and subscribe!

Paying things forward *almost* for free with Outschool

While I teach on Outschool, you do not have to redeem this coupon for one of my classes. If you have a grade school student and are looking for a way to provide supplemental, or primary, education Outschool is one avenue.

Current Outschool students get to participate in Outschool’s “Give $20, Get $20referral program. If you sign up for an Outschool class through this link you’ll get $20 towards your first class. Please note: this particular (aforementioned) link I am sharing is not mine, but is a referral for a family with two students of need – please pay it forward. As an educator and a person growing up with similar challenges in Appalachia, I feel and have an obligation to help others less fortunate; as such I offer free and reduced classes upon request (no request is too small for consideration). This coupon is but one way to pay for the path of education during such difficult times that the current economic and social times has obligated us with.

Paying things forward one on one

If you need individualized instruction on introductory Python coding please contact me via the leave a message form below. I am currently, as of this article, available for one on one educational sessions. Thank you for your consideration.

Paying things forward while investing in your future

If you like this article and want to pay it forward consider joining Robinhood

You are the new Wall Street. Earn 5 stocks on us.For every friend you successfully refer, you’ll now earn five gift stocks! Offer ends on August 16, 2021.

Image attribution: Robinhood

Five total gift stocksYou used to get just one gift stock for every friend you refer. Now, you’ll receive 5 total gift stocks for every referral once your friend successfully signs up and links their bank account. Your friend will also get one gift stock, too! Certain limitations apply. Read terms and conditions.Pay it forwardHelp your friends start their Robinhood journey with a referral. Just make sure their account gets approved and they link their bank account in order to be eligible!Unlimited invitesYou can invite as many friends as you want, and can receive up to $500 in gift stocks each year.

Robinhood

Join Robinhood with my link and we’ll both get free stock  https://join.robinhood.com/ralphp79

Pay it forward AND learn to trade

Step up to a professional trading platform. An excellent platform to learn about trading AND to trade on is TastyTrade/TastyWorks. Their focus is on options trading, they have plenty of tutorials and an awesome LIVE program every day of the week and are available for replay. Traveling, no problem – with their mobile app you can watch/listen to their live program as well as trade straight from your phone or mobile device. TastyTrade offers one of the best execution times of any broker and an actual LIVE help desk you can phone for advice in using their platform.

What do I get for your referral? … a hotdog.

https://tastyworks.com/referral/?_sp=a0898123-fb50-4cb6-97e8-50abc35ecf75.1628091813310&utm_source=tastyworks&utm_medium=homepage_banner&utm_campaign=referral_2021https://start.tastyworks.com/#/login?referralCode=4TKJBESZQB

Attribution for the main article image: “Albino Burmese python with tongue out” by Tambako the Jaguar is licensed under Creative Commons.

Leave a Reply

Your email address will not be published.