Tuesday, 14 February 2017

Python Program to Cluster Data using the K-medoid method

Here, our given data-set is :


A1
A2
O1
2
6
O2
3
4
O3
3
8
O4
4
7
O5
6
2
O6
6
4
O7
7
3
O8
7
4
O9
8
5
O10
7
6

We implement the K-medoid algorithm for this dataset as follows:


import random

data_set = [[2,6],[3,4],[3,8],[4,7],[6,2],[6,4],[7,3],[7,4],[8,5],[7,6]]
median = [1,7]
m = [[3,4],[7,4]]
c1 = []
c2 = []
count = 0

def assign_mediod():
    median.append(random.randint(0,9))
    while True:
        a = random.randint(0,9)
        if a <> median[0]:
            median.append(a)
            break
    print(median)
    m.append(data_set[median[0]])
    m.append(data_set[median[1]])

def manhattan_distance(a,b):
    return abs(a[0]-b[0]) + abs(a[1]-b[1])

def create_cluster():
    print("data_set = ",data_set)
    for i in range(0,10):
        if i == median[0] or i == median[1]:
            continue
        else:
            if manhattan_distance(data_set[i],m[0]) < manhattan_distance(data_set[i],m[1]):
                c1.append(data_set[i])
            else:
                c2.append(data_set[i])

def AE(c1,c2,m):
    total = 0
    for o in c1:
        total = total + manhattan_distance(o,m[0])
    for o in c2:
        total = total + manhattan_distance(o,m[1])
    return total

def Check_for_Swap():
    global m
    global median
    global c1
    global c2
    global count
    count = count + 1
    swap = False
    tm = list(m)
    tc1 = list(c1)
    tc2 = list(c2)
    for i in range(0,len(c1)):
        temp = tm[0]
        tm[0] = tc1[i]
        tc1[i] = temp
        print("Cost Function for old - ",c1,",",c2,",",m," = ",AE(c1,c2,m))
        print("Cost Function for new - ",tc1,",",tc2,",",tm," = ",AE(tc1,tc2,tm))
        if (AE(tc1,tc2,tm) - AE(c1,c2,m)) < 0:
            m = list(tm)
            swap = True
            break
        print("c1 iteration  = ",i)
    for i in range(0,len(c2)):
        temp = tm[1]
        tm[1] = tc2[i]
        tc2[i] = temp
        print("Cost Function for old - ",c1,",",c2,",",m," = ",AE(c1,c2,m))
        print("Cost Function for new - ",tc1,",",tc2,",",tm," = ",AE(tc1,tc2,tm))
        if (AE(tc1,tc2,tm) - AE(c1,c2,m)) < 0:
            m = list(tm)
            swap = True
            break
        print("c2 iteration  = ",i)
    median = [data_set.index(m[0]), data_set.index(m[1])]
    print ("After iteration -",count)
    print ("medoids = ",m)
    print("medians = ",median)
    print("c1 = ",c1)
    print("c2 = ",c2)
    if swap == True:
        c1 = []
        c2 = []
        create_cluster()
        Check_for_Swap()
    else:
        print(count)
        print(m)
        print(median)
        print(c1)
        print(c2)
            
if __name__ == "__main__":
    assign_mediod()
    print(m)
    print(median)
    create_cluster()
    print(c1)
    print(c2)
    Check_for_Swap()

Let us suppose that the two medoids, the program randomly assigns are [O2,O7], then we get the following output

