The basics of syntax

INPUT · Slides

Comparing bigger and smaller

01 / 06

Show what a comparison answers

You have been writing score > 60 after if. That bit on its own has an answer too, and you can see it by handing it to print.

What comes out is True or False — values for holds and does not hold.

print(80 > 60)print(40 > 60)

Result

True
False

02 / 06

True and False are special values

These two are not text. They are a kind of value called a bool. Wrap one up as "True" and it becomes ordinary text, so be careful.

They start with a capital, too. Write true and Python says it does not know that name — a NameError.

print(type(80 > 60))

Result

<class 'bool'>

03 / 06

What if is actually doing

So if is really nothing more than: work out what follows, then run the block if it is True and skip it if it is False.

Inside Python, if 80 > 60: has already become if True:. Seen that way, if gets a lot simpler.

if True:    print("this always shows")

Result

this always shows

04 / 06

Four ways to compare

There are four symbols for comparing size.

  • a > b … a is bigger than b
  • a < b … a is smaller than b
  • a >= b … a is at least b (equal counts)
  • a <= b … a is at most b (equal counts)

>= is not =>. The arrow first, the = after.

print(60 >= 60)print(60 > 60)

Result

True
False

05 / 06

Do not muddle "at least" with "more than"

This one bites in real work too. "Pass at 60 or above" is >= 60; "pass above 60" is > 60. What changes is what happens to the person on exactly 60.

When you are unsure, try the value right on the boundary. Set score = 60 and check you get what you expected.

score = 60if score >= 60:    print("pass")

Result

pass

06 / 06

Text can be compared too

Not just numbers — > and < work on text, which is compared the way a dictionary orders things.

Mind you, capitals all come before small letters, so the order is not always what your gut expects. Get comfortable with numbers first.

print("apple" < "banana")

Result

True