PACKAGES AND IMPORTS

General tools

Random module

Platform module

Modules and execution

sys module

pip

Definitions

STRINGS

General concepts

Properties

Slicing sequence[start : stop : step] → stop is excluded. To reverse a string: text[::-1].

STRING METHODS

1. Modification, analysis, and transformation

2. Boolean string tests

All return True or False:

STRING COMPARISON RULES

EXCEPTIONS

General information

IMPORTANT EXCEPTIONS

ArithmeticError Location: BaseException ← Exception ← ArithmeticError Description: Abstract exception for arithmetic-related errors (e.g., invalid numeric domain).

AssertionError Location: BaseException ← Exception ← AssertionError Description: Raised by assert when the tested expression is False, None, 0, or empty.

BaseException Location: BaseException Description: Root of all Python exceptions. except: is equivalent to except BaseException:.

IndexError Location: BaseException ← Exception ← LookupError ← IndexError Description: Accessing an out-of-range index in a sequence.

KeyboardInterrupt Location: BaseException ← KeyboardInterrupt Description: Raised when a user presses interrupt keys (e.g., Ctrl+C).

LookupError Location: BaseException ← Exception ← LookupError Description: Abstract parent class for errors caused by invalid lookups (indexes, keys, etc.).

MemoryError Location: BaseException ← Exception ← MemoryError Description: Raised when no memory is available for an operation.

OverflowError Location: BaseException ← Exception ← ArithmeticError ← OverflowError Description: Raised when a number is too large to store.

ImportError Location: BaseException ← Exception ← StandardError ← ImportError Description: Raised when an import operation fails.

KeyError Location: BaseException ← Exception ← LookupError ← KeyError Description: Raised when trying to access a dictionary key that does not exist.

OBJECT-ORIENTED PROGRAMMING

Procedural vs Object-Oriented Paradigm

Classes, Objects, and Hierarchy

Object Attributes

An object conceptually contains three groups of attributes:

  1. A name that identifies it within its namespace (some objects may be anonymous).

  2. A set of individual properties that describe its unique state (some objects may have none).

  3. A set of abilities (methods) capable of:

    • modifying itself
    • interacting with other objects

Encapsulation and Private Attributes

Inheritance and Constructor Rules

Example of calling overridden method from superclass:

This process illustrates method overriding: A subclass defines a method with the same name but a different implementation.

__dict__ Attribute

Class Variables vs Instance Variables

Example:

class ExampleClass:
    counter = 0
    def __init__(self, val = 1):
        self.__first = val
        ExampleClass.counter += 1

example_object_1 = ExampleClass()
example_object_2 = ExampleClass(2)
example_object_3 = ExampleClass(4)

Outputs:

example_object_1.__dict__, example_object_1.counter → {'_ExampleClass__first': 1} 3
example_object_2.__dict__, example_object_2.counter → {'_ExampleClass__first': 2} 3
example_object_3.__dict__, example_object_3.counter → {'_ExampleClass__first': 4} 3

Key points:

Checking Attributes with hasattr()

To check if an object/class contains an attribute:

hasattr(object_or_class, "attribute_name")

AttributeError

Modifying a Private Attribute (Name-Mangling Trick)

Even private attributes are accessible if you know their mangled name:

version_2._Python__venomous = not version_2._Python__venomous

This directly negates the private property __venomous of version_2.