Programs and expressions 0/5

Lesson 1 of 21

Your first program.

Python is a programming language. The print instruction shows a value on the screen.

Part 1 of 6 About this course

Who this course is for

This course is for complete beginners. It is also for you if you learned some programming a while ago and are rusty enough that you barely remember the syntax. You don't need to know anything before you start.

It is modeled after CS 61A, the introductory computer science course at UC Berkeley, and follows that course's textbook, Composing Programs. CS 61A teaches Python the way a computer scientist looks at it: how a program actually gets worked out step by step, how functions build on each other, and, at the end, how to write an interpreter yourself.

By the end of the course, you will be able to:

  • write programs with variables, functions, if and loops
  • work with lists, strings, dictionaries and sets
  • use recursion, sorting and brute-force search
  • define your own classes
  • read a problem's input and print an answer an online judge accepts
  • estimate whether a program is fast enough before you run it
  • build a small interpreter for a calculator language

Python and C++

We will write almost everything in Python. Python is easy to read, so you can pay attention to the idea instead of the syntax.

Every code example also has a C++ tab. You don't have to learn C++ here, but it helps to see the same program in a second language: you start to notice which parts are the idea and which parts are only how one language writes it.

Here is a complete Python program. It prints 7:

Sign up to run
print(2 + 5)

Switch the tab to C++ and the same program takes six lines. C++ makes you say where the program starts and where printing comes from. Python asks for neither.

Interpreted and compiled languages

So why not write everything in C++? Mostly because Python is easier to start with. What you give up is speed, and the reason is how each language runs a program.

Python is an interpreted language: another program, called the interpreter, reads your code and carries it out one step at a time while it runs. C++ is a compiled language: a program called a compiler translates your whole program into the processor's own instructions before it runs.

That makes Python slower. When Python reaches a + b, it first has to check what a and b are, because + adds two numbers but joins two pieces of text. It does that check every time it gets there. A C++ compiler works it out once, before the program starts. To give you a sense of the gap, counting the numbers from 1 to 100,000,000 that are divisible by 3 or 5 took about 5 seconds in Python 3.14 and 0.05 seconds in C++ on the Mac we checked this lesson on.

For nearly everything in this course you won't notice, because the programs finish in a fraction of a second either way. It starts to matter on problems with a time limit. USACO, the USA Computing Olympiad, gives C++ programs 2 seconds per test and Python programs 4, and the USACO Guide still finds Python too slow for most problems in USACO's two hardest divisions. Part 3, Interpreters and compilers, goes deeper into how both work.

Practice problems and online judges

Each lesson ends with practice problems. They come from online judges: sites where you submit a program and the site checks it for you. The judge runs your program on tests you can't see, and accepts it only if every output is right and the program finishes within the time limit.

Most of our problems are from AtCoder, a Japanese site that holds a Beginner Contest about once a week, and the rest are from Codeforces. You may have heard of USACO and LeetCode too. USACO judges programs the same way. On LeetCode you write one function instead of a whole program, and the site checks what it returns.

These problems are a good test of whether you can use an idea on something you haven't seen before. They are practice, though: the lessons teach each idea directly, and you don't need to care about contests to take this course. Reading input and the judge, a later lesson in this chapter, shows you how to submit a program.