Programming for Teens: Beginning Python Tutorial

In this Python tutorial you’ll be introduced to computer programming using one of the most beginner-friendly languages available. By .

Leave a rating/review
Save for later
Share

Have you ever wondered how a computer works? While we can’t teach you everything in one blog post, you can get a pretty good start by learning how to write your own programs!

In this Python tutorial, you’ll learn the basics of programming a computer using one of the best languages for beginners.

What is Programming?

To put it as simply as possible, programming is the act of writing code that instructs a computer to perform a task.

A task can be as simple as adding two numbers together, or as complex as sending a spaceship into orbit!

The smallest element of a program is known as statement — this represents a single instruction to the computer.

All the things!

After you’ve written your program, a compiler converts the code you’ve written into machine code — the lowest-level language of a computer. Machine code instructs the central processing unit, or CPU, what steps to take such as loading a value, or performing some arithmetic.

If you’ve ever heard somebody say “I compiled my program”, it means they converted their code into machine code.

Why wouldn’t you just write machine code directly? The obvious reason is that program code is human readable. Compare the Python version of a program with its corresponding set of machine code below:

The Python Program

print "Hello, World!"
...
"Hello, World!"

The Machine Code Equivalent

c7 3c 2a 3c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c
28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c
2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b
2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c
3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28
5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a
2b 2a 00 00 01 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 64 48 65 6c 6c 6f 2c 20 57
6f 72 6c 64 21 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...
"Hello, World!"

It’s quite evident why you don’t want to program directly in machine code. However, there are a few people who do — there’s no accounting for taste! :]

There’s one minor detail we left out above. Python is an interpreted language; you don’t compile it directly into machine code as we alluded to above.

Instead, Python uses something called an Interpreter. An interpreter is yet another program that compiles code into something called Bytecode, which is then converted into machine code as the program runs. You’ll read more about interpreters later.

When you finally run the program, the machine code you recently compiled is loaded into memory and read by the CPU to execute the program.

However, you don’t need to really understand the inner workings of compilers to get started programming in Python, but you should first make sure that you have Python installed.

Getting Started

If you’re on a Mac, you’re in luck — Python comes preinstalled on a Mac.

To use the Python interpreter on a Mac, open Terminal.app; you can find it either in your Applications/Utilities folder, or by typing Terminal directly into Spotlight, like so:

terminalspotlight2

Once Terminal is open, type the following command and press Enter:

$ python

You should see something that looks like the following:

Screen Shot 2014-03-10 at 10.47.36 PM

Note: If you don’t get the output shown above, post the output from Terminal on the forum and we’ll do our best to help you out!

Windows

The process is a little more involved on Windows — but then again, most things are on Windows! :]

First, visit the official Python downloads page in your browser.

Scroll past the Python 3.x.x versions, all the way down to Python 2.7.x. Download the Windows installer, run it, and follow the instructions it presents, accepting the defaults along the way.

Once the installer has finished, you’ll need to launch the interpreter.

On Windows Vista or Windows 7, launch the interpreter like so:

  1. Click the Start button in the lower left hand corner
  2. Click on All Programs
  3. Open the folder named Python
  4. Double click the IDLE interpreter

If you’re on Windows 8, launch the interpreter this way:

  1. Click the Start button in the lower left hand corner
  2. Type IDLE into the search bar
  3. Click on IDLE (Python GUI)

Regardless of how you launched the interpreter, you’ll want to make sure that it works. Type the following command in Terminal, or Command Prompt in Windows, and press Enter:

print “Hello World!”

Although it doesn’t seem like much, you’ve just written your very first Python program! Printing Hello, World is often considered the de facto starting point when learning most languages.

The print command instructs the computer to print what follows on the screen — not print it out to your printer! Note the quotation marks around “Hello World”; anything contained inside the quotation marks is considered to be regular text and won’t be interpreted as an instruction.

Variables

Variables are a way of storing data in the memory on a computer; you’ll use them a LOT as you program. In some languages, variables have a specific type which indicates what kind of variable class they belong to.

In Python, you don’t have to declare your variable types. Don’t worry too much about that detail now; you’ll learn more about this a little later in the tutorial.

Type the following command into the interpreter and press Enter:

hello = "Hello World!"

This declares and sets the hello variable to the value Hello World. Now, instead of typing “Hello World” everywhere you need that string in your program, you can just use the hello variable.

Type the following command into the interpreter and press Enter:

print hello

This prints out the same thing as your Hello World example, but instead it’s printing out the value of the hello variable.

Variables can also be used to store numbers. Type the following commands into your interpreter:

x = 5
print x
y = 10
z = x + y
print z

Note: From now on, there may be multi-line statements of code for you to type in. Simply hit Enter after each line.

Note: From now on, there may be multi-line statements of code for you to type in. Simply hit Enter after each line.

Try to guess what the code above does before reading the spoiler below:

[spoiler]The code prints 5 followed by 15. The first print statement prints the variable x which you set to 5. It then prints the result of the calculation y + x. Since y is set to 10 and x is set to 5, it prints out 15.[/spoiler]

Variables are core to most of the programming you’ll do in your life. You’ll become intimately familiar with them as you work your way through this tutorial!