Archive for the ‘Programming Language’ Category

Introduction to R

R is not for “Rishu” as I made it out to be when I heard of this data mining tool. Initially, I assumed R to be yet another tool as Pentaho. But my assumptions fall apart when I clicked on which says up front its definition:

“R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment”

So here it is R is a language. R is more of a data mining tool as it seems to me. Well if you ever worked on MATLAB, the format and syntax would look the same. What makes R special is its ability to handle complex mathematical queries and computation simple and easier. Creating graphs and plots are never too easy. For example, let take the below image (Screen shot from code I wrote):


The code is pretty simple. I have assigned certain values (which are in vector format) into two separate variables – “a” and “‘b”. The value of variable “b” is the square of variable “a”. And as you can see the computation of mathematical function is done by using simple commands. I calculated the “MEAN” and “VARIANCE” of the variable b using two simple commands – mean (b) and var (b). The variable “c_lm” shows the linear regression model of variable b and a.

Well there are loads more. People have gone ahead and created something like “Google Trends”.  Though Google has its own GUI built over R, but nothing is stopping us from creating one either.

Sources:; Google Trends


Programming Language C : Part 3

April 27, 2009 2 comments

Last day, I’d ended on the basic structure of the C Programming Language. Today, we shall start to do real programming. It is really fun.

Ok. Lets do a small program to print your name:


This program will give you output like: c2

Now, we shall move to DATA Types.

As we know, there are many types of data in our real world. For example, the Integers, the Floating point numbers, Characters etc.

In case of C, there also exist all of them. In case, we want to use them, we need to “INITIALIZE” them to the Compiler. The method is like this:

c3Here we can see, “int a” declares an integer called ‘a’ without any value. But, “int b = 5” declares an integer called ‘b’ with a value 5 assigned in it. Similarly for floating point numbers, ‘float’ is used and for characters, “char” is used. All these ‘int’ or ‘float’ or ‘char’ are called “keywords” in C.

We shall look further more keywords in C:


All these keywords have their own meanings and jobs to do. We shall look them further, while we will need them in future.

Now, there is a question. I know, I can use int as a keyword to mean the integers. But, how long that integer can be? Int x = 1234567891011121314151617…..??? There should be a limit of this.

char     => a single byte, capable of holding one character in the local character set

int        => an integer, typically reflecting the natural size of integers on the host machine

float     => single-precision floating point

double =>  double-precision floating point

In addition, there are a number of qualifiers that can be applied to these basic types. short

and long apply to integers:

short int sh;

long int counter;

The word int can be omitted in such declarations, and typically it is. The intent is that short and long should provide different lengths of integers where practical; int will normally be the natural size for a particular machine. short is often 16 bits long, and int either 16 or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the the restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer than long.  The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers are always positive or zero, and obey the laws of arithmetic modulo 2n, where n is the number of bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values between 0 and 255, while  signed chars have values between -128 and 127 (in a two’s complement machine.) Whether plain  chars are signed or unsigned is machine-dependent, but printable characters are always positive.  The type long double specifies extended-precision floating point. As with integers, the sizes of floating-point objects are implementation-defined; float, double and long double could represent one, two or three distinct sizes.

Python: Bult-in Functions for Numeric types

April 24, 2009 2 comments

Python has some built-in functions for working with numeric types. Some of them are described below: ———–

Function Name


Code Example


The abs(x) function takes the absolute value of any integer, long integer or floating point number.

When applied to a complex number, the function returns the magnitude of the number, which is distance from that point to the origin in the complex plane.

>>> abs(-5.0)


>>> abs(-2)


>>> abs(5+2j)



The coerce(x,y) function applies numeric conversion rules(described below the table) to two numbers and return them as tuple.






The functions divmod(a,b) performs long division on two numbers and returns the quotient and remainder.

>>> divmod(5,2)

(2, 1)

>>> divmod(5.5,2.5)

(2.0, 0.5)


The functions pow(x,y[,z]) performs power operation.

