Intro#
This section highlights features and tools in Python that can be used independently of any specific application.
Syntax#
Syntax is set of rules that define the structure and format of valid statements and expressions in a programming language. It dictates how symbols, keywords, and operators must be arranged to create meaningful instructions for the computer. Find out more at the specific page.
Look at the following code:
from random import choice
some_symbol = lambda: choice(["\\", "|", "/"])
print("\n".join([
''.join([some_symbol() for i in range(50)])
for i in range(10)
]))
||/||\||/|\/\/\/|///|/|/|\||||\/|//\\\/////\|/||\\
/|\////\||//||\|||||/|/||///|\//|\///|||\||//|/\||
\|//||//\\/\\|\\||/|/\\///\\\|\\\\\/|/|\|\||||///\
//\///\\|\/|||///|\//|\///\\/\\/|/|\\\/\\\/\\|\||\
||/\/||||/\|\|//|/|/\||\||/\|/|\|//\|/|\|||\|\/\|\
////|/\\\//\\|//|\//\//|\/////\\\\|\\/\/|/////\|\|
|\|/\|//\\/\/|///|////||\\\|\|\|//\|||/\|\|/\||\\\
\/||/\\\|\/|/|\\\|||\\/|/\/|/|\\//||/\\|\|\/|/\\||
/|\\\|\\|\/|/\/|\\|\\|/\\|||//\/\|/\/|\\\/|/|/\///
|/|////|\\/|\///\/|\///||||\|///|\\/\\\/\\||//\/|/
At this short snippet code were used:
Operators:
=
- Assignment operator (used insome_symbol = lambda: ...
).:
- Used in the lambda function definition (lambda: choice(...)
).[]
- List literal and list comprehension syntax (["\\", "|", "/"]
,[some_symbol() for i in range(50)]
, and[... for i in range(10)]
).()
- Parentheses for function calls and grouping expressions (choice(...)
,some_symbol()
, andrange(...)
)..
- Attribute access (used in"\n".join(...)
and''.join(...)
).for
- Part of thefor
loop in list comprehensions.in
- Used in the context of thefor
loop within the list comprehensions.
Literals:
"\\", "|", "/"
- String literals representing the symbols in the list.\n
- String literal for a newline character.''
- Empty string literal.50
,10
- Integer literals used as arguments torange()
.
Keywords:
from
- Used for importing specific parts of a module (from random import choice
).import
- Used to bring a module or part of a module into the current namespace.lambda
- Used to create an anonymous function.for
- Used in the list comprehension to iterate over a range.in
- Used in thefor
loop to iterate over elements.
Files & folders#
This section reviews options for working with files and folders in Python. There are some tools typically used in such cases:
os
: A core Python module for interacting with the operating system.pathlib
: A package that provides convenient path operations in Python.shutil
: Implements additional tools not available in the previous modules out of the box.
Find out more in the special page.
As example really typical task - list content of the /tmp
folder:
import os
os.listdir("/tmp")[:5]
['dumps',
'pyright-11804-s2fIQ36Ehj4n',
'snap-private-tmp',
'steam_chrome_overlay_uid1000_spid10387',
'systemd-private-57bd5d93c4894144aa34f82104c1dc8e-fwupd.service-wLri9E']
Packages#
Packages in Python are additional code modules that extend the standard Python library. This section provides an overview of:
Building packages: Transforming source code into a distributable format.
Installing packages: Tools and methods for managing Python packages.
Python indexes: Repositories for storing Python packages and the process of uploading packages to them.
Find all that staff on the particular page of this site.
The packages are usually located in the lib/python<version>/site-packages
directory of the Python distribution. The following cell shows the exact directory for the interpreter used to show examples.
Random entitlements from this folder will also be printed.
import os
import sys
import random
from pathlib import Path
packages_path = sys.path[-2]
print(packages_path, end="\n"*3)
for f in random.sample(os.listdir(packages_path), 10):
print(f)
/home/fedor/.virtualenvironments/python/lib/python3.13/site-packages
async_lru
torch
referencing-0.36.2.dist-info
apscheduler
fqdn
jupyter_server_terminals-0.5.3.dist-info
tinycss2-1.4.0.dist-info
jupyter_lsp-2.2.5.dist-info
isoduration-20.11.0.dist-info
jupyter_server
The most basic tool for working with Python packages, pip
, is itself a Python package. The following cell navigates to its folder and displays the contents of its __init__.py
file.
print((Path(packages_path)/"pip"/"__init__.py").read_text())
from typing import List, Optional
__version__ = "25.1.1"
def main(args: Optional[List[str]] = None) -> int:
"""This is an internal API only meant for use by pip's own console scripts.
For additional details, see https://github.com/pypa/pip/issues/7498.
"""
from pip._internal.utils.entrypoints import _wrapper
return _wrapper(args)
Constructs#
Python contains a set of approaches that allow you to use some special constructs in your custom way. Below is a list of things that related to the special python concepts and protocols:
Name |
Description |
---|---|
|
A decorator that generates init, repr, eq, etc., for classes with fields. |
|
An object implementing |
|
An object implementing |
|
A special iterator created with a function using |
|
A special function using |
|
Like a generator, but supports asynchronous iteration with |
|
Implements |
|
Object defining any of |
|
Any object implementing |
|
A user-defined callable object created with |
|
A method bound to the class, not instance, via |
|
A method that doesn’t receive implicit first argument. |
|
A managed attribute using |
|
A class of a class; controls class creation via |
|
Class with abstract methods using |
|
A tuple subclass with named fields, created via |
|
A set of symbolic names bound to unique constant values. |
|
Object exposing the buffer protocol, used for memory sharing (e.g. |
|
Built-in object representing a slice of a sequence. |
|
Object supporting |
|
Ordered collection supporting indexing and slicing (e.g. lists, tuples). |
|
Object with a |
|
Object supporting rich comparison methods ( |
|
Object implementing |
|
Object that can be awaited (e.g. coroutine, object with |
|
Represents a context-local variable, useful in async code. |
Check details in Constructs page.