KISS Python, A Keep It Simple Silly Primer Without Jumping Off A Cliff – Part 1

If you are on the fence about whether or not to learn Python this no frills primer walk you through the basics of learning to code in Python. This has been drafted as there a lot of articles on learning to code with Python, most with a bloat of “course” material that can be daunting – especially if you know nothing about programing. While it’s great to have lots of detail, when you are just wanting to find out what it entails knowing or to have a “sampler” of the syntax used in Python – this article is for you. After all it’s better to learn to swim before investing your time and money by blindly jumping off a cliff into the programming world.

The objective of this study is to expose you to the principles of coding in Python by example, and giving some meaning/usefulness to the code as we develop it. In the end, programming is used to take some input and generate some meaningful output.

Getting Started – You need a code editor

Let’s not waste any time. You will need a Python editor to put your code in. You can install an editor on your local PC (PyCharm or Visual Studio Editor – both available with free versions) or an online editor (Google’s Colab is free and accessible with a GMail account). Once you have access to your editor let’s jump straight into some code examples you can modify and make your own. Once you have the code editor you are going to use, you can copy an paste the code below (but there is something to be said for muscle memory – including the gelatinous mass overhead). The examples used in this article are used with Colab. If you have a GMail account you are logged into it is as easy as opening Chrome and going to colab.research.google.com, select “File” from the tool bar in the upper left and then “New Notebook”.

The Print Statement

Printing “Hello world!” is pretty much a standard. The value of the “print” command is often overlooked being fundamental. Compared to some other programing languages Python’s “print” saves some keystrokes, which is greatly appreciated when it comes to 100’s of lines of code. In its most basic form it is as simple as this:

#Print
print(“Hello world!”)

In this example “Hello World!” is our input to the “print” statement, the output results in printing our input – “Hello World!”. This is the first of many inputs and outputs we will explore.

Now print has some additional capabilities that can be accessed by using arguments. Arguments act like switches to enable certain functionality (we’ll casually incorporate arguments into your coding along our coding journey). Two useful arguments with the print statement are “sep” and “end” parameters. The “sep” argument gives the programmer the ability to put a separation character (an empty space, a comma, dash, etc) between variables. The utility of these and other arguments will be come apparent as we develop our coding abilities. An example comparing three print statements follows:

#Print
print(“Hello world!”) #prints “Hello world!”
print(“Hello”, “world!”) #prints “Helloworld!” <– notice there is no space in the output of this line
print(“Hello”, “world!”, sep=” “) #prints “Hello world!” <– the space is added with the argument sep=” “

While the output is the same, the output is different in how it is processed. The last line of code includes the sep=”-” argument which put a dash (-) between “Hello” and “world”. While simplistic, this functionality will become useful when it comes to formatting our output in future lines of code.

Using Variables

A variable in Python is similar to a variable in mathematics. Basically a variable stores information for use later. It is usually a symbol like a, b, c … x, y, z and then assigned a value. As in a mathematical equation it acts as a placeholder for a quantity that may change, or as a mathematical object. This gives us a short hand method of defining a specific number, vector, a set of numbers or characters, or a function. An example might be one of the following algebraic expressions where we solve for “c” using assigned values for “a” and “b”.

c = a + b
This is a simple addition equation where we are adding the pre-assigned values of a and b and solving for “c”. If we define a =1, b=2 and plug these values into our equation we get 3. If we change the value of a to a=3 and plug the new value of a into the equation we have 3 + 2 = 5

c = a * b
In our new equation, substituting a =1, b=2 and plug these values into our equation we get 2.

c = a / b
Likewise for our division equation with a =1, b=2 and plug these values into our equation we get 0.5.

The value of being able to use variables is that we can create a set of equations where the variables can be used repeatedly in different equations as above. To get started in Colab, add two code blocks by clicking on “+ Code” located directly under “File” in the command line. Paste each block in separately to be come familiar with using code blocks. Paste the following directly into Colab or your code editor to demonstrate the use of variables:

#Variable assignments
a = 1
b = 2

#Equations
c = a + b
print(c)

c = a * b
print(c)

c = a / b
print(c)

Sample code pasted into Google’s Colab and resulting output.

Note, variables are case sensitive. An additional important thing to remember is that when using variables the data types must match. The data types available in Python are numbers, strings, lists, dictionary, tuple, etc. These data types will be explored as we develop our programing abilities. Let’s format our #Equations block output to make it more legible using our previous print statement lesson:

#Equations
print(“a = “, a)
print(“b = “, b)

c = a + b
print(“a + b = “, c)

c = a * b
print(“a * b = “, c)

c = a / b
print(“a / b = “, c)

Useful Example

The previous code was very simplistic, so let’s do a quick project to demonstrate the usefulness of Python by making a temperature converter – we’ll convert Fahrenheit to Celsius. If you had to convert a table of temperatures this could prove useful (although we’ll improve on this as we develop our code). To convert Fahrenheit to Celsius you subtract 32 from the Fahrenheit and multiply the product by 0.5556 (or 5/9). For this we only need one variable, our temperature in Fahrenheit (F), and our conversion equation (C). Our code would look like:

#Fahrenheit to Celsius Calculator
F = 212C = (F-32) * (5/9)
print(F, “degrees Fahrenheit is equal to”, C, “Celsius”, sep=” “) 

Part 1 Wrap-up

This primer will make the code ever increasingly complex as we go. The gradual process will help you develop your programing skills quickly. This exercise should help you in determining your interest in being a programmer and aid you should you take a more complex course. In part 2 we’ll take on Functions, using global and local variables.

Stay tuned, more to come!

Main imagine attributes:
Cliff Jumping is dangerous” by Mark Morgan Trinidad B is marked with CC BY 2.0.
Preparing for flight” by Daniel Flower is marked with CC BY 2.0.

Leave a Reply

Your email address will not be published.