Pathwise

Intro to Python · Lesson 1 of 12 · 12 min

Your first program

A program is a list of instructions Python runs from top to bottom. Write your first lines with print, and use Python as a calculator that knows the order of operations.

WHAT A PROGRAM IS

Instructions, run from top to bottom

A program is a sequence of instructions. Python, the interpreter, reads them one at a time: it starts at the first line, does exactly what it says, then moves to the next. Order matters, so swapping two lines can change what happens. And Python never guesses what you meant; it does what you wrote.

A program with three lines that print Hello, then 7, then Bye shows them in exactly that order, one per line.

print("Hello")
print(7)
print("Bye")

Output

Hello
7
Bye

Each print shows one value and then starts a new line, so you get three lines. The quotes around Hello aren't printed; they only mark where the text starts and ends. The 7 needs no quotes because it's a number.

Check yourself

Omid writes print("2 + 3"). What appears on the screen?

  1. 5
  2. 2 + 3
  3. "2 + 3"
  4. An error
Show the answer

2 + 3

Right. Inside quotes, 2 + 3 is just text, three characters and two spaces, so print shows it exactly as written, without the quotes.

PYTHON AS A CALCULATOR

Seven operators cover everyday maths

+ adds, - subtracts and multiplies (a star, not the letter x). / divides and always gives a decimal, even when the answer is whole. // divides and keeps only the whole-number part, % gives the remainder, and * raises to a power.

10 / 2 is 5.0, 7 / 2 is 3.5, 7 // 2 is 3, 7 % 2 is 1 and 2 ** 3 is 8.

print(10 / 2)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 3)

Output

5.0
3.5
3
1
8

Line by line: division always gives a decimal, so 10 / 2 prints 5.0, not 5. Then 7 split into 2 is 3.5; its whole-number part is 3, with 1 left over. The last line is 2 to the power 3, which is 2 * 2 * 2.

Check yourself

print(7 // 2)
print(7 % 2)
  1. 3
    1
  2. 3.5
    1
  3. 3
    0.5
  4. 3.5
    0
Show the answer
3
1

ORDER OF OPERATIONS

Multiplication before addition, just like in maths

When a line has more than one operator, Python doesn't simply go left to right. It does *, /, // and % before + and -, the same rule you learned at school. Anything in round brackets is worked out first, so brackets are how you say which part should happen first, and they make your intent easy to read.

2 + 3 * 4 is 2 + 12, which is 14. (2 + 3) * 4 is 5 * 4, which is 20.

Check yourself

print(2 + 3 * 4)
  1. 14
  2. 20
  3. 24
  4. 2 + 3 * 4
Show the answer
14

Step through it

  1. Python reads print(2 + 3 * 4)

    The code strip at the top holds one line, print(2 + 3 * 4), and the out panel below it is still empty. Before print can show anything, Python has to work out the one value inside the brackets.

  2. Multiplication first: 3 * 4 becomes 12

    Python does the multiplication first, just as in maths: 3 * 4 is pulled out and turns into 12. The strip now reads print(2 + 12), and the out panel is still empty.

  3. Then the addition: 2 + 12 becomes 14

    With the multiplication done, only the addition is left: 2 + 12 turns into 14, right under the first step. The strip now reads print(14), a single value ready to be shown.

  4. print shows 14

    The 14 travels down the arrow into the out panel: that's what you see on the screen. Beside it, dimmed, is what brackets would change: (2 + 3) * 4 does the addition first and gives 20.

Check yourself

print(10 / 2) prints 5.

Show the answer

False

False. It prints 5.0. The / operator always gives a decimal number, even when the division comes out whole. If you want a whole number, 10 // 2 prints 5.

print(2 + 3)
print(4 * 5)
print(10 - 7)

Output

5
20
3

Three lines, three results, in the same order as the code. Try it yourself: change a number or an operator and run it again, and predict the output before you tap run.

Check yourself

  1. print("Start")
  2. print(6 * 7
  3. print("End")
Show the answer

The bug is on line 2.

Found it. Line 2 opens a bracket and never closes it, so Python stops with a SyntaxError before printing anything at all. It should be print(6 * 7).

Lesson recap

  • A program is a list of instructions that Python, the interpreter, runs one at a time from top to bottom.
  • print(...) shows a value and starts a new line; text needs quotes, numbers don't, and the quotes aren't printed.
  • / always gives a decimal (10 / 2 is 5.0); // keeps the whole part, % gives the remainder, ** is a power.
  • * and / happen before + and -, so 2 + 3 * 4 is 14; brackets go first, so (2 + 3) * 4 is 20.
  • A missing bracket or an x for multiply is a SyntaxError; text without quotes is a NameError.

Keep it, don't just read it

Pathwise brings each idea back just before you'd forget it, with a quick question. Free on Android and on the web, in English and Persian.

Cafe Bazaar Myket Open the web app

All lessons in this course

  1. Your first program
  2. Variables: names for values
  3. Strings: working with text
  4. Input and types
  5. Making decisions with if
  6. Lists: many values in one place
  7. Loops: doing it again
  8. Functions: name a job, reuse it
  9. Dictionaries: look it up by name
  10. Reading error messages
  11. Saving and reading files
  12. Final project: an expense tracker