Week 1 of 105 to 6 hours5 exercises

Week 1: Meet Python: your first programs

Run your first lines of code, store values in variables, do arithmetic and talk to the person using your program.

By the end of this week you can

  • Run Python code and read its output
  • Create variables and know the four basic kinds of value
  • Use arithmetic operators, including // and %
  • Ask for input and print a friendly reply

What Python is, and how you will run it

Python is a general-purpose programming language. People use it for websites, automation, data work, AI and plain everyday scripts. It is popular with beginners because the code reads almost like English and you can see results after a single line.

A Python program is a list of instructions that run from the top to the bottom, one line at a time. In this course every example has a Run button. Press it and the code runs right here in your browser, so you never have to install anything to start. Change the code and run it again; that is the quickest way to learn.

print() shows something on the screen. The text inside the quotes is called a string.

Variables: names for values

A variable is a name that points to a value, so you can use the value later. You create one with a single equals sign. Read it as "the name on the left now holds the value on the right".

Names can use letters, digits and underscores, but cannot start with a digit and cannot contain spaces. By habit, Python programmers write them in lower case with underscores, like total_marks.

Every value has a type. The four you will use first are int (whole numbers like 42), float (numbers with a decimal point like 3.14), str (text like "hello") and bool (True or False). The type() function tells you which one you have.

One name can be given a new value later; the old value is simply forgotten.

Arithmetic

Python works as a calculator. Besides the usual + - * and / there are three operators that surprise beginners:

// is floor division. It divides and throws away the fraction, so 17 // 5 is 3. % is the remainder. 17 % 5 is 2, because 5 goes into 17 three times with 2 left over. ** is the power. 2 ** 10 is 1024.

Division with / always gives a float, even when the answer is a whole number. Brackets work as they do in maths, and multiplication happens before addition.

Anything after a # is a comment. Python ignores it, so you can leave notes to yourself.

Talking to the user: print and input

print() sends text out. input() waits for the person to type something and hands it back as text. Even if they type 25, you get the string "25", not the number. To do arithmetic, convert it with int() for whole numbers or float() for decimals.

On this page, the box labelled Input is what the person types. Put one answer per line there before you press Run.

Input for input()

int() turns the typed text into a number so we can subtract it.

Common mistakes

  • Forgetting the quotes around text. print(hello) looks for a variable called hello; print("hello") prints the word.
  • Mixing up = and ==. One equals stores a value; two equals compares (you will use that in week 3).
  • Doing arithmetic on input() without converting it first. "5" + "5" gives "55", not 10.
  • Spelling a variable differently when you use it. Python treats age and Age as two different names.

Quick recap

  • print() shows output; input() reads text.
  • A variable is a name for a value; types include int, float, str and bool.
  • // drops the fraction, % gives the remainder, ** is power.
  • Convert typed text with int() or float() before doing maths.

Practise this week

Work through the 5 exercises for Week 1. Each one tells you which tests pass, and has a hint and a solution if you get stuck.

Open Week 1 exercises

Check your understanding

Pick an answer for each question, then press Check answers.

  1. What does print(7 // 2) show?
  2. After age = input("Age? ") and the person types 20, what is the type of age?
  3. Which is a valid variable name?
  4. What is 17 % 5?
  5. What does a line starting with # do?

This course is for learning. The Python engine runs in your browser and your code is never sent to us.