4. Loops and Statements

These notes are provided to demonstrate how to get started coding loops and conditional statements in Python. The general name for this topic is control flow.

In the earliest days of computing, one of the first things we realized, is that we would need to execute different branches of code depending on Boolean conditions.

If it is raining, switch on the windscreen wipers!

We might use the console or Notepad++ to write a quick script, but for deliberate work, we are always going to use a proper editor. For writing commercial code, I use PyCharm. However, for these sessions, we are going to move to Visual Studio Code.

During this walkthrough, I am going to introduce important concepts in Python; colons and indentation. Look out for this, and make sure you fully understand it. You really cannot proceed without this basic knowledge.

Before you begin. create a directory to keep example files in and you are ready to code.

This could be on your OneDrive, in these examples, I am using OneDrive\Python\Exercises_04

To control the flow of execution in a program, we normally have a condition associated with a block of code. There are three main types of conditional statements we will deal with.

  • If, Elif and Else

  • Loops

  • While