Python

Published:

Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.

The main features of Python language are:

  • It supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles.
  • Dynamic type system.
  • Automatic memory management.
  • Large and comprehensive standard library.
  • Cross-platform. There are Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems.
  • Strict but easy-to-read syntax. Python structures depends on indentation.

When we should use Python:

  • If you want to use complex statistical models and structured programs to tackle statistical problems and not scripts (in that case is better R).
  • For interactions with DB, web or others.
  • General purpose programs but high-level programming projects.

The main statements and control flow are:

  • if statement, which conditionally executes a block of code, along with else and elif (a contraction of else-if).
  • for statement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.
  • while statement, which executes a block of code as long as its condition is true.
  • try statement, which allows exceptions raised in its attached code block to be caught and handled by except clauses; it also ensures that clean-up code in a finally block will always be run regardless of how the block exits.
  • class statement, which executes a block of code and attaches its local namespace to a class, for use in object-oriented programming.
  • def statement, which defines a function or method.
  • with statement (from Python 2.5), which encloses a code block within a context manager (for example, acquiring a lock before the block of code is run and releasing the lock afterwards, or opening a file and then closing it), allowing Resource Acquisition Is Initialization (RAII)-like behavior.
  • pass statement, which serves as a NOP. It is syntactically needed to create an empty code block.
  • assert statement, used during debugging to check for conditions that ought to apply.
  • yield statement, which returns a value from a generator function. From Python 2.5, yield is also an operator. This form is used to implement coroutines.
  • import statement, which is used to import modules whose functions or variables can be used in the current program.
  • print statement was changed to the print() function in Python 3.

Syntax

The main special object types we have in python are:

  • Iterator: an object which has the iter function and returns with yield partial results using the statement for.
  • Context manager: a user-defined runtime context that is entered before the statement body is executed and exited when the statement ends. The object has the functions enter and exit to define the enter functionality and the exit function when some error arises.
  • Decorators: a function which is able to get a function or a class and call it surrounded by the decorator definition. It is used with ‘@’ to call the function and it is placed in the previous line to the function or class we want to decorate.

See also

R, Julia, SAS, Matlab, Go (Programming language), Java, C, Fortran, Sage

Material

  • https://www.python.org/
  • https://en.wikibooks.org/wiki/Python_Programming
  • https://wiki.python.org/moin/BeginnersGuide
  • https://docs.python.org/devguide/

Books