Python for machine learning – Control Structures

Prerequisite for this course

In my previous posts, I forgot to mention this, the prerequisite for learning python for machine learning will be a basic understanding of basic programming concepts from any programming language like C, C++, Java etc.

In this post we will see the most important part of a programming language control structure

  1. Selection
    1. if
    2. if…else
    3. if….elif…else
  2. Repetition
    1. while
    2. for

Selection

used for making decisions in a program that branches the program in 2 or more ways let us see the selection statements in python and one example program for each statement. It will have the same logic as the other programming languages like C and C++

if – statement

The first selection statement we are going to look at is if statement, the basic syntax is the same as the other programming languages. Let us see the syntax of if statement in python

if (expression):
    statements

if you see the syntax of python you will notice there is no parenthesis ” { ” instead of parenthesis python uses ” : ” and indentation if you see the syntax the statement is indented to let python know that we are inside the if statement.

Other than the simple change the logic of if statement works as the same as the other programming languages. now let us see an example program with if statement.

a = 10
if a==10:
    print('it is ten')

the output will be “it is ten

if…else – statement

The second selection statement is if…else statement if you look at the if…else statement we will see the syntax and basic program in python.

if (expression):
    statements
else:
    statements

Example program

a = 11
if a==10:
    print('it is ten')
else:
    print('it is not ten')

the output will be “it is not ten”

if…elif…else – statement

The third statement we are going to see is if…elif…else statement in python elseif is mentioned as elif that is why i am mentioning it as elif not as elseif. let us see the syntax of if…elif…else statement.

if (expression):
    statements
elif (expression):
    statements
else:
    statements

Now we will see the program.

a = 11
if a==10:
    print('it is ten')
elif a==11:
    print('it is eleven')
else:
    print('it is not ten')

the output will be “it is eleven”

Next we will see repetition statements

while – statement

The while statement functions as the same as in any other programming language we will see the syntax of while statement below.

while(expression):
    statements

We will see a example program

i = 1
while i < 6:
  print(i)
  i += 1

The above code will output ” 1 2 3 4 5 6 “.

for – statement

The next and last control statement we will see is for statement we will see the example below.

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

The output of the program is “apple banana cherry”

We will also see another basic program below.

for i in range(1,5):
    print(i)

The output will be ” 1 2 3 4″

That’s all for this post friends we will see Python collections in the next post

Happy Coding