1.3. Python Language

../../_images/python-logo.png

Figure 1.1. Python Logo

1.3.1. Rationale

  • 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

  • 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

1.3.2. Which Version?

Table 1.2. Python Versions

Version

PEP

Status

Release

End-of-life

Release Manager

3.12

TBA

future

2023-10

2028-10

TBA

3.11

PEP 664

features

2022-10-03

2027-10

Pablo Galindo Salgado

3.10

PEP 619

bugfix

2021-10-04

2026-10

Pablo Galindo Salgado

3.9

PEP 596

bugfix

2020-10-05

2025-10

Łukasz Langa

3.8

PEP 569

security

2019-10-20

2024-10

Łukasz Langa

3.7

PEP 537

security

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

end-of-life

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

end-of-life

2010-07-03

2020-04-20

Benjamin Peterson

2.6

PEP 361

end-of-life

2008-10-01

2013-10-29

Barry Warsaw

features

new features, bugfixes, and security fixes are accepted.

prerelease

feature fixes, bugfixes, and security fixes are accepted for the upcoming feature release.

bugfix

bugfixes and security fixes are accepted, new binaries are still released. (Also called maintenance mode or stable release)

security

only security fixes are accepted and no more binaries are released, but new source-only versions can be released

end-of-life

release cycle is frozen; no further changes can be pushed to it.

1.3.3. Why not Python 2?

  • PEP 373 -- Python 2.7 Release Schedule

  • PEP 404 -- Python 2.8 Un-release Schedule

  • 2020-04-20 - end of Life for Python 2.7

  • Python 2 is no longer developed [1, 2]

  • Python 2.7 is the last in 2.x branch, and there won't be Python 2.8

  • Python 2.7.18, the last release of Python 2 [3]

1.3.4. Changes in Python 3

  • All strings are Unicode

  • In Python 3 print() is a function, not a keyword

  • Changes in standard library modules naming and location

  • New string formatting

1.3.5. Python Release Cycle

  • Since Python 3.9: PEP 602 -- Annual Release Cycle for Python

  • 12 months (1 year) release cycle

  • 18 months (1.5 year) of bugfix updates

  • 42 months (3.5 year) of security updates

../../_images/pep602-release-calendar.png

Figure 1.2. Python 12 months release cycle.

1.3.6. Scripts

  • Python files use .py as an extension

  • Compiled files are in __pycache__ directory

  • Python also uses other extensions

Table 1.3. 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

1.3.7. 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.10
Python 3.10.0 (default, Oct 13 2021, 06:45:00) [Clang 13.0.0 (clang-1300.0.29.3)] 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:
...     print('yes')
... else:
...     print('no')
yes

1.3.8. 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

1.3.9. Python Developer Survey

1.3.10. Assignments

Code 1.3. Solution
"""
* Assignment: About Environment
* Complexity: easy
* Lines of code: 0 lines
* Time: 3 min

English:
    1. Create file `about_env.py`
    2. Run file in your IDE
    3. Where Python is installed?
    4. Are you using "venv"?
    5. Make sure, `venv` is not `None`
    6. Run doctests - all must succeed

Polish:
    1. Stwórz plik `about_env.py`
    2. Uruchom plik w swoim IDE
    3. Gdzie Python jest zainstalowany?
    4. Czy korzystasz z "venv"?
    5. Upewnij się, że `venv` nie jest `None`
    6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert python_executable
    >>> assert python_version
"""

import os
import sys


python_executable = sys.executable
python_version = tuple(sys.version_info)
venv = os.getenv("VIRTUAL_ENV")