[[3, 4], [7, 4]]
[1, 7]
('data_set = ', [[2, 6], [3, 4], [3, 8], [4, 7], [6, 2], [6, 4], [7, 3], [7, 4], [8, 5], [7, 6]])
[[2, 6], [3, 8], [4, 7]]
[[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]]
('Cost Function for old - ', [[2, 6], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[3, 4], [7, 4]], ' = ', 20)
('Cost Function for new - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for old - ', [[2, 6], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 15)
('Cost Function for new - ', [[3, 4], [3, 8], [4, 7]], ',', [[7, 4], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [6, 2]], ' = ', 26)
('c2 iteration  = ', 0)
('Cost Function for old - ', [[2, 6], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 15)
('Cost Function for new - ', [[3, 4], [3, 8], [4, 7]], ',', [[7, 4], [6, 2], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [6, 4]], ' = ', 20)
('c2 iteration  = ', 1)
('Cost Function for old - ', [[2, 6], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 15)
('Cost Function for new - ', [[3, 4], [3, 8], [4, 7]], ',', [[7, 4], [6, 2], [6, 4], [8, 5], [7, 6]], ',', [[2, 6], [7, 3]], ' = ', 20)
('c2 iteration  = ', 2)
('Cost Function for old - ', [[2, 6], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 15)
('Cost Function for new - ', [[3, 4], [3, 8], [4, 7]], ',', [[7, 4], [6, 2], [6, 4], [7, 3], [7, 6]], ',', [[2, 6], [8, 5]], ' = ', 24)
('c2 iteration  = ', 3)
('Cost Function for old - ', [[2, 6], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 15)
('Cost Function for new - ', [[3, 4], [3, 8], [4, 7]], ',', [[7, 4], [6, 2], [6, 4], [7, 3], [8, 5]], ',', [[2, 6], [7, 6]], ' = ', 24)
('c2 iteration  = ', 4)
('After iteration -', 1)
('medoids = ', [[2, 6], [7, 4]])
('medians = ', [0, 7])
('c1 = ', [[2, 6], [3, 8], [4, 7]])
('c2 = ', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]])
('data_set = ', [[2, 6], [3, 4], [3, 8], [4, 7], [6, 2], [6, 4], [7, 3], [7, 4], [8, 5], [7, 6]])
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[3, 4], [7, 4]], ' = ', 20)
('c1 iteration  = ', 0)
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 4], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[3, 8], [7, 4]], ' = ', 18)
('c1 iteration  = ', 1)
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 4], [3, 8]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[4, 7], [7, 4]], ' = ', 18)
('c1 iteration  = ', 2)
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 4], [3, 8]], ',', [[7, 4], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[4, 7], [6, 2]], ' = ', 26)
('c2 iteration  = ', 0)
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 4], [3, 8]], ',', [[7, 4], [6, 2], [7, 3], [8, 5], [7, 6]], ',', [[4, 7], [6, 4]], ' = ', 20)
('c2 iteration  = ', 1)
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 4], [3, 8]], ',', [[7, 4], [6, 2], [6, 4], [8, 5], [7, 6]], ',', [[4, 7], [7, 3]], ' = ', 20)
('c2 iteration  = ', 2)
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 4], [3, 8]], ',', [[7, 4], [6, 2], [6, 4], [7, 3], [7, 6]], ',', [[4, 7], [8, 5]], ' = ', 24)
('c2 iteration  = ', 3)
('Cost Function for old - ', [[3, 4], [3, 8], [4, 7]], ',', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]], ',', [[2, 6], [7, 4]], ' = ', 18)
('Cost Function for new - ', [[2, 6], [3, 4], [3, 8]], ',', [[7, 4], [6, 2], [6, 4], [7, 3], [8, 5]], ',', [[4, 7], [7, 6]], ' = ', 24)
('c2 iteration  = ', 4)
('After iteration -', 2)
('medoids = ', [[2, 6], [7, 4]])
('medians = ', [0, 7])
('c1 = ', [[3, 4], [3, 8], [4, 7]])
('c2 = ', [[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]])
2
[[2, 6], [7, 4]]
[0, 7]
[[3, 4], [3, 8], [4, 7]]
[[6, 2], [6, 4], [7, 3], [8, 5], [7, 6]]
>>> 

Wednesday, 18 November 2015

What they Don't Teach you in C, but always appears in Exams - I

