SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы

Назад

Scripting vs. Programming


Метки: python

Python is often referred to as a scripting language, but scripting languages tend to be limiting, especially in the scope of the problems that they solve. Python, on the other hand, is a programming language that also supports scripting. It is marvelous for scripting, and you may find yourself replacing all your batch files, shell scripts, and simple programs with Python scripts. But it is far more than a scripting language.

The goal of Python is improved productivity. This productivity comes in many ways, but the language is designed to aid you as much as possible, while hindering you as little as possible with arbitrary rules or any requirement that you use a particular set of features. Python is practical; Python language design decisions were based on providing the maximum benefits to the programmer.

Python is very clean to write and especially to read. You will find that it’s quite easy to read your own code long after you’ve written it, and also to read other people’s code. This is accomplished partially through clean, to-the-point syntax, but a major factor in code readability is indentation - scoping in Python is determined by indentation. For example:

# PythonForProgrammers/if.py
response = "yes"
if response == "yes":
    print("affirmative")
    val = 1
print("continuing...")

The ‘#‘ denotes a comment that goes until the end of the line, just like C++ and Java ‘//‘ comments.

First notice that the basic syntax of Python is C-ish as you can see in the if statement. But in a C if, you would be required to use parentheses around the conditional, whereas they are not necessary in Python (it won’t complain if you use them anyway).

The conditional clause ends with a colon, and this indicates that what follows will be a group of indented statements, which are the “then” part of the ifstatement. In this case, there is a “print” statement which sends the result to standard output, followed by an assignment to a variable named val. The subsequent statement is not indented so it is no longer part of the if. Indenting can nest to any level, just like curly braces in C++ or Java, but unlike those languages there is no option (and no argument) about where the braces are placed - the compiler forces everyone’s code to be formatted the same way, which is one of the main reasons for Python’s consistent readability.

Python normally has only one statement per line (you can put more by separating them with semicolons), thus no terminating semicolon is necessary. Even from the brief example above you can see that the language is designed to be as simple as possible, and yet still very readable.