As usual, Python coerces the two numbers to a common type if needed. If the resulting type can’t express the correct result then python will show an error message.

An optional third argument to pow specifies the modulo operation to perform on the result.

>>> pow(2,3)


>>> pow(2,-1)


>>> pow(2,5,10)


For all the built-in functions and there definitions, please visit this link:

Numeric Conversion Rule:

  1. If one of the numbers is a complex number then convert the other two a complex number too.
  2. If one of the numbers is floating point number then convert the other to floating point.
  3. If one of the numbers is a long integer then convert the other to a long integer. (Not applicable for Python 3.1)
  4. No previous rules applied, so both are integers, and Python leaves them unchanged.

C Programming: Part 2

April 16, 2009 1 comment

Hello friends! After a long time, I am back with the Programming Language C.

In regard of the last post and the comments posted by our readers, I must insist you trying all of these in Linux / UNIX also. In Linux / UNIX, you don’t need any 3rd party software line ‘Turbo C’ in Windows. In the OS itself, the ‘gcc’ (GNU’s C Compiler) compiler is preloaded. You just need to call the ‘gcc’ and then compile/run the program.

Now, we will go into the C programming!

Before everything, we will write a C program first.

Windows: Open Turbo C è write this code.

Linux / UNIX: Open terminal ècreate ‘hello.c’ in the VI editor è write this code.

#include <stdio.h>

#include <conio.h>

int main()


printf(“ Hello World.”);


return 0;


Now, we will look, what are these lines signify:

Line 1 and 2: These are called “Pre-Processor Directives”. We include these “.h” files rather “header files” in our program to use some of their properties. These properties are called “Functions”. Here, in line 5, 6 and 7, you will see some “printf”, “getch” and “return”…these are those Functions. The “printf” and “return” functions are from the header file “stdio.h” (the ‘stdio’ means STANDARD INPUT OUTPUT and ‘conio’ means CONSOLE INPUT OUTPUT) and the “getch” is from the header file “conio.h”. Remember, other than these Functions, we ourselves can also write Functions as we wish.

Line 3: This line indicates the ‘Return type’ and ‘Name’ of a user written function (as I’d said in last paragraph). Now, to know what is ‘return type’ needs some more knowledge in C. So, we will discuss that later. Now, ‘name’. Here as we can see, after the ‘return type’ called ‘int’, there is the name of this function “main”. Now, a question arises, why ‘main’? The reason is, in any C program, there may exist more than 1 function and all of them are controlled by 1 function. Now, to get rid of the question, among those functions, which one is the 1st one, that controls other functions? The answer is: the main function. It’s like the main door of a building. After traversing through it, you can go anywhere in the building. So, here no other function exists, hence, the main door is the ‘main’ function.

Line 4 and 8: You can see these are nothing but, an opening parenthesis and a closing parenthesis. This opening means the starting of any function and closing means the closing of the same.

Line 5, 6 and 7: “printf” is a function, whose job is to print something. Inside the two brackets, there are two upper commas. Inside those ‘upper commas’, if you write some thing, that will be shown to you in the monitor when that program will run. Similarly, “getch” and “return” has some jobs to do. These jobs will be cleared to us when we will have some more knowledges in C.

One more thing, if you have noticed, you can see there is a semicolon (;) after every line inside the main function. This semicolon indicates the ‘End of the Line’.

Now, to compile:

Windows: (in Turbo C) F2 (to save the program) è Alt + F9.

Linux / UNIX: (after saving the program in VI editor) gcc -c -a hello.c

Now, to Run:

Windows: (after compilation) Ctrl + F9.

Linux / UNIX: (after compilation) ./hello.c

Python: Variables, Numeric Types and Operators

April 8, 2009 Leave a comment

Python offers four different kinds of numbers with which you can work:integers, long numbers (or longs), floating-point numbers (or floats), and imaginary/complex numbers.

We are familiar with Integer, Long or Floating point Numbers. The new type is Imaginary/Complex number. The imaginary number behaves very much like a float, except that it cannot be mixed with a float. When you see an imaginary number, it will have the letter j trailing it.

