Have you ever forgotten a distant relative's name or a friend's birthday? Well, computers don't forget these things when they're told to remember them.
OK computer: remember Joe as my name display my name
//Output Below
Joe
The North .. err .. the computer remembers until we tell it to hold on to a different name.
Psst: these instructions are a little oversimplified, of course, but we'll get to actual computer-speak in no time.
To remember things, computers use variables, a concept that's a lot like boxes with labels. Variables, too, have a label and content.
OK computer: remember 1970/1/1 as my birthday display my birthday
//Output Below
1970/1/1
Great! Just like you'd label a box my birthday
, it makes sense to call a variable my birthday
, right?
In computer-speak, labels have to be single letters or single words, which disqualifies my birthday
.
Can you think of a valid label?
Sweet it's the first! Labels don't have to be real words and can include numbers, as long as they don't start with them. Also, it's smart to use meaningful labels.
Now, why would we use variables when we could also use the information they carry?
OK computer: remember "Walter White" as my_name display my_name
//Output Below
Walter White
Well, because variables can change the values they represent as the program runs. That's why we can use variables to keep track of things.
There are different kinds, or types, of information. Most importantly, there are words and numbers.
Which two statements would you say make sense?
OK computer:
It wouldn’t make sense to subtract a year from your name, would it?
There are different kinds of numbers as well. Among others, whole numbers and floating-point numbers.
OK computer: divide 10 by 3 display the result divide 10 by 1 display the result
//Output Below
3.3333333333 10
In practice, floating-point numbers often occur when we divide whole numbers.
Also, numbers are great for arithmetic.
OK computer: remember 1 as my_number add 42 to my_number multiply my_number by 3 display the result
//Output Below
129
Boom! Even if we were mathematicians, it'd be hard to beat the arithmetic accuracy and speed of computers.
Words, which are also called strings, have quotation marks and can't be used for arithmetic. They're still pretty great, though.
Have a guess: What's the computer going to display?
OK computer: remember my_name as "Walter" add "White" to my_name display my_name
Yup, the strings are combined without a space. As spaces are also strings, we've got to add them if we want to see them.
As we've seen, variables are kind of like boxes for values. What if we want to store more values, though?
Well, there are boxes with space for multiple values: lists.
OK computer: create a list add "cereal" to the list add "milk" to the list display the list
//Output Below
cereal, milk
See that? Lists are a great way to store multiple values; especially, if they're of the same kind.
Because computers can't think, they can't make decisions by themselves. That's why they need rules to guide them.
OK computer: rule #1: there are no rules
As you may have expected, these rules have to be a lot more precise ... and stringent.
Computer rules depend on conditions, another word for results of evaluations.
Which of these conditions might help you determine if you need to go to work?
Sweet except the last! If these conditions are met, it's probably time to go to work.
Computers use words like if and then to guide their behavior.
OK computer: if it is before 12pm then display "Good morning!"
Sweet! That's a precise but straight-forward rule, right?
So, what if we want some other thing to happen when the condition isn't met? There's a word for that and it's called else.
OK computer:
if it is before 12pm then display "Good morning!" else display "Good day!"
Sweet! The ability to follow different paths in different situations is what makes computers seem intelligent.
In essence, rules make programs more intelligent.
Do you remember the door sequence from earlier?
unlock the door open the door enter the room close the door
Well done! Now, what if the door was unlocked from the start? And what if it was open?
Let's take care of that. Which of these rules are necessary to make the sequence more intelligent?
Nailed it if you chose de-first and de-second! These rules make sure the door is unlocked and open before you enter the room. The others aren't necessary.
With the help of else if, you can take care of alternative scenarios.
How would you greet people after 6pm? You wouldn't wish them a good day, would you?
OK computer:
if it is before 12pm then display "Good morning!" else if it is before 6pm then display "Good afternoon!" else display "Good evening!"
Fantastic! You're getting a hang of computer rule-making, aren't you?
While computers can't think, they can repeat tasks over and over again and never get bored.
What might the output of this be?
OK computer: repeat 5 times: display "Hello!"
It'd take a huge amount of time to handle repetitive tasks if computers couldn't repeat them.
Imagine you wanted to make sandwiches for you and a friend. It sounds simple, but it already involves some repetitive tasks.
get 4 slices of bread put peanut butter on slice 1 put peanut butter on slice 2 put jelly on slice 3 put jelly on slice 4 press slices 1 and 3 together press slices 2 and 4 together
Complicated, huh? Just imagine you were to make sandwiches for ten or more people!
Now, let's actually make ten sandwiches with a little big shortcut!
repeat 10 times: get 2 slices of bread put peanut butter on slice 1 put jelly on slice 2 press slices 1 and 2 together
There they are: ten sandwiches, computer-crafted.
These shortcuts are known as loops (because they loop back to steps). As a programmer, it's up to you to find repetitive tasks and wrap them in loops.
Can you think of repetitive tasks?
Great! If the same or similar instructions follow each other, it might be good idea to wrap them in a loop.
Quite often, a sequence needs to be repeated for a set number of times.
pump in water repeat 72000 times: rotate washing machine drum then pump out water
Fantastic! Count-controlled loops come in handy when you know exactly how often the sequence has to be repeated.
At times, however, you might not know how often a sequence has to be repeated.
This breakfast sequence is an example.
get a bowl, cereal, and milk pour cereal into the bowl pour milk into the bowl repeat until the bowl is empty: spoon the bowl contents into the mouth
Sweet! Condition-controlled loops repeat sequences while or until a condition is met.
Can you think of a way to loop through a shopping list and speed up a shopping spree?
get a basket repeat for every item on the list: find the item put the item in the basket then proceed to checkout
Fantastic! That way, you'll be at the checkout counter in no time.
Now that we know how to store values, what if we want to store multiple values, like a bunch of friends?
OK computer: remember "Cece" as my_friend remember "Jess" as my_friend remember "Nick" as my_friend display my_friend
//Output Below
Nick
Huh! It looks like my_friend
is a variable and can only store a single value at a time.
In that case, we'll create a variable for every single one of our friends!
Let's try and output a value from one here.
OK computer: remember "Cece" as my_friend_1 remember "Jess" as my_friend_2 remember "Nick" as my_friend_3 display my_friend_3
//Output Below
Nick
So, if we have hundreds of friends, does that mean we have to create hundreds of variables?
Instead of having three variables that store single names, we can make a list that holds all the names.
OK computer: create a list of my_friends add "Cece" to my_friends add "Jess" to my_friends add "Nick" to my_friends display my_friends
//Output Below
Cece, Jess, Nick
Fantastic! This list stores "Cece"
, "Jess"
, and "Nick"
under a single name.
Now that the values are on a list, how can we access them?
Let's try and display one of our friends.
OK computer: create a list of my_friends add "Cece" to my_friends add "Jess" to my_friends add "Nick" to my_friends display the first item of my_friends
//Output Below
Cece
Just like that! We can point to the position of the value in the list.
Because we can insert values at specific positions, lists are quite flexible.
OK computer: create a list of favorite_dishes add "Curry" to favorite_dishes add "Pizza" to favorite_dishes add "Ramen" to favorite_dishes as the first item display favorite_dishes
//Output Below
Ramen, Curry, Pizza
Nice! When we add or insert a value at a position, the other values simply move back.
And, of course, we can remove values from lists as well.
Let's try and remove one item from our list here.
OK computer: create a list of favorite_dishes add "Curry" to favorite_dishes add "Pizza" to favorite_dishes add "Ramen" to favorite_dishes remove the first item from the list display favorite_dishes
//Output Below
Pizza, Ramen
Poof! We can either tell the computer to remove a specific value or an item at a specific position.
Alright. Now, let's take a look at why lists are so great, shall we?
OK computer: repeat for every friend in my_friends: display "I like " and the friend
//Output Below
I like Cece I like Jess I like Nick
Sweet! We can create a loop to go through my_friends
one-by-one.
Psst: again, we need to include a space between "I like"
and the name.
Another great thing about lists is that we can sort them.
OK computer: sort my_friends in reverse alphabetical order display my_friends
//Output Below
Nick, Jess, Cece
Sweet. That would've been a lot harder with single variables, eh?
Sequences often contain parts that come up multiple times and in very similar ways. Can you think of such a part?
True that! A breakfast routine may well come up every day, while the other parts of sequences may vary too much.
Programs, too, contain parts that come up again and again. For example, what might happen when we cut text?
OK computer:
Perfect! Cutting text involves remembering and deleting a text snippet while leaving the cursor where it is.
In order to reuse a procedure, we need to tell the computer what the procedure looks like.
OK computer: remember as cutting: remember selected text delete selected text
As soon as we have defined a procedure, we can reuse it anywhere in the program.
Pretty much anything we can tell the computer to do, including cutting, is actually a pre-defined procedure.
OK computer: create a list of my_friends add "Phoebe", "Chandler", and "Rachel" to my_friends sort my_friends in alphabetical order display my_friends
//Output Below
Chandler, Phoebe, Rachel
There! How would the computer know how to create or display a list let alone sort it without pre-defined procedures?
When we pass information, or input, to procedures, they become more flexible.
OK computer: remember as greeting somebody: display "Hey ", somebody, and "!"
Yass! By passing a name to the procedure, we can make it more flexible.
Parts of sequences aren't limited to tasks, though. They can also produce information, or output.
OK computer: remember as converting miles to kilometers: multiply miles by 1.60934 return the result
Great! While procedures perform tasks, so-called functions produce, or returns, output.
Then, how might we get the computer to display 5 miles in kilometers?
OK computer: convert 5 miles to kilometers remember the result as kilometers display kilometers
Good stuff! Because functions return something, the computer needs to remember the result in order to display it.
So, the good news is that computers don't make mistakes.
The bad news is that, because it's up to humans like us to tell computers what to do, mistakes can and often do creep in.
These mistakes, or bugs, can cause a program to produce unexpected results or stop working altogether.
OK computer: remember 0 as my_number repeat until my_number is less than 0: add 1 to my_number then display my_number
//Output Below
...
See that? Because my_number
will never be less than 0
, this so-called infinite loop will never get to display my_number
.
Debugging, which means finding and fixing bugs, is an important part of programming. Can you spot a bug in this program?
OK computer: remember "Walter" as my_name add " White" to my_name display my name
display my name
Perfect! Because my name
misses an underscore, the computer can't make sense of line 3.
Instructions need to follow special rules. When we break these rules, computers don't know what we want them to do.
What rule would make sense here?
OK computer: if it is teatime then display "time for afternoon tea"
//Output Below
time for afternoon tea
See that? These so-called syntax bugs can be typos or words that computers don’t understand.
When a program has a logic bug, it doesn't crash but doesn't act as intended either. Can you spot such a bug in this program?
OK computer: remember 42 as my_age if my_age is greater than 65 or my_age is greater than 12 then display "20% discount" else display "no discount"
my_age
is less than 12
Fantastic! We need to check if my_age
is less than 12
or anybody above 12
gets a discount.
As programs become more and more powerful, their instructions become harder and harder to read. How might we be able to prevent that?
Yes! Using comments and meaningful names for variables and functions, programs become easier to understand and debug.
Before we release a program, we should perform a range of tests. Why might this be important?
Great! Even though we won't be able to find every bug in a program, we shouldn't release a program with severe bugs.
As you know, computers are pretty great if they're given precise instructions.
Still, there's a considerable problem that we haven't talked about so far. What might that be?
Yeah, sorry about that! In fact, computers don't understand anything other than 0s and 1s.
So, if computers don't understand natural languages, how do we get them to do all these things we've talked about?
Not that hard, eh? We need to find a common ground .. and that's up to programming languages.
Still, programming languages aren't much more than communication aids between you and your computer. Why might that be?
That's it! Because computers use machine language, special programs called compilers have to translate our instructions into a great many 0's and 1's.
Now, what might we call instructions and rules written in a programming language?
Nice! When we put source code in the right order, it tells the computer how to behave.
There are many programming languages for many different purposes.
In this course, we'll sneak-peek into a simple but powerful programming language called Python.
my_name = "Joe" print("Hey, " + my_name)
//Output Below
Hey, Joe
Woot! We just wrote a bit of Python.
Psst: in order to experiment with Python code, it's a great idea to visit the Python website and hit the >_
button to launch the interactive shell.
In Python, storing information is easy; in fact, we did it a minute ago.
Instead of telling the computer to remember a value "as something", we write down a label and use the =
sign to assign something to it.
my_result = (1 + 3 - 5) * 2 / 4 print(my_result)
//Output Below
-0.5
See that? The print
statement displays the value of anything you put in the parentheses.
Defining rules in Python works almost in the same way as in a natural language.
What's this code going to display?
hour = 10 if hour < 12: print("Good morning!") else: print("Good afternoon!")
//Output Below
Good morning!
Nice! The <
sign checks if the left term is less than the right term. As that's the case, "Good morning!"
is displayed.
Python can handle repetitive tasks as well.
Let's make the code run for as long as the bowl has contents.
cereal = 100 milk = 100 bowl_contents = cereal + milk while bowl_contents > 0: bowl_contents = bowl_contents - 10 print(bowl_contents)
//Output Below
0
Well done! While the bowl's contents are greater than 0
, there has to be something in the bowl.
Of course, we can also make a list of friends
.
Let's try and output the first value in our friends list here.
friends = [] friends.append("Cece") friends.append("Jess") friends.append("Nick") print(friends[0])
//Output Below
Cece
Do you see the difference? Python starts counting at 0
, so the first item in the list has the position 0
, the second item has the position 1
, and so forth.
In Python, defining procedures and functions works with the def
keyword.
def convert_to_kilometers(miles): km = miles * 1.60934 return km
Fantastic! We've bundled instructions to convert miles to kilometers.
Basically, it is writing a set of instructions (code) to a computer, in one of the computer's languages (called a Programming language), in order for the computer to "understand" and execute the instructions.
When you program, you are basically controlling the computer. The main purpose for programming is automating tasks and therefore making them easier to execute. Think of say; automating your computer to send a greeting message to a friend every morning instead of having to do this every morning yourself, which of course, you could sometimes forget. Or in another case, for the computer to remind you of your friend's birthday. All you need is to let the computer know your friend's birthday and tell the computer that when that day comes, it should let you know. That's how come Facebook never forget your birthday.
We call the writing of instructions to the computer coding. It's actually like writing out a recipe for a food. It's that simple, what makes it a bit difficult is the fact that every step counts, and you know that's how it is with recipes too, missing a step or putting one before the other will mess up your food.
The difficulty of programming can be subjective but we can agree on one thing, you will have millions of programmers out there who have already gone through all the problems you will encounter when you start programming. This means your solutions are a search away, you just have to search and I assure you that you have your solution and that someone actually typed the problem the way you are typing it: small world right?
It's a misconception that programming is maths. Please, programming can be any subject at any point depending on the project you are undertaking. And as a beginner, you won't have to worry about any of these. And even when it comes to maths, you will rarely have to think up the solution yourself because the truth is, someone has already worked out the solution for you in the most elegant way.
That's a good question. To start programming, you need an environment (a text editor) where you will be writing your instructions. I recommend, you download Visual Studio Code, please, just search for "Visual Studio Code Download" and download it. Install it after it finishes downloading, visual studio code is for PCs, in case you want to program using your phone, it is not impossible, get to the play store and make some search, you will find an editor for your language of interest. For example, if you want to code in python, you can search for qpython or pydroid on the play store.
After that, you will need to start with a particular programming language. We have a lot of programming languages here. You will need to read the "Getting started" or Introduction section of the language you are interested in, in order to understand what you can use it for.