Introduction

Hi! Welcome to Python for Humans, a free online course for learning how to code with the Python programming language. Learning Python has changed my life, and it can change yours, too.

What You'll Build

This course starts from zero and teaches by building. Every chapter ends with a program you wrote yourself, and the course is split into five parts:

Part I is a complete course on its own, and it's where everyone should start. After that, follow what interests you. Parts I and II run entirely in your browser. Games and the web need Python on your computer, so do Part III before either of them.

Why Python?

Python is particularly friendly to beginners because its syntax is relatively straightforward and readable. To display a message, for example, you can simply write print("Hello, World!"). Other programming languages often require more setup and syntax to accomplish the same thing.

And Python isn't just for beginners. It's used professionally for websites, artificial intelligence, scientific research, automation, and much more.

You've probably heard that Python is the language of AI. That's true, and it's less mysterious than it sounds. Most "AI code" is ordinary Python, the same kind you'll write in this course, with a library of code that other people have written doing the hard math.

Want to see Python vs other programming languages?

In Python, displaying "Hello, World!" takes one line:

print("Hello, World!")

In Java, another popular programming language, it looks like this:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

And in C++, again a programming language used often by professional programmers, the syntax required is this:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Quite a difference, right? All three programs display the same message, but Python requires considerably less code.

At this point, traditionally you would need to install Python on your computer and figure out how to run it. But we're going to skip all that for now (later in the course it will be covered). You can write and run Python directly on this page and for the first chapters of this course we'll only work in the browser.

Your First Python Program

Python comes with a built-in print() function that displays messages on the screen. Any text placed between either double "" or single '' quotes will be output. Try it out for yourself! Click the "Run" button on this coding snippet.

print("Hello human!")
print('Welcome to programming.')

Now get creative. Update the text within either print statement with anything you like and press Run. I'll wait.

I know you just ran perhaps your first code ever and are feeling good, but it's important to stress that programs are fast but not inherently smart. Even the smallest little typo will cause them to break. For example, what happens if you start a print statement with double quotes but end with single? Pause and think about what you'd expect before pressing the Run button below.

print("This statement mixes double and single quotes.')
What happened? Uh oh. Red text complaining about a `SyntaxError`. When you start learning to code it is important that you type all the code yourself, by hand, as this helps with learning. It also means you will invariably make mistakes, either simple typos like this or later actual errors in reasoning.

Don't be frustrated! Mistakes are how you learn. Every good programmer has made many more mistakes than you have. That's how they improved. At first, you will be very, very frustrated at seeing these errors. Eventually, you will come to enjoy them--I know, you don't believe me now, but you will--because each error is a chance to learn. Was it just a typo or something else?

In this case, SyntaxError tells us there was a problem with the syntax, which usually means a silly typo. And it tells us the exact line and where the issue started. This is very helpful because soon you will write programs with many lines of code and so having an error message that tells you what you did wrong and where is incredibly helpful.

Numbers

So far, we've used Python to print out messages. But Python can also do math! Try running the following code:

print(2 + 2)
print(6 - 3)
print(5 * 5)
print(10 / 2)

Pretty straightforward, right? Python can add, subtract, multiply, and divide numbers. Notice that we use * for multiplication and / for division.

But here's something interesting. What do you think will happen when you run these two lines of code?

print(2 + 8)
print("2" + "8")

The first line prints 10, as you'd expect. But the second prints 28! What's going on?

When you put quotation marks around something, Python treats it as text, even if it looks like a number. In programming, we call text a string. Without quotation marks, Python treats 2 and 8 as numbers and adds them together.

You can also use + to join strings together and * to repeat them.

print("Hello" + " World!")
print("ha" * 5)

Your turn: Change "ha" to something else and experiment with the number 5. What happens if you make it 100?

As you've discovered, Python uses the same symbols for different purposes depending on whether you're working with numbers or strings. And we're going to take advantage of that soon!

Variables

So far, we've been typing words and numbers directly into our programs. But what if we wanted to use the same piece of information again and again? Or change it in one place and have that change affect the rest of our program?

message = "ha"

print(message * 3)
print(message * 10)
print(message * 20)

Run it once, then change "ha" to "🐍" and run it again. We changed just one line of code, but the result changed in three different places.

That's the power of a variable. It's simply a name we've given to a piece of information so we can use it again. Here we called it message, but we could have called it almost anything.

Imagine changing one variable in a program with thousands of lines of code. A single change could affect the entire program!

Input

A final tool to add before we put everything together is input(), a built-in function that pauses the program and waits for the user to type something. A function is a named piece of code that performs a task. You've already used one with print(). Now let's try another: input().

name = input("What's your name? ")
print("Hello,", name)

Now let's put everything we've learned together.

Time Machine

We've played around with strings, numbers, and variables. Now let's put them together and build our first little program: a time machine! Click Run to see where it takes you.

print("~" * 30)
print("   THE TIME MACHINE")
print("~" * 30)

name = input("What's your name? ")
age = int(input("How old are you? "))
years = int(input("How many years should we travel? "))

this_year = 2026
future_year = this_year + years
future_age = age + years

print("Whoosh! " * 3)
print("Welcome to the year", future_year)
print(name, "is now", future_age, "years old.")
print("That's about", future_age * 365, "days on Earth!")

Notice the int() wrapped around two of our input() calls? Python treats anything typed into input() as text, even if it looks like a number. Remember how "2" + "8" gave us 28? The int() function converts that text into a whole number (an integer) so we can do math with it. Try typing a word like twenty when it asks for your age. Another error! Python can't turn "twenty" into a number, and it tells you so.

Okay, so we haven't actually traveled through time. But look at everything this little program does:

That's every tool from this chapter, working together in fifteen lines of code.

Your turn: Upgrade the Time Machine

This is your program now. Try changing it:

Bonus: What happens if you type a negative number, like -10, when it asks how many years to travel? Can you turn your Time Machine into a Past Machine?

Need help with a code box?
  • Start over: Click Restore original example to bring back the course's starting code and clear the output. Undo restore brings your edit back until you edit again or leave the page.
  • Stop a program: Click Restart to stop it and clear its output. Your edited code stays in the box.
  • Keep your code: This browser remembers your edits when browser storage is available. To keep a separate copy, select your code and copy it into a plain-text file. Clearing your browser's data also clears those saved edits.

We'll learn to save and run Python files on your computer later. For now, you can keep experimenting here.

What's Next?

Look at what you've done in one short chapter. You wrote your first Python code, made Python do math, discovered the difference between 2 and "2", stored information in variables, and asked the user questions with input(). Then you put it all together and built a Time Machine. That's a real program, and you wrote it.

And this is just the beginning. Everything else in this course is built the same way, one small program at a time. You don't need to understand everything before you start. You'll learn as you go.

Next up: strings. You've already used them in every program you've written, but they can do a lot more. In the next chapter you'll make Python talk back, write ridiculous stories, and build a robot that remembers what you tell it.

Ready? Let's go!