The basics of syntax

INPUT · Slides

Making a variable

01 / 06

Giving a value a name

Up to now you have written values straight in. From here you learn how to put a value away under a name.

That named container is called a variable. You make one by writing name = value, and that is it.

price = 150print(price)

Result

150

02 / 06

= does not mean "is equal to"

In maths = means "the left and the right are the same". In Python it does not. It is an instruction: put the thing on the right into the name on the left.

It has a direction, and that is the point. price = 150 is fine; 150 = price is an error. The container goes on the left.

name = "Aoi"print(name)

Result

Aoi

03 / 06

Do not wrap the name in quotes

This is the first place people trip. When you use a variable, you do not wrap it in quotes.

Wrap it and it becomes ordinary text, so what is inside never comes out.

name = "Aoi"print(name)print("name")

Result

Aoi
name

04 / 06

You can do sums with variables

If what is inside is a number, you can work with it straight away. Think of it as the value dropping into wherever you wrote the name.

Inside Python, price * count has become 150 * 3.

price = 150count = 3print(price * count)

Result

450

05 / 06

The rules for names

There are rules about what a name can be.

  • You may use letters, digits and the underscore _
  • It cannot start with a digit (1st is out, first is fine)
  • Capitals and small letters are different (Name and name are two variables)
  • No spaces, so join words up with _

Other alphabets do actually work, but they will sit oddly next to the rest of your code, so leave them.

user_name = "Aoi"print(user_name)

Result

Aoi

06 / 06

Choosing a good name

Follow the rules and a or x1 will run. But running and being readable are two different things.

  • a = 150 … in three days you will not know what number that is
  • price = 150 … you know at a glance

In Python the convention is small letters joined with underscores (user_name, total_price). That style has a name: snake case.

total_price = 1200print(total_price)

Result

1200