It's the same story, every year and every semester. Everyone teaches or studies lethargically throughout the semester and tries to rush through the entire syllabus in a few weeks. It's common; so common that a few very important topics and chapters are left untaught and it's the student, who is befuddled by the strange questions in the Question Paper. Phrases such as, 'From which chapters, were those 12 Mark Questions?' are commonplace.


                 Although, we sincerely hope that, it hasn't happened to you, but at the off-chance that it has, we will be discussing a few topics that we believe are indispensable and are usually not covered by your teachers:

Pointers in C


A pointer is a variable that contains the memory location of another variable. It is a variable that represents the location of a data item, such as a variable or an array element or a function.Let us take a look at the following code to understand how a pointer works: 

    int x = 8;
    int *ptr = &x;
    printf("x = %d",x);
    printf("\tptr = %d",*ptr); 


This code produces the following output:

    x = 8              ptr = 8

Pretty simple, isn't it? You just pass on  the memory address of a variable to variable whose name starts with an asterisk '*' and now, that variable becomes equal to the original variable!

Wrong!!!

That's not remotely what pointers do. A pointer takes the address of the variable and that's it. It's the asterisk '*', also called indirection operator, that fetches the data stored at that address. You don't believe me, try printing without '*', and you'll see!
Now, that we know what a pointer does, the next question that comes to mind is how do we declare and use it? And the answer to it is simple. The syntax for declaration of a pointer in C is 
<data_type> *<pointer_name>;  
e.g. int *x;  char *ch; float *f; etc.    
And now turning to how do we use it? To use a pointer, you must pass the address of alegitimate variable (not a pointer) of the same data type to the pointer. e.g. the statements

        float f = 3.14;
    char *ch = &f; 


are absolutely illegal! Why you ask? Because the size of a pointer is 1 byte (ANSI C 1989), irrespective of it's data type. If an integer is stored in memory at the addresses 5001 and 5002, then the pointer only stores the address 5001, for it can store only one byte. What the data type does is tells the indirection operator how many bytes it needs to read in order to fetch the data stored. So, the statement *pointer tells the compiler that it needs to read the data at 5001 and 5002 in order to read the integer stored. Now. we'll move onto our next topic:

Pointer Arithmetic


The above table describes the operations that can be performed on pointers (without the indirection operator '*', for with the indirection operators, the arithmetic is on the data rather than address)

We'll come to pointer arithmetic later. Let us take a look at how regular arithmetic can be performed on variables, via pointers, first!

So, below is a C Program to add two integers using pointers:

#include <stdio.h>

main()
{
    int num1, num2, sum;
    int *ptr1 = &num1, *ptr2 = &num2, *psum = &sum;;
    scanf("%d %d",ptr1,ptr2); /*why ptr1 and not &ptr1?     Because ptr1 = &num1, and that's what we need to pass       with scanf*/
    *psum = *ptr1 +*ptr2;
    printf("\nsum = %d",*psum);
    return 0;
} 

Output : 4 5
         sum = 9

Let us see some more examples:

1. Code to find the area of a circle 

#include <stdio.h>

main()
{
    int rad, area;
    int *prad = &rad, *parea = &area;
    scanf("%d",prad);
    *parea = 3.14 * (*prad) * (*prad);
    //why (*prad), because * is also the multiplication operator
    //so, you need to case the indirection operator and pointer in parenthesis(brackets)
    printf("\narea = %d",*parea);
    return 0;

}

Output : 7
         area = 153.86

2. Code to find the largest of three numbers

#include <stdio.h>

main()
{
    int num1, num2, num3;
    int *p1 = &num1, *p2 = &num2, *p3 = &num3;
    printf("\nEnter three numbers : \t");
    scanf("%d %d %d",p1,p2,p3);
    if(*p1 > *p2 && *p1 > *p3)
        printf("\n %d is the largest",*p1);
    else if (*p2 > *p3)
            printf("\n%d is largest",*p2);
    else
        printf("%d is largest",*p3);
    return 0;
}

Output:  Enter three numbers :  2 14 5
         14 is largest

