1.4. Python Language

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

Figure 1.1. Python Logo

1.4.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.4.2. Which Version?

  • You should use newest official Python version

Table 1.2. Future Versions

Version

PEP

Status

Release

End-of-life

Release Manager

3.10

TBA

future

2021-10

2025-10

TBA

3.9

PEP 596

development

2020-10-05

2025-10

Łukasz Langa

Table 1.3. Currently Supported Versions

Version

PEP

Status

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

Table 1.4. Unsupported Versions

Version

PEP

Status

Release

End-of-life

Release Manager

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

1.4.3. Why not Python 2?

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

  • 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 PEP 404

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

1.4.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.4.5. Python Release Cycle

New in version Python: 3.9 See PEP 602

  • 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.4.6. Scripts

  • Python files use .py as an extension

  • Compiled files are in __pycache__ directory

  • Python also uses other extensions

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

Listing 1.1. Minimal script
print('Ehlo World!')

1.4.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
3.8.2 (default, Mar 11 2020, 00:29:50)
[Clang 11.0.0 (clang-1100.0.33.17)]
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('yes')
... else:
...     print('no')
yes

1.4.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.4.9. Python Developer Survey

1.4.10. Assignments

1.4.10.1. About Version

  • Complexity level: easy

  • Lines of code to write: 0 lines

  • Estimated time of completion: 3 min

  • Solution: solution/about_version.py

English
  1. Create file wih name about_version.py

  2. Use code from "Input" section (see below)

  3. Run code in your IDE (right click on code -> "Run File in Python Console")

  4. What Python version is installed?

  5. Newest official Python is recommended

  6. Compare result with "Output" section (see below)

Polish
  1. Stwórz skrypt o nazwie about_version.py

  2. Użyj kodu z sekcji "Input" (patrz poniżej)

  3. Uruchom kod swoim IDE (prawy klawisz myszy na kodzie -> "Run File in Python Console")

  4. Jaka wersja Python jest zainstalowana?

  5. Zalecana jest najnowsza oficjalna wersja Python

  6. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
import sys

print(sys.version)
Output
3.8.2 (default, Mar 11 2020, 00:29:50)
[Clang 11.0.0 (clang-1100.0.33.17)]
The whys and wherefores
  • Czy Python działa

  • Jaka jest wersja Python

  • Korzystanie z print

  • Umiejętność uruchamiania skryptów

  • Szukanie rozwiązań zadań z książki

1.4.10.2. About Environment

  1. Stwórz plik o nazwie about_env.py

  2. Plik ma mieć zawartość:

    import sys
    import os
    
    print(f'Python Executable: {sys.executable}')
    print(f'Python Version: {sys.version}')
    print(f'Virtualenv: {os.getenv("VIRTUAL_ENV")}')
    
  3. Uruchom go w swoim IDE (menu Run -> Run... -> nazwa Twojego skryptu)

  4. Gdzie Python jest zainstalowany?

  5. Czy korzystasz z "Virtualenv"?

  6. Upewnij się, że w linijce z "Virtualenv" nie masz None

The whys and wherefores
  • Czy Python działa

  • Jaka jest wersja Python

  • Czy korzystasz z Virtualenv

  • Korzystanie z print

  • Umiejętność uruchamiania skryptów

  • Szukanie rozwiązań zadań z książki