Friday, August 16, 2013

Syntax wise comparison between python and other languages


Python Differences (comparison based on the syntax)


Python Differences For the most part, Python behaves much like PHP, Perl, Ruby and other languages you may be familiar with. However, there are some important and noteworthy differences. Perhaps the most obvious (and Python aficionados would argue, important) is that line breaks and indentions in your code have meaning in Python.
Whereas PHP and others use a semicolon or other mark to designate the end of a line, Python sees all new lines as, well, new lines. Also where PHP and others use curly brackets to enclose code blocks, Python merely wants the code indented.

 Python forces you to properly indent code blocks and eschews end-of-line punctuation like PHP’s semicolons in favor of simple line breaks. This has some import consequences. First and foremost, it makes Python code much easier to read. The structure of a Python script is a snap to figure out at first glance.

Even if you have no idea what the code is doing, you can tell how it does it just by glancing at it. Python forces neat, well structured code, but it also forces you pay more attention to how you write your code. Consider the following two code snippets, which do very different things:

In the first code block our return statement is indented, and therefore within the if statement. In the second code block we didn’t indent the return statement so that function always returns true, regardless of our if test. Technically, that second function would generate an error because Python expects an indented block after the colon.


You'll find that if you're writing code in any language other than Lisp or some other non-intuitive language (not claiming that C is, but in comparison, certainly) language, the syntax isn't too much of a variable. There are some differences, but nothing that should be considered too abstract, in my opinion. In C, you have to worry about things like pointers and whatnot which is a pain, but I think that more-so straddles the line of memory management than syntax if anything. You mostly have to worry about the differences in usages of semicolons and whatnot.

You'll find that Python is like writing English sentences, or at least writing pseudocode with constraints, which makes it significantly easier than C. Additionally, I wouldn't consider jQuery a language on its own. It's an extension of a language though, just as STL might be considered a particular type of extension to C++, I guess.


Spaces versus Tabs

As the joke goes, the most popular way to write Python code is to indent with spaces. The second most popular way to write Python is with tabs. Most good text editors have an “entab/detab” function which can convert tabs to spaces and vice versa.

The important thing is to be consistent. Don’t mix tab and space indenting in the same script! Doing so will cause Python to throw an error and your code won’t execute.



No comments:

Post a Comment