DEV Community

Cover image for Part One: Getting Started
Simon Chalder
Simon Chalder

Posted on • Edited on

Part One: Getting Started

"Programmer? I barely knew her!" - Unknown


What's it all about and why should I care?


Fair question.

At some point in your student or working career you may have been told that you should "learn to code". Well, what is learning to code? What is code? Is coding different to programming? Why should you even care?

Writing code, computer programming, or writing software are all terms which are often used interchangeably and essentially mean the same thing - giving a computer a set of written instructions. Just like instructing a human worker or volunteer, the amount of instruction and the complexity of those instructions can be determined by the task we need the worker, or in this case, computer to perform. Just like their human counterparts, computers will produce poor results if they are given poor instructions.

So why bother learning to code? Computers are tools which allow us to perform complex or repetitive tasks quickly and reliably. There are things we may wish to do such as collate survey results, examine geospacial data, or compare population numbers over time. In some cases we can get by using common software packages such as spreadsheets but perhaps our spreadsheet software does not allow us to display our data the correct way or does not work well with the type of data we are using. Maybe we need software to do something niche or bespoke and a readily available, off the shelf solution does not exist. In some situations, having the ability to write simple scripts (a small, usually simple application) or applications which will do a specific task for us can save us a great deal of time learning how to use a commercial software package.

Here are some examples of simple software applications which you could write:

  • Comb through large datasets to find what you need and then manipulate that data in any way you wish

  • Produce custom graphs and visualisations from your data to show others

  • Write a script which will automatically go out onto the internet and retrieve data from websites or databases. No browser required!

  • Use machine learning to make future predictions based on historical data

My intention with this series is not to take you from novice to professional developer but to give you a taste of the possibilities that writing your own code can bring. How far you wish or need to take it from there is completely up to you.


"Yes! Get on with it!" - Monty Python


Ok, enough waffle lets start coding. A quick online search for programming languages will bring back a frighteningly large list of results. Fortunately we are only going to focus on one - Python. Python, like all programming languages has its pros and cons. However, it is generally seen as a good language for beginners to get to grips with and as a bonus is used extensively in the data science and machine learning fields. Additionally, if you follow the principles of python, that is an object orientated approach (more on this later), then you will be able to pick up other programming languages easily. The way each language is written (the syntax) is slightly different from language to language but they mostly follow the same core concepts.

First things first we need to install Python on the device you will be using (preferably something with a keyboard). The Python website contains download links for Windows and Mac, simply select the relevant link, download and install. For Linux users, you may already have python installed with your distribution - type python -V into the terminal. If you see a version number you are good to go. If not follow instructions to install via your package manager. It's a good idea to bookmark the Python site as the documentation there can be very helpful when making your own projects.

Python should be installed and ready to go. To begin, open your device's application list or perform a search for 'IDLE' and open the application. If all is going well up to this point you should see something similar to the image below:

Python IDLE

IDLE stands for 'Interactive Development Learning Environment' and it is essentially a way for us to try out code without worrying about breaking anything. So lets type something and see what happens. In the text window of IDLE click to get a cursor and type your name, then press Enter.

Error Message

Yikes! Red text! Traceback what...? What's a NameError? Why is Python angry at me? Well, firstly Python isn't angry, in fact it is trying to help us. The error message we received is telling us that Python did not understand the code we typed. Specifically, the message tells us which bit of the code it did not understand (in this case line 1) and the kind of error (NameError) gives us information as to what the problem may be. Error messages are not to be feared and you will see them a lot, particularly in the beginning.


Variables - The Coder's Magic Box


Let's try typing our name again but this time type name = and then your name but make sure your name is in either single or double quotations. For example I would type name = "Simon" and press Enter.

Wait, nothing happened. Or did it? What we did here was to create a variable called 'name' and give it a value, in this case your name. A variable is a placeholder for a value and we use the '=' sign to assign values. We can imagine a variable to be a box. We give the box a name or label so that we can find it and refer to it when we need it. However, this is no ordinary box, it can stretch to hold anything from a single number or letter, to a sentence, a list of values or even an entire application spanning hundreds or even thousands or lines of code! In this instance we called our box 'name' and it contained a single word - your name.


A quick note on 'white space' and comments


White space is all of the spaces and gaps in your code and they are ignored by Python. We wrote name = "Simon" but we could equally have written name="Simon" and Python would read both of these the same. Code is written using certain conventions which make it more human readable and allow others to more easily see and understand our code.

Comments are denoted by a '#' symbol and anything written after this symbol for the remainder of the line is ignored by Python. Comments allow us to make notes in our code as a reminder to ourselves or to help explain what that part of the code is doing to anyone who reads it.

Our code:

# This is a comment - Python ignores this, it's just for us

name = "Simon" # Everything after the '#' is a comment
Enter fullscreen mode Exit fullscreen mode

What Python sees:

name="Simon"
Enter fullscreen mode Exit fullscreen mode

Variables are one of the key building blocks we use when writing code. A simplified way of looking at a computer program would be to say that we are 'doing things with variables to produce some kind of result'.

So, we created this name variable and it contains your name. So where did it go? Python looked at this variable and has stored it in memory. For as long as your IDLE application is open on your device this variable will exist. When the application is closed everything held in that part of the memory will be lost. We can of course store data more permanently but that is a subject for another day. How can we check to see if Python has stored our name variable? Go back to IDLE and simply type the name of the variable - name and press Enter. There is our name displayed on the screen! Python recognises the name of the variable and shows us the contents of the box.

What if we now wanted to give name a different value? Easy, we simply assign a new value with the '=' sign name = "new name" and press Enter. If we now type name and press Enter, we will see the new value has replaced the old value. Another way of showing the contents of our variables is to use print(). We will get into exactly what print() is later but for now just know that it will print to the screen (not your paper printer) whatever we place inside the brackets. For example:

IDLE


Putting it all together


We now know what a variable is and how to create them, but what can we do with them? Variables aren't too exciting on their own, but what we can do with them is much more interesting. In the next part of the series I will introduce you to some of the things we can put into variables other than words as well as some ways we can interact with them. Until then, keep playing with IDLE (you can't break anything!). Try using print() to display different variable names.

Want a challenge? See if you can learn how to display multiple variables on the same line. Bonus points for figuring out how to put a space between the names).

genus = "Pinus" # Declare these variables
species = "sylvestris"
print(#write your code here between the brackets)
# Output
Pinus sylvestris
Enter fullscreen mode Exit fullscreen mode

Conclusion - the first step on the journey


"The cool thing about being famous is travelling. I have always wanted to travel across seas, like to Canada and stuff." - Britney Spears

In this first part of this series of articles, I wanted to write about a very important aspect of learning to code - why do it? Nearly all learning endeavours are destined to fail if we are not motivated to learn. Coding like most things is very difficult to learn if you are not interested in the subject matter. My aim with this series is simply to open your eyes to the possibilities and hopefully spark your imagination as to what can be accomplished. To this end I encourage you to look around, search online for 'python projects' and see the types of projects people are making and then think how you could use similar projects for your own work or studies. There are many great teaching resources out there and you would do well to try a few to see which one works best for you. In the meantime, thank you for reading and I look forward to seeing you in part two.

Simon

Top comments (0)