Skip main navigation

True or False

Computers make decisions by working out if a statement is True or False. In this article, Martin O'Hanlon explains how you can try this out.
A cartoon illustration of a 'True or False' game show

Computers make choices by determining if the value of a statement is True or False. This is known as evaluation. If the statement is True then the program will run the next instruction, otherwise it won’t.

You can try out some evaluations by using the REPL, also called the console in Trinket. The REPL allows you to run one line of code at a time and see the result immediately. It’s a great tool for testing to see how something works.

Tip: REPL stands for “Read-Eval-Print Loop”. it reads one line of code, evaluates it, prints the result and loops back to the start.

Mu

If you are using Mu, follow these instructions to use the REPL:

  • Open Mu and click on the “REPL” button.

Mu IDE, with the REPL button circled in red

Trinket

If you are using Trinket, follow these instructions to use the REPL (known as console):

  • Open trinket.io.

  • Create a new Python trinket, by clicking New Trinket, Python.

New Trinket> Python selected”>

  • Open the console by clicking on the down arrow next to Play and selecting >_ Console.

Trinket, with the down arrow next to the play button symbol highlighted with a red box. On the menu below it, "Console" has been highlighted with a red box.

Try some evaluations

  • Enter the following into the REPL window and press Enter:
1 == 1

Note: the double == operator means “is this equal to?” as opposed to a single = which follows a variable and means “make this equal to”.

When you press Enter, True will be displayed. This is because it’s true that 1 is equal to 1.

Mu IDE with the REPL open. It read "In [1]: 1==1" followed by "Out[1]: True". The cursor is then ready for input after "In [2]: "

>> 1==1" followed by "True" on the next line. The cursor is then ready for input after ">>>"”>

  • Try entering 1 == 2. False will be displayed, because it’s false that 1 is equal to 2.

  • Enter these evaluations and see if you can predict whether the outcome will be True or False.

1 < 2

The operator < means less than. Ask yourself is 1 less than 2? If the answer is yes then the statement is true.

1 > 2

> means greater than, so is 1 greater than 2?

1 != 2

!= is the operator for is not equal to, which means this statement means is 1 not equal to 2. != is the opposite of the == operator.

Whats do you think will be the result of:

1 < 1

This evaluates as false because 1 is not less than 1. However, instead of <, you can use <= test if something is less than or equal to:

1 <= 1

This would give True.

You can evaluate other data types, not just integers (whole numbers) as you have been doing.

  • Try some string evaluations:
"hi" == "bye"
"hi" != "bye"

What do you think the result would be for:

"a" < "b"
  • Run it in the REPL and observe the result.

Perhaps the result makes sense to you, but what about other character evaluations such as "=" < "!"? Discuss the results in the comments and try to draw some conclusions as to why you get the results you do.

Evaluation is one of the core concepts of computer programming, so it’s important that you understand the principle. Throughout this course there will be lots of opportunities to practise using and creating statements to be evaluated.

You can close the REPL by clicking on the REPL button in Mu or by selecting run from the drop-down menu in Trinket.

This article is from the free online

Programming 101: An Introduction to Python for Educators

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education