Using computer-speak

Computers don't understand

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?

  • Computers don't understand natural languages

Yeah, sorry about that! In fact, computers don't understand anything other than 0s and 1s.

Common ground

So, if computers don't understand natural languages, how do we get them to do all these things we've talked about?

  • Programming languages

Not that hard, eh? We need to find a common ground .. and that's up to programming languages.

Programming languages

Still, programming languages aren't much more than communication aids between you and your computer. Why might that be?

  • Because computers actually "speak" in the 0's and 1's of machine language

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.

Code

Now, what might we call instructions and rules written in a programming language?

  • Source code

Nice! When we put source code in the right order, it tells the computer how to behave.

Peeking into Python

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.

Storing information

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.

Making decisions

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.

Repeating things

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.

Making lists

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.

Bundling instructions

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.