Learn Python Relative to C++ (Theory of Relativity in action !!!)

A wise man once stated a great truth about this universe : “Every thing that exists in this world is Relative ,but Only one thing The speed of Light is Absolute.” Well enough of the physics….. (BTW that wise man’s name was Albert Hermann Einstein just FYI.)

As a programmer you may learn any number of programming languages but at last  in the end all comes down to what logic you use to put together that piece of code. It is easy to learn new language fast and easily if you have already master one of the languages. If you learn the new language in relation to the language you already know it becomes a child’s play. Now that google is at our service 24×7 you don’t remember syntax and structure just get the concept and you may just google rest of the things… So here we will give a shot to learn python. We will see it relative to how stuff works in C++. Well why C ? Since the most of modern OS are written in C, compilers/interpreters for modern hight-level languages are also written in C. Python is not an exception – its most popular/”tradition” implementation is called CPython and is written in C. That is not all but C is (in my opinion) one of the best language to start with. If you don’t know C but some other language(anything other than HTML!!!) it will be very fast to learn python from that here. So i will assume here that you already have basic (not necessarily too deep) knowledge of what variable types , classes, objects and their methods / attributes,pointers , function return types and parameters are. I will not waste time in explaining that fundamental knowledge.Now lets jump right in for a beautiful journey to see how python works and start with looking at what is new in python !!

Python an Introduction :

First great thing about Python is that there are no curly braces there ( Great lord Finally!!). It is a loosely typed language i.e. you don’t need to declare a variable type or a function return type. It is an interpreted language unlike C which is a compiled language. So in C you write a program compile it and than run that compiled file which is not the case with python. Python interpreter reads and at the same time executes the code line by line.Python has same set of logical and arithmetic operators as C has so nothing new or fancy there. Python code is more close to English like statements than C or many other languages.Lets start with some code here to see all this in action: C implementation of hello world:

int main()
/*main program starts here*/
//This is C comments
std::cout<<"Hello World";

Python Implementation:

print "Hello World"
#Don't look down this is just a comment !!!

Pretty and neat what you say?

Basics of variables and stuffs !!!

We have some basic types in C. They are Integer, Float, Character, Boolean ….. But you know what , That do not matter when you are using python because you NEVER declare a variable type (NEVER EVER Promise). In C we declare a variable as:

int foo_int = 20;
float foo_float = 2.45;
char foo_char = 'C';

In Python we just assign them:

foo_int = 20
foo_float = 2.45
foo_char = 'C'

(Did you notice the missing semi colons there? They are just not there!!! And never will be so cheer up its python) When you assign a variable the interpreter decides what to make out of it. According to its value assigned python interpreter automatically takes it as an int, long, char or anything else. Pure magic there.

Python List, Dictionary, Tuples:

In C we use arrays to represent a sequence of variables.It is something like:

int foo_array[50];

Here foo_array is name of a new integer sequence which may contain maximum 50 values. In python ……hmmm lets start with lists.

 new_list = [1,2,3,4]

here new_list is a list of 4 elements 1,2,3,4. so new_list[0] = 1 , new_list[1] = 2 and so on. So a list in python is elements separated by coma inside braces ([ ]). [ ] is considered an empty list. Now lets see wonders of python : you can never put a float in an integer array in C. But in python you can have what ever you need in that single list you made. A single list may have a int, many floats , also put some characters and when you are already at it you may also put a list inside a list!!! Example:

another_list = [12 , 20.4 , 'C' , new_list]

Here new_list is another list that we just declared. Tuples are just like lists but they are elements in round brackets ( ). The thing different about tuple from list is that they are immutable objects.Some of these objects like lists and dictionaries are mutable, meaning you can change their content without changing their identity. Other objects like strings and tuples … are objects that can not be changed. Lets put the immutable talk to a rest for now. So a tuple is just like new_tuple = (9 , 2 ,3). You may also just use new_tuple = 9,2,3 . So any comma separated data will be taken as a tuple in python..COOL. Now lets have a look at dictionary. Dictionary is a data type that has a key that points to a value. Hmmmm thats pretty much it !! But what are keys? well they are strings. But how are strings in python? Well string in python can be described with one word : Awesome. Every thing we have in python is an object. It may be basic integer ,a list , a tuple or string everything is an object. As we all know objects have there methods and this methods make python objects so easy to use and Awesome (Don’t think i forgot about dictionary!!). lets see an example:

first_name = "James"
last_name = "Bond"
name = first_name + " " + last_name
print name
name_len = len(name)
print name_len


James Bond

here first_name, last_name and name all are strings. name will contain “james Bond”. In the last line we used method len() that needs a string/List/Tuple as parameter and returns their length.(Thats some method overloading right there!!) There are plenty of string operations and methods available as default in python on which you can read at http://docs.python.org/2/library/string.html. You can also use string like lists in python i.e. first_name[0] = “J” , first_name[1] = “a” and so on.. {Note:String or character in python maybe written in single or double quotes unlike C where single quote on char and double on string is needed} Back to dictionary now : So where were we…Ohh ya keys are strings so you may write some thing like

new_dict = {'first' : 20,'second' : 12.4}
print new_dict['first']
print new_dict['second']



Here ‘first’ and ‘second’ are keys of dictionary new_dict. Key ‘first’ points to value 20. In above example we access each values of dictionary with there keys. Easy enough….
This are mainly all the basic compound types a beginner may need to write even a bit complex program in python. Of course there are more types such as sets , linked lists , many types of graphs and tree which may be supported by many other libraries for python.

Basics ends here and we start with branching looping and finally functions and classes. At last we will end it with modular and dynamic programming concepts in python.


One thought on “Learn Python Relative to C++ (Theory of Relativity in action !!!)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s