3. A code to print all the even numbers between two bounds (m and n)

#include <stdio.h>

main()
{
    int m, n, i;
    int *p1 = &m, *p2 = &n;
    printf("\nEnter the upper and lower bounds :\t");
    scanf("%d %d",p1,p2);
    for(i = *p1; i < *p2; i++)
    {
        if(i % 2 == 0)
            printf("%d\t",i);
    }
    return 0;

}

Output : Enter the upper and lower bounds : 4 14
         4  6  8  10  12

Null Pointer

A null pointer is a special pointer value that doesn't point anywhere i.e a null pointer doesn't carry (or points to) any valid memory address. To declare a null pointer, we may use the Preprocessor  constant NULL defined in the header files <stdio.h>, <stdlib.h> and <string.h>.

It is used in evaluating the functions or structures that return a NULL value i.e No return when the operation fails. e.g. 
Mathematical underflow in Queues is evaluated by using null pointers, as:

if(front == NULL)
{
    printf("\nUndeflow");
    exit(0);
}

Generic Pointers

A special type of pointer, of the data type void, that can point to any data type is called a generic pointer. It is declared like a normal pointer, but with the keyword void. e.g.  void *ptr

But, since C doesn't allow variables of void data type, we need to type cast a void pointer, every time it is used.

Note : The type casting is to be done with pointers of intended types

Let's take an example to understand it better:

#include <stdio.h>

main()
{
    int x = 65;
    char ch = 'P';
    void *gp;
    gp = &x;
    printf("\nValue of void pointer with int = %d"*            (int*)gp);
    gp = &ch;
    printf("\nValue of void pointer with char = %c",*          (char*)gp);
    return 0;

}

Output : Value of void pointer with int = 65
         Value of void pointer with char = P

Now, if some of you are wondering, what the hell did I do before passing gp (void pointer) to printf(), then you need to read the Note above, written in bold, again. It was said, that you need to typecast a void pointer with a pointer of the data type, whose address is passed onto it. In case, all the asterisks are confusing you, here's a break down -
The first asterisk is the indirection operator, and the asterisk in the bracket, after the keyword int represents a pointer. In case, you don't know what type casting is, you are gonna be in big trouble at your exams.

The use of generic pointers is widely discouraged, for they are not type checked by the compiler, and cause some serious incompatibility and run time errors like : _out_of_range, _too_long and _incor_type. And still, they are a part of most university syllabuses. You'll almost never use void pointers, but it is presumed that it will be good for you to know that you can turn to them if you want to point a different data types at different times.

Why use pointers?

From all we have seen up to this point, it seems as though all we have accomplished with pointers is 'Aliasing'. But wait, haven't we done that already using something like this : int x = &y ?

Yes we have, and also that's not the job of pointers, or atleast that's not what pointers are for anyways. Just ask yourself this question:

Your friend who moved to a different city is back in town, after a long time and doesn't remember the city as well as he used to before. He also wants to meet everyone at their houses. So what do you think would be easy, bringing everyone's house to one place OR giving him their addresses? 

This concept may seem a little foggy at this moment but with computers, maintaining actual copies of all the variables in large data structures, is equivalent to, copying someone's entire house : it's interiors, furniture, paint and everything, in your house. it also saves a ton of memory, that may prove to be very crucial at run-time. As we move onto more complex, user-defined data types, pointers become an indispensable part of our code. You'll find several applications for pointers (that you'll need to memorize and regurgitate in your exams) in your books; we are not going to discuss that.

That's it for this first part in 

What they Don't Teach you in C, but  always appears in Exams 

We'll be with our second part tomorrow. where we'll discuss Pointers and Arrays, Matrix Operations using Pointers and Structures.





Sunday, 26 July 2015

Insights into Programming - 1


published : 05/06/2013

This is the first article in our series, "Insights into Programming". This page is meant to enhance your understanding of the laws and mechanisms that lay behind the functioning of a programming language. This series will serve as an aide to "Baby steps into Programming". As we have done in our other series', we'll discuss three programming languages here also, namely C, C++ and Java.

