3. About Python Language

3.1. What is Python?


Fig. 3.1. Python Logo

3.1.1. Python as a Language

  • Turing complete, general purpose language
  • Multi platform
  • Dynamic typing with automatic memory allocation and GC
  • Code readability and simplicity is important
  • White space are important
  • Everything is an object, but you can write functional code too

3.1.2. Python as a Community

  • Standard language in Machine Learning and Data Science
  • Very good standard system library
  • Huge ecosystem of external open source libraries
  • Open Source created by non-profit Python Software Foundation

3.2. Which version?

  • newest Python 3

3.2.1. What changed in Python 3?

  • All strings are Unicode
  • Changes in standard library naming
  • In Python 3 print() is a function, not a keyword
  • New string formatting

3.2.2. Why not Python 2?

  • Python 2 is no longer developed
  • Python 2.7 is the last in 2.x branch
  • There won’t be Python 2.8 (PEP 404)
  • End of Life for Python 2.7 is 2020 (PEP 373)

3.3. Scripts

3.3.1. File types and extensions

  • Python files use .py as an extension

  • Compiled files are in __pycache__ directory

  • Python also uses other extensions:

    Tab. 3.1. Python file types and extensions
    Extension Description
    .pyc Compiled source code (bytecode)
    .pyd Compiled Windows DLL file
    .pyw Compiled Windows file. Executable with pythonw.exe
    .pyx cPythona source for C/C++ conversion
    .pyz zipapp compressed archive. Since Python 3.5

3.3.2. Minimal script

print('Ehlo World!')

3.3.3. Interpreter declaration

  • Declare interpreter for standalone scripts #!/usr/bin/env python3

    #!/usr/bin/env python3
    print('Ehlo World!')

3.3.4. System environment variable PATH

  • While installing Python, add its executable to one of system $PATH directories

3.3.5. System environment variable PYTHON_PATH

  • Python search for libraries and modules in directories listed in system $PYTHON_PATH
  • $PYTHON_PATH is a base for sys.path

3.4. Python Console (REPL)

  • Read–Eval–Print Loop
  • Quickly test and evaluate code
  • Lines starts with >>>
  • Line continuation starts with ...
  • Result is printed below
  • Open REPL with python3 command in terminal
$ python3

Python 3.7.0 (default, Aug 22 2018, 15:22:33)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> print('Ehlo World!')
Ehlo World!


In documentation and books you may find >>> and ... at the beginning of code listing lines

if True:

3.5. Jupyter

  • Open Source web application REPL
  • Very popular in Machine Learning and Data Science world
  • Create and share documents that contain live code, equations, visualizations and narrative text
  • Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, etc