3. About Python Language

3.1. What is Python?

../_images/python-logo.png

Figure 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.2.3. Status

Branch

Schedule

Status

First release

End-of-life

Release manager

3.8

PEP 569

features

2019-10-20

2024-10

Łukasz Langa

3.7

PEP 537

bugfix

2018-06-27

2023-06-27

Ned Deily

3.6

PEP 494

security

2016-12-23

2021-12-23

Ned Deily

3.5

PEP 478

security

2015-09-13

2020-09-13

Larry Hastings

3.4

PEP 429

end of life

2014-03-16

2019-03-16

Larry Hastings

3.3

PEP 398

end of life

2012-09-29

2017-09-29

Georg Brandl

3.2

PEP 392

end of life

2011-02-20

2016-02-20

Georg Brandl

3.1

PEP 375

end of life

2009-06-27

2012-04-09

Benjamin Peterson

3.0

PEP 361

end of life

2008-12-03

2009-01-13

Barry Warsaw

2.7

PEP 373

bugfix

2010-07-03

2020-01-01

Benjamin Peterson

2.6

PEP 361

end of life

2008-10-01

2013-10-29

Barry Warsaw

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:

    Table 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!

Note

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

if True:
    print(True)
else:
    print(False)

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