Though, the fundamental laws that govern all the programming languages are similar but there are slight differences that exist and give every programming language it's unique character.

First of all let us take a look at programming terminology, it starts with these terms:
1) Keywords : These are a set or collection of words that convey a special meaning to the compiler. These are reserved for use by the compiler and whilst using IDEs, they are highlighted by the use of a specific color (white in Turbo C++ 3.0, navy blue in Dev C++ or NetBeans, red in C9, etc.) or emboldened. e.g Take the world if in any programming language. It is a conditional statement and is used to test a condition, say if( 3 < 5) and execute the statements in it's block, that is bounded a pair of braces { }.

2) Identifiers : It is the name that we give to different elements of a program, such as variables, functions, etc.

3)Data Types : Programming languages support different kind of data elements to hold different kind of values in a program. The basic data types that can be found in almost any modern programming language are:

1) integer, used with the keyword int. It stores basically whole numbers, positive and negative. This might sound a bit ambiguous, but it is true. An integer in programming languages can carry +ve and  -ve values but they can only be whole numbers. e.g.
                 int x = 5;
                 y = x / 4;
then the value of y will be 1. Because integers, in Programming languages exclude any value after the decimal point and that is why 5 / 4 equates to 1, not 1.25.
The range of values that an integer variables can carry vary from language to language, but as a general measure, it is taken to be from -32,768 to 32,767.

The syntax or the rule for declaring an integer variable is:
          int <variable_name>;
Note: In modern programming languages, every line of code needs to be terminated by termination punctuation, or a semicolon (;). 

2)  character, used with the keyword char. It stores a single character such as 'a', '8' or '&'. In C, the char doesn't actually store the character assigned to it, but the number or code, it has in the codification paradigm that it's compiler follows. In it's early editions, C stored a character's corresponding code in ASCII (American standard code for information interchange). Whereas, in C++ or java, a character variable actually stores the character. We'll discuss this in much detail in subsequent articles. 

The syntax for declaring a character variable is :   
             char <variable_name>;

3) floating point value or real number, used with the keyword float, is used to store the real numbers, or those which have fractional values. The range of values it can support also varies from from language to language, But as a general measure it does  from 3.4E-38 to 3.4E+38.  Well this values are in hexadecimal, but you can know that it can support values even after 10 places right to the decimal. Again, returning to the syntax:
            float <variable_name>;
Again, some distinctions exist as to how values can be assigned to floating point variables. In C or C++ they can be assigned values just like any other normal variable, the difference being, they can hold fractional values, like float x = 3.14. But in java, the assignment needs to end with an 'f' at the tail of the fractional value, otherwise, it is taken to be a long variable. In java, the same assignment needs to be somewhat like this, 
                                                   float x = 3.14f;

These are all the basic data types that can be found in almost any programming language. Some extensions to these, like a long integer that expands the range of int, or a double that expands the range of float, or String, that increases the capacity of a char, in the sense that a string can hold multiple characters, e.g. String x can be equal to a name, say"Sarah".
                                                    i.e. String x = "Sarah";
But these extensions are language specific, and that is why we'll discuss them later, when we dig deep into specific languages.

4) Operators : Operators are crucial parts of any programming language. they enable us to perform various operations on different data items. e.g. the plus '+' operator, lets us add two integral or floating point values, like 
                                          y = z + x;
And here the assignment operator '=', helps us store the resultant vale of that operation in the variable y.

The basic types of operators, that are generic to all programming languages are Binary Operators i.e which perform an operation on two variables, or operands. These binary operators can be further classified into several sub-classes, as :

i) Arithmetic Operators: the operators that perform arithmetic operations are called arithmetic operators. These include =, *, /, +, -, %. We'll dig into them, using examples. Let us take two integer variables a  and b.

