Categories
Python Programming

Branching

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

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.

Conclusion

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.

Categories
Python Programming

Programming Languages

Computer programming languages allow us to give instructions to a computer in a language the computer understands.

The diagram above shows how programming languages hide details from the user.

At the base of the diagram, we have the computer hardware.

The hardware refers to the part of the computer that we can see and touch.

To control a computer, we need to use a machine language. Each computer has it’s own machine language and programmers found it hectic to have to learn because it was in binary.

As time when on, the Assembly Language was developed. Assembly Language provided a symbolic representation of the machine code needed to program a given CPU architecture.

The real breakthrough in computer programming would start with the arrival of high level languages.

High level languages were closer to English and in addition to making the task of computer programming easier, they also opened the field to a lot more people.

The top four from that period in computing history are C, BASIC, FORTRAN and COBOL.

Applications running this languages are still in use today.

We will be learning the Python programming language.

According to the TIOBE Index as at January 2020, the Python programming language is the 3rd most popular programming language in the world.

Each language has it’s strength and weakness but for absolute beginners, I will give my reasons why recommend learning the Python programming language first.

Simplicity

When you are starting out on the journey of learning to code, you don’t want to deal with problems that occur while setting up your computer.

While learning Java at the University, one of the biggest issues we would run into was the setting of classpath.

The issue of classpath would stop many beginners dead in their tracks.

In Python, once you have installed the interpreter, you are ready to start programming.

In addition, the language is simple and readable. The commands in the language look like English.

Versatility

Python is a general purpose language that caters to every use case that you can imagine.

It might not beat a specialized language in its domain but it will allow you get started in any domain.

Large Community

The true secret weapon of the Python programming language is the community.

The versatility of the Python programming language is due to the libraries developed by the community.

Conclusion

As technology become more integrated with everyday life, learning how to program will become important in being a member of the society.

For absolute beginners, the Python programming language is great for starting because it is simple and easy to use.

Learn Python in One Week

If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.