1.4. Python 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

  • 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

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

Figure 1.1. Python Logo

1.4.1. Scripts

  • Python files use .py as an extension

  • Compiled files are in __pycache__ directory

  • Python also uses other extensions

Table 1.2. 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.4.2. 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.4.3. 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.4. References

1.4.5. 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")