Monday, April 21, 2008

Comparing the language

Again: There is a book freely available online named Dive into Python which I really recommend. I have just downloaded it and started to read, so I will post some differences that I have already seen from java and .Net.

Language Type

In C# or java we need to specify type at the declaration for variables(This book calls this statically typed language) and once this variables has that type, it cannot be used like other type, unless with a explicit convert(called strongly typed language).

In Python the types are not specified at the declaration, but only in the assign of a value(dynamically typed language), but once this variable is assigned, this variable has to be used like that type or can be explicit converted. So Python is dynamically strongly typed language. In Python we don't declare a variable, they are created when they are assigned.

I used to be a VB programmer and when I have leaned C# I get used to statically typed language and I liked it. Python do it in a different way, at first I didn't like this of not declaring a type of a variable, I would rather the statically typed than dynamically typed. A friend of mine, Gustavo Ferreira Moreira, who have already worked with Python and now is learning C#, prefer define the type at the declaring time too.

Indenting Code

This is really a cool stuff. Code blocks are defined by their indentation. Like C# and Java are defined by curly braces "{}". A code is good to read when they are indented correctly, and Python needs that.

Example(Python):
number = 4
if number > 3:
print 'number greater than 3'
print 'number', number

Same Example in C#:
int number = 4;
if (number > 3)
{
Console.WriteLine("number greater than 3");
Console.WriteLine("number" + number.ToString());
}

Python is case-sensitive
Just like C# and Java.

Namespaces
Python uses namespaces too!

Other references:
http://www.python.org/doc/essays/styleguide.html

No comments: