Python for AI, module 1: Python basics, lesson 1 of 5

Your first program

Write a line of Python, run it, and see what comes back. Everything in AI code starts with instructions this plain.

6 minute read2 things to try6 questions

Run something

The box below is real Python, running inside this page. Nothing to install. Press Run.

Loading the interactive…

Three lines, three results. print shows whatever is inside its brackets. 2 + 3 is a sum, so Python works it out. "2 + 3" has quotation marks, so it is text, and Python shows it exactly as written.

Change it

Edit the code: print your own name, then try print(7 * 6) and print(10 / 4). Press Run again after each change. If you break something, the arrow button puts the original back.

When it goes wrong

Delete one quotation mark and run it. Python stops and prints an error that names the line and the problem. Errors are not failures. They are Python telling you exactly where it got confused, and reading them is most of what programmers do.

Loading the interactive…

Python reads the whole program before running it, so the first line does not appear either. Fix the quote and run it again.

What to remember

  • A program is a list of instructions that run in order, top to bottom.
  • print(...) shows a value. Quotation marks make text instead of maths.
  • An error message names the line and the problem. Read it, fix it, run again.

Check yourself

3 questions. Each one explains itself once you answer. Get 2 right to complete the lesson.

Question 1

1What does print(4 * 5) show?

Question 2

2What does print("4 * 5") show?

Question 3

3Python shows an error message. What is the best thing to do?

0 of 3 answered

Next lesson: Variables and types