Introduction to Computer Programs
As an introduction to computer programming, let's draw a parallel to giving person-to-person instructions. Imagine you want to verbally deliver a step-by-step recipe well enough that a listener could make bagels. What will you say? (Knowing a bagel recipe is not required; this is simply a thought experiment!)
Bagels start with making the dough. You would need to specify the amounts of ingredients needed, and the steps to make the dough. The next part would be to shape the bagels and boil them. At the last step, bagels need to be baked.
With the appropriate level of detail, you can talk someone through the steps to create a finished product.
Computers work similarly. They can follow specific instructions, but without giving them complete, detailed and accurate set of instructions they cannot solve any problems.
What you did here with the bagel recipe involved 2 parts: 1. Pointing out the required ingredients 2. Giving a step-by-step set of instructions for turning the ingredients into bagels. This is almost the same with computers. When teaching computers how to do things, the ingredient list is called the input data, and the step-by-step set of instructions is called an algorithm.
When describing the bagel recipe, you used the English language to do so. For example, you would say: "knead the dough." That instruction is using the English language to communicate the intended instruction. When working with computers, you also need some kind of language for communicating the instruction. These are called programming languages. Since it is not easy to verbally communicate with computers, we write down the instructions using program language. A set of instruction written in a specific programming language that does a particular job is called a computer program.
Humans have many different languages for communication; similarly, there are different programming languages. For example, Java, Python, C, C++, and Fortran are all different programming languages used for different domain of problems. In this course, we are going to learn the basics of writing code in Python. Python is very similar to English but it has a much more strict structure. We will learn to write simple programs to solve some simple problems.
In order to be able to program, we need an environment for writing down the program and executing it. In this course, we are going to use Google Colab. Google Colab lets anyone to write down their code and run it, without installing anything on their computer. You can use it in your browser (simply open a new tab).
You should use Google Colab while signed in with your Google account. If you already do not have a Google account, please make on using this link. If you already have a Google account, make sure you are signed in with your intended account when using Google Colab. This way you can make sure all your files will can be stored on your space and you can access them later!
Now, let's get familiar with Google Colab's environment. Please open this link to access Colab and watch this video discussing Colab basics.
Feel free to make your own new notebook. Try adding text and code blocks and see how it works. Any notebook that you make on Google Colab will be stored on your own personal storage space, called Google Drive, that Google provides for free. You can find the notebooks you have previously made there. Also, whenever you open the first page of Colab, you will be shown your previous notebooks that you have recently accessed. You can also share your notebook with others using the "share" button on top right corner of the notebook's toolbar.
Introduction to Google Colaboratory and Python
Variables
We mentioned that computers take data as their input to produce our desired outputs. When passing data to a computer program, how can we access the data? How will it be stored for easy future access? The answer is variables! In fact variables are pieces of computer's working memory that can store our desired values, i.e. data. We put a name on each variable so we can easily access the piece of memory. A simple example of this would be "CharacterName = Joey". This name, CharacterName, calls the piece of memory that holds the information "Joey".
Variables also allow single point of control: this means that the value is assigned to a variable in one specific place. That way, you can change the value of the variable in that one spot, and its value will update through the whole program. This helps us avoid writing the value out every time. Writing in a specific value over and over is called "hardcoding". Hardcoding should be avoided!
We are going to call a piece of text a "string" from now on. What if we wanted the string that we are printing out to change every time we run the program? For example, what if we wanted to write a piece of code that asks the user their name, and then says hello to them using their name. If our ambassador Joey runs the program, it would prompt them to enter their name, and then print out: "Hello Joey!" This is an example in which variables come handy!
The first thing we need to do when working with variables is choosing a name for the variable. The name we choose should reflect the information or data that will be stored in it. For example, if the variable stores a user’s height, naming it “a” is not a good choice! It does not tell us anything about the information stored in it. Naming it "height" would tell us more about it.
The variable name can not start with a number, so “123start” is not a valid name. Variable names can contain characters (A-Z or a-z), numbers (0-9) and the underscore character (“_"), but they cannot contain spaces, punctuation, or other special characters. Variable names are also case-sensitive; this means if we call a variable “Height” and another one “height”, they will be 2 separate variables.
Each variable has 2 parts: name and value. The value stored in a variable can change either during the one execution of the program or different executions while it keeps its name. Variables can hold different types of values like an integer (int for short), a string, or a fractional number (we call it float). A variables can also store multiple values. This is called a list.
Other use cases of variables can be:
Counting the number of times an event occurs
Taking input from the user
Choosing a value from multiple values stored in a list
Making complicated calculation more comprehendible
Triggering an action
Below is a short example notebook that you can copy and experiment with; see if you can identify each part of the code and what it does.
0 Comments