1 Minute Read #18 - IF statements in Python

We can mark our progress through learning a programming language through the following concepts:

●    Sequence

●    Selection

●    Repetition

The first of these is what we learn at the very beginning of programming a computer - learning how to correctly program a computer to do something in a certain set of steps, such as calculate a value and print the result. The code, when written correctly, performs these steps in a certain sequence.

Selection is a more advanced twist on this - it allows us to insert code which gives the user multiple pathways to take. In Python we use statements known as IF, ELIF and ELSE. These

Let’s try a couple of Naughty List programs…

In Python→ Idle 3.6 → New File, type the following:

Save the file as NaughtyList.py and run it.

It should look like this:

Now we can make a more sophisticated program. Start another New File, rather than use the existing NaughtyList file.

Call the new program NaughtyListVersion2.py

Type the following:

Test your code a few times to check it is working. It should run like this:

So far we have used IF and ELIF and ELSE in a fairly simple way. The next stage is to nest IF statements.

Clearly you can nest lots of different blocks of code within each other if you know what you are doing.

Whitespace is something like a tab (a block of blank spaces that move the cursor along when you hit the TAB key) or even a single tap of the spacebar. Python code that is at the same position - indented the same number of spaces from the left margin - groups the code into its own block. Whenever you start a new line of code with more spaces than the one that came before it, you are starting a new block. AND that block is part of the previous one.

The code below nicely illustrates blocks of code, including nested IFs. Type it into a New File, and call the file InstaMegaIF.py

As easy as a mince pie!



To view or add a comment, sign in

More articles by Stephen Trask

  • The nature of learning

    What is learning? Across the globe parents are being confronted with this question. So are children and teachers.

    4 Comments
  • 1 Minute Read #20 - AI and probability

    Artificial Intelligence algorithms love simplicity. Noughts and crosses is simple.

  • 1 Minute Read #19 - AI and Game Trees

    Artificial Intelligence has long been associated with calculating or computing something - Alan Turing designed a…

    1 Comment
  • The Seven Cs of Claxton and Lucas - 'Educating Ruby'

    It is not often that educational commentators and renowned learning experts agree on a particular matter. One thing…

  • Digital Strategies for 2019 and beyond

    Digital strategies loom large in any forward-thinking organisation, and a recent report from PWC Australia caught my…

  • 1 Minute Read #17 - f-strings in Python*

    How long is a piece of string? If it is as long as the many ways to manipulate strings in Python, then it is mighty…

    1 Comment
  • 1 Minute Read #16 - Applications of Python

    Python is a very powerful, general purpose programming language. However, when students are learning the nuances of…

  • AI and the Smart City Concept

    A smart city is one which uses a variety of different types of data collection sensor, gleaning data in order to manage…

  • 1 Minute Read #15 - Python string manipulation

    Previously we looked at some code, and discussed the use of + , and {0}. Let’s take a look at this: All is well when we…

  • 1 Minute Read #14 - Placeholders in Python strings

    Let’s dive right in with some Python strings here: We can see that, as previously, we are printing (“Hello world”) in…

Explore content categories