Programming Like What You Get In The Movies

Programming isn't really that difficult. In fact, one of the great things about it is that (mostly) it makes perfect logical sense.

Getting Started

We'll be using the Python Language during this tutorial. Python is a simple language that uses indentation to show structure in programs. It's also pretty easy. I'm going to assume you're using Python 3. I'll also probably assume a general level of competence, and focus more on programming that on environment setup.

To start off, we won't need to download Python. We'll be using a pretty cool website called repl.it which allows us to write our code in the browser. Go ahead and click the link! It'll look like this:

The repl.it environment.

Your First Program

Hello World

By default in Python, programs output text. You use functions, which are little bits of code that help you write bigger bits of code, to get your program to do output that text. The text is in a window called a console. It's not just the realm of hackers, though. Soon, you'll be able to make stuff happen in the console too! The first thing anyone writes when programming is a 'Hello World' program. All this does is prints hello world to the console.

In Python, it's really easy. You can use the print() function and it will output whatever you tell it to, straight to the console:

Try it now! It's important to remember to put the speech marks around the words you want to print. This way, Python knows it's a string (a string is some text, as opposed to a number or something else).

If you feel confident that you understand that bit of code, try changing it so it prints out your name.

Printing Numbers

You don't just have to print words, though. You can also print numbers. If you want to print a number, you don't have to put it in speech marks, because otherwise Python would think it was a word.

If you wanted, you could get Python to print the sum of two numbers:

Or even multiply two numbers:

You can add (+), subtract (-), multiply (*) and divide (/) using Python, and that's just the start.

Variables

What if you wanted the program to print your name? Yes, you could type print("Monty"), but have a look at this program:

We've said "Monty" lots of times. But what if we wanted to change the story to be about someone else, like "Clare"? Wouldn't it be great if there was a way to get the computer to remember your name... Well there is!

We can use variables to store things for later. It's really easy to assign something to a variable, you can give it any name you want, as long as it doesn't have spaces in it:

Don't worry that we don't get any output in the console. That's because we haven't told Python to print anything. We can print variables just like we printed numbers and strings:

We can also perform maths on two variables, if they are both numbers:

Actually, you can also perform maths on two variables even if they're strings!

Input

Saying Hello

That's all great if your name is Monty, but we might not know the name of our user. So why not ask them? In Python, you can easily do this with the input() function. It works pretty similar to the print() function, but the user has to enter some text, which you can store in a variable:

Calculator

We know how to get a user to input data, and we know how to add things together. Why not try and make our first real program, a calculator? How would it work - we can ask our user to input two numbers which are stored in variables, add them and print the result:

Let's have a look at this program. If we input 2 and 4, it outputs 24. That's odd! If we input 13 and 7, it outputs 137! The problem is, the program thinks that our numbers are strings! It's joining them together, not adding them!

It's easy to solve this though. Python has a function called int() which tries to turn any variable you input into an integer (it won't work if your variable has letters in it. We'll talk about errors later on).

There's another function that can be used to turn integers into strings - str(). You'll need to use this to print your answer:


Thanks for reading. I'll be adding more soon, so stay tuned!


This website was coded on Arch Linux, with the Geany IDE, and the Vim text editor. It is powered by the Dapper static site generator, which is written in Perl, and uses the Skeleton Boilerplate. For analytics, we use Google Analytics (which uses cookies). The site is hosted with static hosting at Surge.sh.