CHAPTER 1 · 20 lessons · 260 exercises
The basics of syntax
Show a value, do some maths, give things names, branch on a condition. The writing that everything else is built on.
When people hear "a program" they tend to picture something complicated. Unpick one, though, and there is surprisingly little going on. It handles values, it remembers them under a name, and it does different things in different situations. Those three are all this chapter is about.
The first thing you do is put something on the screen. Write print and hand it whatever you want shown. That alone will show text and numbers. It looks like small change, but it is your only way of finding out whether the code you wrote really did what you thought. You will be using it forever, so get your hands used to it here.
Next comes the kinds a value can be. In Python, text goes inside quotes, like "hello", and a number is written bare, like 100. They may look alike, but "100" and 100 are different things to a computer. Join two pieces of text with + and they stick together; join two numbers with + and they add up. Try to join text to a number with + and Python flatly refuses. This is the first place beginners come unstuck.
Then variables. Rather than writing the same value again and again, you give it a name and put it away. Write price = 150 and from then on price calls that value back. Fix the value in one place and every use of it follows, which takes the pain out of changing your mind. There is a knack to naming, too: price beats a. The test is whether you will still understand it in three days.
Partway through the chapter we stop and look at indenting on its own. In Python the spaces at the start of a line are the grammar itself — what most languages do with curly braces { }, Python does with how far across a line sits. It is set up that way for readability, but until you are used to it, it is a source of errors. This one is worth facing early and properly.
The second half of the chapter is branching. This is where a program first gets to make a decision. Write an if and the block inside runs only when the condition holds. else covers what happens when it does not, and elif adds another fork. The conditions themselves are built from comparisons like > and ==, and joined with and and or. Words rather than symbols — very Python.
The classic stumble in branching is muddling = with ==. = means "put the value on the right into the left"; == asks "are the left and right the same?". They mean completely different things, and writing one for the other gets you behaviour you never intended. Get it into your hands in this chapter.
One tip for working through it. When an error appears, read it as it stands, first. Plenty of people shut it away because it is in English, but what it says is usually simple. NameError: name 'x' is not defined is "I do not know any x" — either you used the variable before you made it, or you mistyped the name. An error message is not your enemy; it is the teacher standing closest to you.
This course shows you errors exactly as Python produced them. Nothing has been softened or reworded. That is the form you will meet in real work, and the sooner you are comfortable with it the better.
By the end of this chapter you will be able to write a program that takes a value, branches on a condition and produces a result. It is still a small program, but that shape does not change however big an app gets. A system of tens of thousands of lines is doing the same thing over and over: taking values, deciding, producing results.
One last thing: this course is still being written, and the lessons grow from the top down. Where it stops is not a dead end, only a stretch of road still under construction.
Lessons in this chapter
Show text and numbers
- 02Numbers and mathsLet Python do the sums. A number is written without quotes, unlike text.Go to the exercises
- 03Dividing, and what is left overThere are three symbols for dividing. We will see why a tidy answer still comes out as a decimal.Go to the exercises
- 04The order things happen inTimes first, brackets even sooner. Here is how to make Python work things out the way you meant.Go to the exercises
- 05Joining text together
+is not only for numbers. It works on text too — but mix in a number and Python tells you off.Go to the exercises - 06Writing commentsEverything after
#is ignored. Leave a note for the you of next week.Go to the exercises
Give a value a name
- 07Making a variableGive a value a name, and you can call it back up as many times as you like.Go to the exercises
- 08Why bother with variablesIt all runs without them, so why use them? Put the two side by side and see.Go to the exercises
- 09Changing what is insideWhat is in a variable can be swapped out later. We will see why
x = x + 1makes sense.Go to the exercises - 10The shorter way to write it
x = x + 1can be writtenx += 1. It comes up so often that it is worth knowing.Go to the exercises - 11Dropping values into a sentenceWrite
{}insidef"..."and the contents of a variable land there. It is the way you will write most often.Go to the exercises - 12Changing what kind of value it isThe text
"10"and the number10are different things. Here are the tools for going between them.Go to the exercises
The rules of indentation
- 13The rules of indentingIn Python the spaces at the start of a line mean something. Get this now and you will not trip later.Go to the exercises
Branch on a condition
- 14Branching on a conditionWrite an
ifand your program has an opinion for the first time.Go to the exercises - 15Comparing bigger and smallerA comparison answers
TrueorFalse. Let us open up a condition and look inside.Go to the exercises - 16Asking whether things are the same
=puts in,==asks. This is the one beginners muddle most.Go to the exercises - 17The other roadWrite an
elseand you have somewhere to go when the condition does not hold.Go to the exercises - 18Splitting it more waysLine up some
elifs and you can have three roads, or four, or as many as you need.Go to the exercises - 19Combining conditionsIn Python it is
and, not&&. Being able to read it out loud is very Python.Go to the exercises - 20When there is nothing there
Noneis the value for "nothing at all". We will also hand values straight to a condition.Go to the exercises