Python Programming

IF Statements

IF statements are branching statements used for conditional execution of a program in the Python programming language.

IF statements execute when a programmer defined condition is true. The condition allows the IF statement to be used for decision making.

Decision making is required when we want to execute a piece of code if certain conditions are met.

IF Statement

IF Statement Flowchart

The standalone IF statement checks is a condition is true and executes the statements under it.

Standalone IF Statement

The above standalone IF statement makes no provision for when the condition is false.

IF statements can also be placed one after the other to execute in sequence as shown below:

Combined IF Statements

ELIF Statement

ELIF Statement Flowchart

ELIF allows you to chain multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to true.

It is the equivalent of chaining IF statements in sequence. An example of this is show below:

Compound IF Statements

Note that the first IF statement is the IF statement that you are used to. The ELIF statements follows it.

ELSE Statement

ELSE Statement Flowchart

ELSE statements go with the IF statement. They are used for when the conditions that we are testing for fails.

The can be used with the standalone IF statement as shown below:

ELSE Statement

The ELSE statement can also be used with the compound IF statements as shown below:

Compound ELSE Statement

In the above program, we would only get to the ELSE block when the other conditions fail.

Python Programming


The default mode of execution of a program is from top to bottom. However in the course of the execution of a program, the Python interpreter comes across some statements that change the default mode of execution.

Such statements are called a branch. A branch is an instruction in a computer program that causes a computer to begin executing a different instruction sequence and thus deviate from its default behaviour of executing instructions in order.

Branching is implemented as a series of control statements in high level programming languages. In the Python programming language, these include:

  • IF Statement
  • WHILE Loop
  • FOR Loop

IF Statement

IF Statement
If-Then-Else flow diagram

The IF Statement works by evaluating a programmer specified boolean condition and checking if it evaluates to true or false.

If the condition evaluates to true, certain actions are carried out. An evaluation to false means that alternate actions are carried out.


While loop flow diagram

A loop ensures that code is repeated. However in programming when you want ensure that you loop an infinite number of times, you use a WHILE loop.

The WHILE loop consists of a block of code and a condition/expression. The condition/expression is evaluated, and if the condition/expression is true, the code within all of their following in the block is executed. This repeats until the condition/expression becomes false.

In the Python programming language, the WHILE loop has an ELSE branch just like the IF statement.

FOR Loop

For loop flow diagram

When you want to loop a fixed number of times in the Python programming language, you use a FOR loop.

A FOR loop has two parts: a header specifying the iteration, and a body which is executed once per iteration. The header often declares an explicit loop counter or loop variable, which allows the body to know which iteration is being executed. FOR loops are typically used when the number of iterations is known before entering the loop.


Branching statements allow the flow of execution to jump to a different part of the program.

Mastering them is important in the journey of learning how to program.

Python Programming

Euclid’s Algorithm I

Euclid’s Algorithm allows you to find the HCF of two numbers. The steps of the algorithm are given below:

  1. Ask the user for 2 numbers
  2. Test which one is bigger
  3. Make the bigger number m and the smaller number n
  4. Set r to be the remainder when m is divided by n
  5. Set n to m
  6. Set r to n
  7. If r is 0, the HCF is n. Stop
  8. Else go back to step 5

This article will cover steps 1 to 3. The first step is to create a script and name it

To write the code for step 1, enter the code shown below into the script:

# Ask the user for the first number
first_number = int(input("Please enter the first number: "))

# Ask the user for the second number
second_number = int(input("Please enter the second number: "))

To test which of the input numbers is bigger, we will use the greater than symbol. We will ignore other cases. The code to do this is shown below:

# Test if the second number is bigger than the first number
if (second_number > first_number):
    # Swap if the first number is greater than the second number
    temp = second_number
    second_number = first_number
    first_number = temp

# Print out to check the swap
print(first_number, second_number)

Now the program can receive input and swap them correctly. Below is an image of a running program.

Sample Program Run

Python Programming


When writing code in the Python programming language, you use comments to isolate portions of the program that you don’t want the interpreter to run.

Comments are used to:

  • Document a program
  • Isolate a portion of a program during debugging
  • Make code more understandable to the programmer

Creating a Comment

To create a comment in the Python programming language, the # symbol is used.

# can be used above a line of code or inline.

#This is a comment
print("Hello, World!")

print("Hello, World!") #This is a comment

Multiline Comments

In the Python programming language, multiline comments don’t exists. However, you can use the docstring which are made using the “”””””.

This is a comment
written in
more than just one line
print("Hello, World!")

The docstring is specifically meant for documenting a method or function in the Python programming langauge.


Comments are used in programming languages to document, debug and make code understandable. They can be single line comments or multiline comments.

Python Programming

Arithmetic Operators

There are seven basic arithmetic operators in the Python programming language.

They are:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Integer Division
  6. Exponentiation
  7. Modulus


This is represented by the + symbol. It adds two numbers together and gives their sum.


This is represented by the – operator. It takes the value on the right from the value on the left to give the difference ot the two values.


This is represented by the * operator. It multiplies the value on the left with the value on the right to give a product of the two.


This is represented by the / operator. It divides the value on the left with the value on the right to give the quotient.

Integer Division

This is a special form of division represented by //. It gives the whole number value of the quotient.


This is represented by the ** operator. It gives the result of a value raised to another value.


This is the remainder of division of one number by another number. It is used to test if the number on the right is a factor of the number of the left.


The above operators are how arithmetic is done in the Python programming language on a basic level.