if Python

In some ways, Python is a very user-friendly language.  But it can be very finicky about indentation.  Instead of relying on white space and parentheses to impart structure, it uses exacting syntax.  To an inexperienced programmer, this is not a problem – in fact, it makes writing scripts very easy to learn.  Experienced programmers, on the other hand, might have a hard time getting used to Python’s syntax, which is quite different from other languages.

A prime example can be found in the if-then-else element.  Here is a short example in Python:

if x < 5:
   print("x is less than 5")
elif x > 5:
   print("x is greater than 5")
else:
   print("x equals 5")

Notice the use of colons and indentation to show structure.  Now take a look at this example from JavaScript:

if (x < 5) {
   document.write("x is less than 5");
   }
else if (x > 5) {
   document.write("x is greater than 5");
   }
else {
   document.write("x equals 5");
   }

In JavaScript, curly braces are used to separate control structure, and semicolons are used to terminate individual statements.  Even though I have used an indentation style similar to the Python example, it’s important to note that I could have skipped the indentation altogether – the JavaScript interpreter knows to ignore all white space.

In summary, it may take you awhile to develop new habits when programming in Python.  And it may temporarily lead to bad habits in other languages.  But the benefits of learning Python far outweigh the cost.

Comments are closed.