Python Modules Modules in Python are files containing Python statements and definitions. They help break down large programs into smaller, more manageable, and organized files , providing code re-usability. A file containing Python code, like my_lib.py, is called a module, and its name is my_lib. Importing a module can be done in several ways: Import the entire module: import my_module. Import by name/alias: import my_module as example. Import a single function: from my_module import my_function. Import all names: from my_module import *. Module Management Module Search Path: When importing, Python searches in this order: Built-in modules. The current directory. The PYTHONPATH environment variable (a list of directories). The installation-dependent default directory. Reloading a Module: Since the interpreter imports a module only once per session, if a module is changed, it must be reloaded. This can be done by using the importlib.reload() function, as restarting the interpreter is less clean Introspection: Introspection is the ability of the language to provide information about its objects at runtime. Python supports this well with built-in functions: dir(): Used to find out names defined inside a module or all names in the current namespace (when used without arguments). help(): Available for each module/object, it allows accessing its documentation. type(): Allows determining the type of an object, e.g., type(a). Python Data Types Python has two main families of data types: Simple and Container. Simple Data Types Simple data types include Int, Long (in Python 2), Float, Complex, and String. Numeric Types: Integer, Long (eliminated in Python 3.x, where int has arbitrary length) , Float (double precision real numbers) , and Complex (double precision, with real and imaginary parts accessed via .real and .imag). Boolean (bool): True and False. bool is a subclass of int, with True corresponding to 1 and False to 0. Non-zero integer values are interpreted as True, and 0 as False. String (str): Character sequences enclosed in single or double quotes. Triple quotes can create strings spanning multiple rows. The char type does not exist; a single character is accessed via indexing or slicing. Strings are immutable; single characters cannot be assigned a new value. Operators + (concatenation) and * (repetition) can be used. Slicing uses square brackets along with an index or indices (e.g., var[1:5]). String Formatting is done using the % character with format specifiers (e.g., "%s", "%d", "%8.2f"). Built-in functions for strings include split(), replace(), strip(), upper(), lower(), find(), index(), startswith(), and endswith(). Container Data Types Container data types store collections of items, including list, tuple, dict, set, and frozenset. List (list[]): An ordered sequence of comma-separated elements inside squared brackets. Items can be of different types. Lists are mutable (can be modified). Supports + (concatenation) and * (repetition), resulting in a new list. Supports slicing with [start:stop:step]. Main methods include append(), insert(), extend(), pop(), remove(), count(), index(), reverse(), and sort(). list.extend() is generally more efficient than + for concatenation. Can be used to implement stacks (using pop and append) and queues (using pop(0) and append). Tuple (tuple()): An ordered sequence of comma-separated values, often enclosed in parentheses. Tuples are immutable (cannot be changed). Supports access via square brackets and slicing. Supports + (concatenation) and * (repetition), resulting in a new tuple. Individual elements cannot be deleted, but the entire tuple can be removed using del. Dictionary (dict{}): An unordered collection of key:value pairs. Dictionaries are mutable. Keys must be of an immutable type (string, number, or tuple with immutable elements) and unique. Keys are used to access values. Values can be of any data type and can repeat. Elements can be accessed using square brackets (my_dict['key']) or the .get() method. get() returns None if the key is missing, while [] raises a KeyError. New items are added, or existing values are updated, using the assignment operator (e.g., my_dict['key'] = value). Elements can be removed using pop(key), popitem(), or del. The keys() and values() functions return views of the dictionary's keys and values, respectively.