When we write, a = 9 and b = 3, it's obvious enough, they carry the values, 9 and 3 respectively.
Now if we perform a * b, the result will be 27.   a * b = 27
Similarly,   a / b = 3, a + b = 12, a - b = 6. These should be obvious enough to you.

Now, we'll move to the new Modulus operator %. It looks like it might calculate the percentage of some value, over the other, but what it actually does, is stores the value of the remainder from the division of two numbers. Let us take an example to understand it better.

Let a = 5 and b = 2. Now if we are to divide these two numbers, the long division process will yield this result. 

The remainder is left 1, that is if we are not cross over to decimals. And that is the point of the modulus operator, while using that, the division must remain in whole numbers. So if we perform 
     c = a % b, c will be equal to 1, that is, the remainder from the division of the two operands.
Now, you might think that this is pretty much as silly operator, and what purpose will it serve. But as we move on, you'll see that it has a lot importance in a programming environment.

ii) Relational Operators :
As the name suggests, a relational operator or a comparison operator, establishes the relation between two variables or operands. Like, 9 is greater than 6 or 5 is less than or equal to 7, and in doing so compares them. Basically, there are four relational operators, which are:

1) <  (Less than)
2) > (Greater than)
3) >= (Less than equal to)
4) <= (Greater than equal to)

Though I think that their use is pretty obvious, but still, some example would be dandy, wouldn't they?

Suppose a and b are two variables and carry the values 9 and 12 respectively, so:
1) So, a is less than (<) b, right, so its True. And true or false are the results we will take on relational operators.
2) a b, is it? False
3) a >= b ? True. Why? Because 9 is either less than 12 or equal to it. Satisfies one of the two conditions, that's why the result is True.
4) a <= b? False, why? Because it satisfies neither of the conditions. 9 is neither greater than 12 nor equal to it, so the result comes out to be true

iii) Equality Operators :
There are two equality operators, that can be used to check whether two variables oroperands are equal to each other. The Equality operators are:

1) == (Equal to)
2) != (Not Equal to)

Again, while discussing Equality operators, we get a True or False result.
So, if we have, again two variables a and b, carrying the values 5 and 5 respectively, then

1) a == b is True
2) a != b is False

iv) Logical Operators:
The Logical operators,w ell, they let us perform logical operations, which are to compute, say whether something is true or something else is true, or both are true or neither. It's all sounding rather foggy right now, isn't it? Let us look at an example to understand it better.

Say we have three variables ab and c. Suppose we are to find if a is greater than both band c, what will we do? There is a method in which we check if a is greater than b and if a is greater than c and if both the comparisons are true, then we say that a is greater than both b and c.

We can do the same logical comparison in programming languages using the And Operator (&&).
The coding equivalent of our thought experiment is

if (a > b && a > c) == True.

Also, we have another operator to check whether a is greater than either or c, which is the Or Operator(||) i.e the button right above Enter on your keyboard. It works this way:

if (a > b || a > c) == True.
The only disadvantage of this operator is that if the first condition turns out to be True, then then it skips the second condition. And it could never tell if the second condition is true as well. So, using the Or operator, you can never know if a > b or a > c or a > b,c.

Barring the Or and And operators, we have another Logical operator called Not (!). What it does is, checks whether a variable is not equal to the test condition and return True orFalse. Let us take an example,: Say we are to allow access to a particular website, except for an IP address 102.345.1.1. What will we do. We can do something like this

if( ! 102.345.1.1) == True
      pass.

The above statement is what we call a pseudo code, where we mark down, what is need to be done, in order to perform the desired operation. Blocking uses from a real website, is somewhat different and more complicated than this, but this is just to give you a general Idea.

These are the basic kinds of operators that can be found in almost any Modern Programming Language. There are more categories that can be found in particular languages, but this pretty much gives the gist. We'll discuss the language specific operators in different languages, when we look at them individually.

That's all for this article. Everything taught today will be used to code in the next edition of Baby Steps into programming.

In the next edition we'll talk about escape sequences, endline streams and tools to format your output and much more.