Note: Python 2 had separate int and long types for non-floating-point numbers. An int could not be any larger than sys.maxint, which varied by platform. Longs were defined by appending an L to the end of the number, and they could be, well, longer than ints. In Python 3, there is only one integer type, called int, which mostly behaves like the long type in Python 2. Since there are no longer two types, there is no need for special syntax to distinguish them.

To determine the type of a number, you can use a special function that is built into Python, called type. When you use type, Python will tell you what kind of data you’re looking at. Let’s try this with a few examples.

code5In the above examples, we use type() function to determine the type of 2 different numbers: integer, float and complex.

Format Specifiers:

Int: %d

Float: %f

Exponential (For large numbers): %E

Manipulating the Numeric Datatypes:

We can use most of the Python’s operators when working with Numeric data types. the following table lists the operators and how they behavewith numeric types:


Next Post: Useful functions in Python

Code practice – String usage in .NET

April 6, 2009 3 comments

String in .Net, is an Immutable object (Object cannot be modifed). Hence memory allocated for a string remains even if you alter/modify the string through code, forcing the CLR to create new space for the modified string.

//Code Illustration in C#.Net

string str = ""; //memory created for str and value "" is stored
str = "String is Immutable" //new memory space is allocated for str
                            //and value is stored
str += str  + " in .NET" //Again, new memory allocation

This means more string operation you do with, more memory is occupied, though its with the same string variable. Better approach would be to use StringBuilder to do all appends and get the final output as StringBuilder class provides an efficient way to append. And its mutable.

Also string str = “” and string str = String.Empty is not the same in terms of memory allocation. the first creats an object in the memory and the latter does not. Hence prefer the latter

Avoid ToLower() and ToUpper() in code (Remember strings are immutable)

–Writing code is easy, writing good code is not.  Learn, Implement, Share–

Python: Strings

April 6, 2009 Leave a comment

Hello everyone. I am back with Python again. Let’s try to learn it from the scratch. After reading this post, you will be understood about Strings in Python.

Strings are the basic unit of text in Python. Unlike some other programming languages, a single letter is represented as a one-letter string. Let’s try to understand it using some example code:code1

How It Works:

If you use different quotes, they may look different to you; to the Python interpreter; however all of them can be used in the same situations and are very similar.

Three different types of quotes are used in Python. First, there are the single and double quotes.

Python has one more special way of constructing strings, one that will almost always avoid the entire issue of requiring an escape character and will let you put in new lines as well: the triple quote. If you ever use a string enclosed in three quotes in a row—either single or double quotes, but all three have to be the same kind—then you do not have to worry about escaping any single instance of a single or double quote. Until Python sees three of the same quotes in a row, it won’t consider the string ended, and it can save you the need to use escape characters in some situations. See the 3rd line of code in the above figure.

Putting More than One Strings together:

To put more than one string together you can use ‘+’ operator. Suppose you want to put the two strings “Hello” and “Everyone” together. So use the following code:–


Another way to specify strings is to use a format specifier. It works by putting in a special sequence of characters that Python will interpret as a placeholder for a value that will be provided by you.

code3That %s is the format specifier for a string. Several other specifiers are there. Each specifier acts as a placeholder for that type in the string; and after the string, the % sign outside of the string indicates that after it, all of the values to be inserted into the format specifier will be presented there to be used in the string.

Displaying Strings with Print:

For displaying text, a special feature is built into useful languages, one that helps the programmer display information to users. The basic way to do this in Python is by using the print function:

code4print is a function—a special name that you can put in your programs that will perform one or more tasks behind the scenes. Normally, you don’t have to worry about how it happens. In this case, the print function is an example of a built-in function, which is a function included as a part of Python, as opposed to a function that you or another programmer has written. The print function performs output—that is, it presents something to the user using a mechanism that they can see, such as a terminal, a window, a printer, or perhaps another device (such as a scrolling LED display).

In the next post I will discuss about the Numbers and Operators in Python.