Python Builtin Functions Examples

    python comprehensive List Manipulation functions

    Python provides a rich set of built-in functions and methods for comprehensive list manipulation, ranging from simple modifications to powerful data transformations. 

    The primary list manipulation tools can be categorized into List Methods (called on the list object), Built-in Functions (called on the list as an argument), and Advanced Techniques (like list comprehensions). 

    List Methods (In-place Modification)

    These methods are called directly on a list object and modify the list in place. 

    MethodDescription
    append(elem)Adds a single element to the end of the list.
    extend(iterable)Appends elements from an iterable (e.g., another list) to the current list. Equivalent to using the += operator with another list.
    insert(index, elem)Inserts an element at a specific position, shifting subsequent elements to the right.
    remove(elem)Removes the first occurrence of a specified value. Raises a ValueError if the element is not found.
    pop(index)Removes and returns the element at the specified index. If no index is provided, it removes and returns the last element.
    clear()Removes all items from the list, making it empty.
    sort(reverse=False)Sorts the list in place in ascending order. Use reverse=True for descending order.
    reverse()Reverses the order of elements in the list in place.

    List Methods – Syntax & Examples

    append(elem)

    Syntax: list.append(element)

    # Example
    fruits = ['apple', 'banana']
    fruits.append('orange')
    print(fruits)  # Output: ['apple', 'banana', 'orange']
    Python

    extend(iterable)

    Syntax: list.extend(iterable)

    # Example
    fruits = ['apple', 'banana']
    fruits.extend(['orange', 'grape'])
    print(fruits)  # Output: ['apple', 'banana', 'orange', 'grape']
    
    # Alternative using += operator
    fruits += ['mango']
    print(fruits)  # Output: ['apple', 'banana', 'orange', 'grape', 'mango']
    Python

    insert(index, elem)

    Syntax: list.insert(index, element)

    # Example
    fruits = ['apple', 'banana', 'orange']
    fruits.insert(1, 'grape')
    print(fruits)  # Output: ['apple', 'grape', 'banana', 'orange']
    Python

    remove(elem)

    Syntax: list.remove(element)

    # Example
    fruits = ['apple', 'banana', 'orange', 'banana']
    fruits.remove('banana')  # Removes first occurrence
    print(fruits)  # Output: ['apple', 'orange', 'banana']
    Python

    pop(index)

    Syntax: list.pop([index])

    # Example
    fruits = ['apple', 'banana', 'orange']
    last_fruit = fruits.pop()  # Removes and returns last element
    print(last_fruit)  # Output: 'orange'
    print(fruits)  # Output: ['apple', 'banana']
    
    second_fruit = fruits.pop(1)  # Removes and returns element at index 1
    print(second_fruit)  # Output: 'banana'
    print(fruits)  # Output: ['apple']
    Python

    clear()

    Syntax: list.clear()

    # Example
    fruits = ['apple', 'banana', 'orange']
    fruits.clear()
    print(fruits)  # Output: []
    Python

    sort(reverse=False)

    Syntax: list.sort(key=None, reverse=False)

    # Example
    numbers = [3, 1, 4, 1, 5, 9, 2]
    numbers.sort()
    print(numbers)  # Output: [1, 1, 2, 3, 4, 5, 9]
    
    numbers.sort(reverse=True)
    print(numbers)  # Output: [9, 5, 4, 3, 2, 1, 1]
    
    # Sort with key function
    words = ['banana', 'pie', 'Washington', 'book']
    words.sort(key=len)
    print(words)  # Output: ['pie', 'book', 'banana', 'Washington']
    Python

    reverse()

    Syntax: list.reverse()

    # Example
    fruits = ['apple', 'banana', 'orange']
    fruits.reverse()
    print(fruits)  # Output: ['orange', 'banana', 'apple']
    Python

    Built-in Functions (General Operations) 

    These are general Python functions that operate on lists and other iterables, often returning new objects or values. 

    FunctionDescription
    len(list)Returns the number of items in the list.
    max(list)Returns the largest item in the list.
    min(list)Returns the smallest item in the list.
    sum(list)Returns the sum of all elements in the list (must be numeric).
    index(elem, start, end)Returns the index of the first occurrence of an element within a specified range.
    count(elem)Returns the number of times an element appears in the list.
    sorted(list, reverse=False)Returns a new sorted list without modifying the original. This is often preferred over the sort() method when the original list needs to remain unchanged.
    list(iterable)Converts another iterable (like a tuple, string, or range) into a list.
    enumerate(list)Returns an iterator of tuples containing indices and values, useful for looping.

    Built-in Functions – Syntax & Examples

    len(list)

    Syntax: len(list)

    # Example
    fruits = ['apple', 'banana', 'orange']
    print(len(fruits))  # Output: 3
    Python

    max(list)

    Syntax: max(iterable, *[, key, default])

    # Example
    numbers = [3, 1, 4, 1, 5, 9, 2]
    print(max(numbers))  # Output: 9
    
    words = ['apple', 'banana', 'kiwi']
    print(max(words, key=len))  # Output: 'banana'
    Python

    min(list)

    Syntax: min(iterable, *[, key, default])

    # Example
    numbers = [3, 1, 4, 1, 5, 9, 2]
    print(min(numbers))  # Output: 1
    
    words = ['apple', 'banana', 'kiwi']
    print(min(words, key=len))  # Output: 'kiwi'
    Python

    sum(list)

    Syntax: sum(iterable, /, start=0)

    # Example
    numbers = [1, 2, 3, 4, 5]
    print(sum(numbers))  # Output: 15
    
    print(sum(numbers, 10))  # Output: 25 (sum + start value)
    Python

    index(elem, start, end)

    Syntax: list.index(element, start, end)

    # Example
    fruits = ['apple', 'banana', 'orange', 'banana']
    print(fruits.index('banana'))  # Output: 1
    
    print(fruits.index('banana', 2))  # Output: 3 (searching from index 2)
    Python

    count(elem)

    Syntax: list.count(element)

    # Example
    numbers = [1, 2, 3, 2, 4, 2, 5]
    print(numbers.count(2))  # Output: 3
    Python

    sorted(list, reverse=False)

    Syntax: sorted(iterable, *, key=None, reverse=False)

    # Example
    numbers = [3, 1, 4, 1, 5, 9, 2]
    sorted_numbers = sorted(numbers)
    print(sorted_numbers)  # Output: [1, 1, 2, 3, 4, 5, 9]
    print(numbers)  # Output: [3, 1, 4, 1, 5, 9, 2] (original unchanged)
    
    # Reverse sorting
    print(sorted(numbers, reverse=True))  # Output: [9, 5, 4, 3, 2, 1, 1]
    
    # Sort with key
    words = ['banana', 'pie', 'Washington', 'book']
    print(sorted(words, key=len))  # Output: ['pie', 'book', 'banana', 'Washington']
    Python

    list(iterable)

    Syntax: list([iterable])

    # Example
    # From tuple
    my_tuple = (1, 2, 3)
    my_list = list(my_tuple)
    print(my_list)  # Output: [1, 2, 3]
    
    # From string
    my_string = "hello"
    char_list = list(my_string)
    print(char_list)  # Output: ['h', 'e', 'l', 'l', 'o']
    
    # From range
    range_list = list(range(5))
    print(range_list)  # Output: [0, 1, 2, 3, 4]
    Python

    enumerate(list)

    Syntax: enumerate(iterable, start=0)

    # Example
    fruits = ['apple', 'banana', 'orange']
    for index, fruit in enumerate(fruits):
        print(f"{index}: {fruit}")
    # Output:
    # 0: apple
    # 1: banana
    # 2: orange
    
    # Custom start index
    for index, fruit in enumerate(fruits, start=1):
        print(f"{index}: {fruit}")
    # Output:
    # 1: apple
    # 2: banana
    # 3: orange
    Python

    Advanced Techniques

    • List Comprehensions: Provide a concise way to create new lists based on existing ones, often replacing traditional for loops and map() or filter() functions. They are highly efficient and readable for transformations and filtering. # Example of list comprehension to create a list of squares squares = [x * x for x in range(10) if x % 2 == 0] Use code with caution.
    • Slicing: The syntax my_list[start:stop:step] allows you to access a subset of elements, extract elements, or even modify/replace multiple items in the list.
    • Concatenation (+) and Replication (*): The + operator joins two lists to create a new one, while the * operator repeats a list a specified number of times.
    • del statement: Used to remove an element at a specific index or a slice of a list, without returning the value. my_list = [1, 2, 3, 4] del my_list[1] # list becomes [1, 3, 4] Use code with caution.
    • copy() method or [:] slicing: Crucial for creating a shallow copy of a list to avoid modifying the original when making changes.

    python comprehensive Dictionary Manipulation functions

    Python offers a wide range of functions and methods for comprehensive dictionary manipulation. The tools available cover creation, modification, iteration, and data transformation, utilizing both built-in functions and dictionary-specific methods. 

    Dictionary Methods (In-place and Value Retrieval)

    These methods are called directly on a dictionary object for manipulation and data access. 

    MethodDescription
    update(other_dict)Merges key-value pairs from other_dict into the current dictionary, overwriting existing values if keys overlap.
    clear()Removes all items from the dictionary, making it empty.
    pop(key, default)Removes the item with the specified key and returns its value. If the key is not found, it returns the default value if provided, otherwise it raises a KeyError.
    popitem()Removes and returns an arbitrary (usually the last inserted in Python 3.7+) key-value pair as a tuple.
    get(key, default)Returns the value for the key if it exists. If not found, it returns None or the specified default value (does not raise an error).
    keys()Returns a view object that displays a list of all the keys in the dictionary.
    values()Returns a view object that displays a list of all the values in the dictionary.
    items()Returns a view object that displays a list of a dictionary’s key-value tuple pairs.
    setdefault(key, default_value)If key is in the dictionary, return its value. If not, insert key with default_value and return default_value.
    copy()Returns a shallow copy of the dictionary.
    fromkeys(seq, value)A class method that creates a new dictionary with keys from an iterable seq and all values set to value (defaults to None).

    Dictionary Methods – Syntax & Examples

    update(other_dict)

    Syntax: dict.update([other])

    # Example
    person = {'name': 'John', 'age': 30}
    person.update({'age': 31, 'city': 'New York'})
    print(person)  # Output: {'name': 'John', 'age': 31, 'city': 'New York'}
    
    # Using keyword arguments
    person.update(occupation='Engineer')
    print(person)  # Output: {'name': 'John', 'age': 31, 'city': 'New York', 'occupation': 'Engineer'}
    Python

    clear()

    Syntax: dict.clear()

    # Example
    person = {'name': 'John', 'age': 30}
    person.clear()
    print(person)  # Output: {}
    Python

    pop(key, default)

    Syntax: dict.pop(key[, default])

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York'}
    age = person.pop('age')
    print(age)  # Output: 30
    print(person)  # Output: {'name': 'John', 'city': 'New York'}
    
    # With default value
    country = person.pop('country', 'USA')
    print(country)  # Output: USA
    Python

    popitem()

    Syntax: dict.popitem()

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York'}
    item = person.popitem()
    print(item)  # Output: ('city', 'New York') - last inserted in Python 3.7+
    print(person)  # Output: {'name': 'John', 'age': 30}
    Python

    get(key, default)

    Syntax: dict.get(key[, default])

    # Example
    person = {'name': 'John', 'age': 30}
    print(person.get('name'))  # Output: John
    print(person.get('city'))  # Output: None
    print(person.get('city', 'Unknown'))  # Output: Unknown
    Python

    keys()

    Syntax: dict.keys()

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York'}
    print(person.keys())  # Output: dict_keys(['name', 'age', 'city'])
    
    # Convert to list
    keys_list = list(person.keys())
    print(keys_list)  # Output: ['name', 'age', 'city']
    Python

    values()

    Syntax: dict.values()

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York'}
    print(person.values())  # Output: dict_values(['John', 30, 'New York'])
    
    # Convert to list
    values_list = list(person.values())
    print(values_list)  # Output: ['John', 30, 'New York']
    Python

    items()

    Syntax: dict.items()

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York'}
    print(person.items())  # Output: dict_items([('name', 'John'), ('age', 30), ('city', 'New York')])
    
    # Iterate over items
    for key, value in person.items():
        print(f"{key}: {value}")
    # Output:
    # name: John
    # age: 30
    # city: New York
    Python

    setdefault(key, default_value)

    Syntax: dict.setdefault(key[, default])

    # Example
    person = {'name': 'John', 'age': 30}
    city = person.setdefault('city', 'Unknown')
    print(city)  # Output: Unknown
    print(person)  # Output: {'name': 'John', 'age': 30, 'city': 'Unknown'}
    
    # Key already exists
    name = person.setdefault('name', 'Jane')
    print(name)  # Output: John (returns existing value)
    Python

    copy()

    Syntax: dict.copy()

    # Example
    original = {'name': 'John', 'age': 30}
    copy = original.copy()
    copy['age'] = 31
    print(original)  # Output: {'name': 'John', 'age': 30}
    print(copy)  # Output: {'name': 'John', 'age': 31}
    Python

    fromkeys(seq, value)

    Syntax: dict.fromkeys(iterable[, value])

    # Example
    keys = ['name', 'age', 'city']
    new_dict = dict.fromkeys(keys)
    print(new_dict)  # Output: {'name': None, 'age': None, 'city': None}
    
    # With default value
    new_dict = dict.fromkeys(keys, 'Unknown')
    print(new_dict)  # Output: {'name': 'Unknown', 'age': 'Unknown', 'city': 'Unknown'}
    Python

    Built-in Functions and Operators

    These general Python functions and operators work effectively with dictionaries. 

    Function/OperatorDescription
    len(dict)Returns the number of key-value pairs in the dictionary.
    del dict[key]Removes a specific key-value pair using the del statement.
    key in dictThe in operator checks if a key exists within the dictionary (does not check values).
    dict()Converts other data structures (like a list of tuples) into a dictionary.
    sorted(dict)Returns a new list containing the sorted keys of the dictionary.

    Built-in Functions and Operators – Syntax & Examples

    len(dict)

    Syntax: len(dict)

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York'}
    print(len(person))  # Output: 3
    Python

    del dict[key]

    Syntax: del dict[key]

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York'}
    del person['age']
    print(person)  # Output: {'name': 'John', 'city': 'New York'}
    Python

    key in dict

    Syntax: key in dict

    # Example
    person = {'name': 'John', 'age': 30}
    print('name' in person)  # Output: True
    print('city' in person)  # Output: False
    
    # Check if key NOT in dict
    print('city' not in person)  # Output: True
    Python

    dict()

    Syntax: dict(**kwargs) or dict(mapping) or dict(iterable)

    # Example - from keyword arguments
    person = dict(name='John', age=30)
    print(person)  # Output: {'name': 'John', 'age': 30}
    
    # From list of tuples
    pairs = [('name', 'John'), ('age', 30), ('city', 'New York')]
    person = dict(pairs)
    print(person)  # Output: {'name': 'John', 'age': 30, 'city': 'New York'}
    
    # From another dictionary
    original = {'name': 'John', 'age': 30}
    copy = dict(original)
    print(copy)  # Output: {'name': 'John', 'age': 30}
    Python

    sorted(dict)

    Syntax: sorted(iterable, *, key=None, reverse=False)

    # Example
    person = {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
    sorted_keys = sorted(person)
    print(sorted_keys)  # Output: ['age', 'city', 'country', 'name']
    
    # Sort by values
    sorted_by_value = sorted(person.items(), key=lambda x: x[1])
    print(sorted_by_value)  # Output: [('age', 30), ('city', 'New York'), ('country', 'USA'), ('name', 'John')]
    
    # Create sorted dictionary
    sorted_dict = dict(sorted(person.items()))
    print(sorted_dict)  # Output: {'age': 30, 'city': 'New York', 'country': 'USA', 'name': 'John'}
    Python

    Advanced Techniques

    • Dictionary Comprehensions: Similar to list comprehensions, these provide a concise and efficient way to create new dictionaries or filter/transform existing ones. # Example to create a dictionary with squared values squares_dict = {x: x*x for x in range(5)} # Example to filter an existing dictionary filtered_dict = {k: v for k, v in original_dict.items() if v > 10} Use code with caution.
    • Merge Operator (|): Introduced in Python 3.9, the union operator (|) merges two dictionaries into a new one. The right-hand dictionary’s values take precedence in case of key conflicts. d1 =d2 = merged_dict = d1 | d2 # merged_dict becomes Use code with caution.
    • **Unpacking (**)**: Used within function calls or dictionary literals ({**d1, **d2}) for merging dictionaries in Python versions prior to 3.9 or for passing key-value pairs as keyword arguments.

    python comprehensive Python 3 69 built-in functions

    Python 3 currently includes 69 built-in functions as of version 3.8+ (previous versions had 68). These functions are always available in the global namespace without requiring any imports. Many of these “functions” are actually built-in types (like listdictstrint) that can be called to create objects of that type. 

    A comprehensive list of these functions, categorized by their primary purpose, is provided below. For official and detailed documentation, refer to the Python documentation on built-in functions

    Core Types and Constructors

    • Functions like bool(), bytearray(), bytes(), complex(), dict(), float(), frozenset(), int(), list(), object(), range(), set(), str(), tuple(), and type() are used for creating and manipulating core Python data types.

    Core Types and Constructors – Syntax & Examples

    bool()

    Syntax: bool([x])

    # Example
    print(bool(1))  # Output: True
    print(bool(0))  # Output: False
    print(bool(""))  # Output: False
    print(bool("hello"))  # Output: True
    print(bool([]))  # Output: False
    print(bool([1, 2]))  # Output: True
    Python

    int()

    Syntax: int([x]) or int(x, base=10)

    # Example
    print(int(3.14))  # Output: 3
    print(int("42"))  # Output: 42
    print(int("1010", 2))  # Output: 10 (binary to decimal)
    print(int("ff", 16))  # Output: 255 (hexadecimal to decimal)
    Python

    float()

    Syntax: float([x])

    # Example
    print(float(42))  # Output: 42.0
    print(float("3.14"))  # Output: 3.14
    print(float("inf"))  # Output: inf
    Python

    str()

    Syntax: str(object='') or str(object=b'', encoding='utf-8', errors='strict')

    # Example
    print(str(42))  # Output: '42'
    print(str(3.14))  # Output: '3.14'
    print(str([1, 2, 3]))  # Output: '[1, 2, 3]'
    Python

    list()

    Syntax: list([iterable])

    # Example
    print(list((1, 2, 3)))  # Output: [1, 2, 3]
    print(list("hello"))  # Output: ['h', 'e', 'l', 'l', 'o']
    print(list(range(5)))  # Output: [0, 1, 2, 3, 4]
    Python

    tuple()

    Syntax: tuple([iterable])

    # Example
    print(tuple([1, 2, 3]))  # Output: (1, 2, 3)
    print(tuple("hello"))  # Output: ('h', 'e', 'l', 'l', 'o')
    Python

    set()

    Syntax: set([iterable])

    # Example
    print(set([1, 2, 2, 3, 3, 3]))  # Output: {1, 2, 3}
    print(set("hello"))  # Output: {'h', 'e', 'l', 'o'}
    Python

    dict()

    Syntax: dict(**kwargs) or dict(mapping) or dict(iterable)

    # Example
    print(dict(name='John', age=30))  # Output: {'name': 'John', 'age': 30}
    print(dict([('a', 1), ('b', 2)]))  # Output: {'a': 1, 'b': 2}
    Python

    range()

    Syntax: range(stop) or range(start, stop[, step])

    # Example
    print(list(range(5)))  # Output: [0, 1, 2, 3, 4]
    print(list(range(2, 8)))  # Output: [2, 3, 4, 5, 6, 7]
    print(list(range(0, 10, 2)))  # Output: [0, 2, 4, 6, 8]
    Python

    frozenset()

    Syntax: frozenset([iterable])

    # Example
    fs = frozenset([1, 2, 3, 2, 1])
    print(fs)  # Output: frozenset({1, 2, 3})
    # fs.add(4)  # Error: frozenset is immutable
    Python

    complex()

    Syntax: complex([real[, imag]])

    # Example
    print(complex(2, 3))  # Output: (2+3j)
    print(complex('3+4j'))  # Output: (3+4j)
    Python

    bytes()

    Syntax: bytes([source[, encoding[, errors]]])

    # Example
    print(bytes(5))  # Output: b'\x00\x00\x00\x00\x00'
    print(bytes('hello', 'utf-8'))  # Output: b'hello'
    print(bytes([65, 66, 67]))  # Output: b'ABC'
    Python

    bytearray()

    Syntax: bytearray([source[, encoding[, errors]]])

    # Example
    ba = bytearray('hello', 'utf-8')
    print(ba)  # Output: bytearray(b'hello')
    ba[0] = 72  # Mutable, unlike bytes
    print(ba)  # Output: bytearray(b'Hello')
    Python

    type()

    Syntax: type(object) or type(name, bases, dict)

    # Example
    print(type(42))  # Output: <class 'int'>
    print(type("hello"))  # Output: <class 'str'>
    print(type([1, 2, 3]))  # Output: <class 'list'>
    Python

    Mathematical and Numerical

    • Functions such as abs(), bin(), divmod(), hex(), max(), min(), oct(), pow(), round(), and sum() perform mathematical and numerical operations.

    Mathematical and Numerical – Syntax & Examples

    abs()

    Syntax: abs(x)

    # Example
    print(abs(-5))  # Output: 5
    print(abs(3.14))  # Output: 3.14
    print(abs(-3.14))  # Output: 3.14
    print(abs(3+4j))  # Output: 5.0 (magnitude of complex number)
    Python

    bin()

    Syntax: bin(x)

    # Example
    print(bin(10))  # Output: '0b1010'
    print(bin(255))  # Output: '0b11111111'
    print(bin(-5))  # Output: '-0b101'
    Python

    hex()

    Syntax: hex(x)

    # Example
    print(hex(255))  # Output: '0xff'
    print(hex(16))  # Output: '0x10'
    print(hex(-255))  # Output: '-0xff'
    Python

    oct()

    Syntax: oct(x)

    # Example
    print(oct(8))  # Output: '0o10'
    print(oct(64))  # Output: '0o100'
    print(oct(-8))  # Output: '-0o10'
    Python

    divmod()

    Syntax: divmod(a, b)

    # Example
    print(divmod(17, 5))  # Output: (3, 2) - quotient and remainder
    print(divmod(100, 7))  # Output: (14, 2)
    print(divmod(9.5, 2.5))  # Output: (3.0, 1.5)
    Python

    pow()

    Syntax: pow(base, exp[, mod])

    # Example
    print(pow(2, 3))  # Output: 8
    print(pow(2, 3, 5))  # Output: 3 (2^3 % 5)
    print(pow(5, -1))  # Output: 0.2
    Python

    round()

    Syntax: round(number[, ndigits])

    # Example
    print(round(3.14159))  # Output: 3
    print(round(3.14159, 2))  # Output: 3.14
    print(round(2.5))  # Output: 2 (rounds to nearest even)
    print(round(3.5))  # Output: 4
    Python

    Iteration and Control Flow

    • Functions like all(), any(), aiter(), anext(), enumerate(), filter(), iter(), map(), next(), reversed(), sorted(), and zip() are used for iterating over sequences and controlling program flow.

    Iteration and Control Flow – Syntax & Examples

    all()

    Syntax: all(iterable)

    # Example
    print(all([True, True, True]))  # Output: True
    print(all([True, False, True]))  # Output: False
    print(all([1, 2, 3]))  # Output: True (all non-zero)
    print(all([1, 0, 3]))  # Output: False (0 is falsy)
    print(all([]))  # Output: True (empty iterable)
    Python

    any()

    Syntax: any(iterable)

    # Example
    print(any([False, False, False]))  # Output: False
    print(any([False, True, False]))  # Output: True
    print(any([0, 0, 1]))  # Output: True
    print(any([]))  # Output: False (empty iterable)
    Python

    enumerate()

    Syntax: enumerate(iterable, start=0)

    # Example
    for i, val in enumerate(['a', 'b', 'c']):
        print(f"{i}: {val}")
    # Output:
    # 0: a
    # 1: b
    # 2: c
    
    # With custom start
    for i, val in enumerate(['a', 'b', 'c'], start=1):
        print(f"{i}: {val}")
    # Output:
    # 1: a
    # 2: b
    # 3: c
    Python

    filter()

    Syntax: filter(function, iterable)

    # Example
    numbers = [1, 2, 3, 4, 5, 6]
    evens = list(filter(lambda x: x % 2 == 0, numbers))
    print(evens)  # Output: [2, 4, 6]
    
    # Filter None values
    mixed = [1, None, 2, None, 3]
    filtered = list(filter(None, mixed))
    print(filtered)  # Output: [1, 2, 3]
    Python

    map()

    Syntax: map(function, iterable, ...)

    # Example
    numbers = [1, 2, 3, 4, 5]
    squared = list(map(lambda x: x**2, numbers))
    print(squared)  # Output: [1, 4, 9, 16, 25]
    
    # Multiple iterables
    nums1 = [1, 2, 3]
    nums2 = [10, 20, 30]
    sums = list(map(lambda x, y: x + y, nums1, nums2))
    print(sums)  # Output: [11, 22, 33]
    Python

    zip()

    Syntax: zip(*iterables)

    # Example
    names = ['Alice', 'Bob', 'Charlie']
    ages = [25, 30, 35]
    combined = list(zip(names, ages))
    print(combined)  # Output: [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
    
    # Unzipping
    pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
    nums, letters = zip(*pairs)
    print(nums)  # Output: (1, 2, 3)
    print(letters)  # Output: ('a', 'b', 'c')
    Python

    reversed()

    Syntax: reversed(seq)

    # Example
    numbers = [1, 2, 3, 4, 5]
    rev = list(reversed(numbers))
    print(rev)  # Output: [5, 4, 3, 2, 1]
    
    text = "hello"
    rev_text = ''.join(reversed(text))
    print(rev_text)  # Output: 'olleh'
    Python

    iter()

    Syntax: iter(object[, sentinel])

    # Example
    my_list = [1, 2, 3]
    iterator = iter(my_list)
    print(next(iterator))  # Output: 1
    print(next(iterator))  # Output: 2
    
    # With sentinel
    with open('file.txt') as f:
        for line in iter(f.readline, ''):
            print(line)
    Python

    next()

    Syntax: next(iterator[, default])

    # Example
    my_iter = iter([1, 2, 3])
    print(next(my_iter))  # Output: 1
    print(next(my_iter))  # Output: 2
    print(next(my_iter))  # Output: 3
    print(next(my_iter, 'Done'))  # Output: 'Done' (default instead of StopIteration)
    Python

    String and Character Handling

    • Functions including ascii()chr()format()ord(), and repr() are for working with strings and characters. 

    String and Character Handling – Syntax & Examples

    chr()

    Syntax: chr(i)

    # Example
    print(chr(65))  # Output: 'A'
    print(chr(97))  # Output: 'a'
    print(chr(8364))  # Output: '€'
    Python

    ord()

    Syntax: ord(c)

    # Example
    print(ord('A'))  # Output: 65
    print(ord('a'))  # Output: 97
    print(ord(''))  # Output: 8364
    Python

    ascii()

    Syntax: ascii(object)

    # Example
    print(ascii('hello'))  # Output: "'hello'"
    print(ascii('café'))  # Output: "'caf\\xe9'"
    print(ascii(['a', 'b', 'café']))  # Output: "['a', 'b', 'caf\\xe9']"
    Python

    repr()

    Syntax: repr(object)

    # Example
    print(repr('hello'))  # Output: "'hello'"
    print(repr([1, 2, 3]))  # Output: '[1, 2, 3]'
    s = 'hello\nworld'
    print(repr(s))  # Output: "'hello\\nworld'"
    Python

    format()

    Syntax: format(value[, format_spec])

    # Example
    print(format(3.14159, '.2f'))  # Output: '3.14'
    print(format(42, 'b'))  # Output: '101010' (binary)
    print(format(1000000, ','))  # Output: '1,000,000'
    print(format(0.5, '%'))  # Output: '50.000000%'

    Object and Attribute Management

    • Functions for managing objects and their attributes include callable()classmethod()compile()delattr()dir()getattr()globals()hasattr()hash()id()isinstance()issubclass()locals()memoryview()property()setattr()slice()staticmethod()super(), and vars()

    Object and Attribute Management – Syntax & Examples

    dir()

    Syntax: dir([object])

    # Example
    print(dir([]))  # Lists all attributes and methods of list
    print(dir(str))  # Lists all attributes and methods of string
    
    class MyClass:
        def __init__(self):
            self.x = 10
    
    print(dir(MyClass()))  # Lists all attributes including 'x'
    Python

    getattr()

    Syntax: getattr(object, name[, default])

    # Example
    class Person:
        name = 'John'
        age = 30
    
    p = Person()
    print(getattr(p, 'name'))  # Output: 'John'
    print(getattr(p, 'city', 'Unknown'))  # Output: 'Unknown'
    Python

    setattr()

    Syntax: setattr(object, name, value)

    # Example
    class Person:
        name = 'John'
    
    p = Person()
    setattr(p, 'age', 30)
    print(p.age)  # Output: 30
    Python

    hasattr()

    Syntax: hasattr(object, name)

    # Example
    class Person:
        name = 'John'
    
    p = Person()
    print(hasattr(p, 'name'))  # Output: True
    print(hasattr(p, 'age'))  # Output: False
    Python

    delattr()

    Syntax: delattr(object, name)

    # Example
    class Person:
        name = 'John'
        age = 30
    
    p = Person()
    delattr(p, 'age')
    print(hasattr(p, 'age'))  # Output: False
    Python

    isinstance()

    Syntax: isinstance(object, classinfo)

    # Example
    print(isinstance(5, int))  # Output: True
    print(isinstance('hello', str))  # Output: True
    print(isinstance([1, 2], (list, tuple)))  # Output: True
    print(isinstance(5, str))  # Output: False
    Python

    issubclass()

    Syntax: issubclass(class, classinfo)

    # Example
    class Animal:
        pass
    
    class Dog(Animal):
        pass
    
    print(issubclass(Dog, Animal))  # Output: True
    print(issubclass(Animal, Dog))  # Output: False
    print(issubclass(bool, int))  # Output: True
    Python

    callable()

    Syntax: callable(object)

    # Example
    def my_func():
        pass
    
    print(callable(my_func))  # Output: True
    print(callable(lambda x: x))  # Output: True
    print(callable(42))  # Output: False
    print(callable('hello'))  # Output: False
    Python

    id()

    Syntax: id(object)

    # Example
    a = [1, 2, 3]
    print(id(a))  # Output: unique integer (memory address)
    
    b = a
    print(id(a) == id(b))  # Output: True (same object)
    
    c = [1, 2, 3]
    print(id(a) == id(c))  # Output: False (different objects)
    Python

    hash()

    Syntax: hash(object)

    # Example
    print(hash('hello'))  # Output: some integer
    print(hash(42))  # Output: 42
    print(hash((1, 2, 3)))  # Output: some integer
    # print(hash([1, 2, 3]))  # Error: unhashable type
    Python

    vars()

    Syntax: vars([object])

    # Example
    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    p = Person('John', 30)
    print(vars(p))  # Output: {'name': 'John', 'age': 30}
    Python

    globals()

    Syntax: globals()

    # Example
    x = 10
    print('x' in globals())  # Output: True
    print(type(globals()))  # Output: <class 'dict'>
    Python

    locals()

    Syntax: locals()

    # Example
    def my_func():
        a = 10
        b = 20
        print(locals())  # Output: {'a': 10, 'b': 20}
    
    my_func()
    Python

    Input/Output and System Interaction

    • Functions related to input/output and system interaction are breakpoint()exec()eval()help()input()len()open()print(), and __import__().

    Input/Output and System Interaction – Syntax & Examples

    print()

    Syntax: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

    # Example
    print('Hello, World!')  # Output: Hello, World!
    print('Hello', 'World', sep=', ')  # Output: Hello, World
    print('Hello', end=' ')  # Output: Hello (no newline)
    print('World')  # Output: World
    
    print('Name:', 'John', 'Age:', 30, sep=' | ')  # Output: Name: | John | Age: | 30
    Python

    input()

    Syntax: input([prompt])

    # Example
    name = input('Enter your name: ')  # Waits for user input
    print(f'Hello, {name}!')
    
    age = int(input('Enter your age: '))  # Convert input to int
    print(f'You are {age} years old')
    Python

    open()

    Syntax: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

    # Example
    # Reading a file
    with open('file.txt', 'r') as f:
        content = f.read()
        print(content)
    
    # Writing to a file
    with open('output.txt', 'w') as f:
        f.write('Hello, World!')
    
    # Appending to a file
    with open('log.txt', 'a') as f:
        f.write('New log entry\n')
    
    # Reading lines
    with open('file.txt', 'r') as f:
        for line in f:
            print(line.strip())
    Python

    eval()

    Syntax: eval(expression[, globals[, locals]])

    # Example
    result = eval('2 + 3 * 4')
    print(result)  # Output: 14
    
    x = 10
    result = eval('x * 2')
    print(result)  # Output: 20
    
    # With dictionary
    data = {'a': 10, 'b': 20}
    result = eval('a + b', data)
    print(result)  # Output: 30
    Python

    exec()

    Syntax: exec(object[, globals[, locals]])

    # Example
    code = '''
    x = 10
    y = 20
    print(x + y)
    '''
    exec(code)  # Output: 30
    
    # Execute with namespace
    namespace = {}
    exec('x = 100', namespace)
    print(namespace['x'])  # Output: 100
    Python

    help()

    Syntax: help([object])

    # Example
    help(len)  # Displays help for len function
    help(str.upper)  # Displays help for str.upper method
    help(list)  # Displays help for list class
    Python

    breakpoint()

    Syntax: breakpoint(*args, **kws)

    # Example
    x = 10
    y = 20
    breakpoint()  # Starts debugger here
    z = x + y
    print(z)
    Python

    python comprehensive Python 3 standard library functions

    The Python 3 Standard Library is an extensive collection of modules and built-in functions that come bundled with every Python installation, offering a vast range of functionalities for everyday programming tasks without needing external installations. The library is often summarized as “batteries included” due to its breadth. 

    The official documentation for the complete, up-to-date list can be found at the Python documentation on the standard library

    Key modules and their primary functions are categorized below:

    Core Modules

    • os: Provides a portable way to interact with the operating system, including functions for manipulating files, directories (os.listdir()os.remove()), and environment variables.
    • sys: Provides access to system-specific parameters and functions, such as command-line arguments (sys.argv) and program termination (sys.exit()).
    • pathlib: Offers an object-oriented approach to handling filesystem paths, which is often more readable than the older os.path functions.
    • shutil: Provides high-level file operations, including robust file copying, moving, and deletion. 

    Data Types and Structures

    • collections: Contains high-performance container data types like deque (double-ended queue), Counter (for counting hashable objects), and namedtuple.
    • copy: Provides functions for shallow and deep copying of objects.
    • array: Offers efficient arrays of basic numeric values, a compact alternative to lists for numerical data. 

    Mathematics and Statistics

    • math: Provides access to standard mathematical functions (trigonometry, logarithms, floor/ceil, etc.) and constants like pi.
    • random: Implements pseudo-random number generators for various distributions, useful for simulations and games (random.randint()random.uniform()).
    • statistics: Provides functions for mathematical statistics, such as mean, median, and variance.
    • decimal: Offers support for fixed-point and floating-point arithmetic with user-definable precision, useful for financial calculations. 

    Dates and Times

    • datetime: Supplies classes for manipulating dates, times, and time deltas in both simple and complex ways.
    • time: Provides various time-related functions, including measuring execution time (time.perf_counter()) and pausing program execution (time.sleep()).
    • calendar: Offers general calendar-related functions and formatting. 

    String Processing and Pattern Matching

    • string: Contains common string operations and utilities.
    • re: Provides powerful support for regular expressions (regex) for advanced pattern matching and text manipulation.
    • textwrap: Functions for wrapping and filling text paragraphs. 

    Internet and Web Programming

    • urllib: A package for handling URLs, including modules to fetch data (urllib.request.urlopen()), parse URLs, and handle errors.
    • json: Provides functions for encoding and decoding data in the JSON format, common in web APIs.
    • http: A package of modules for handling the HTTP protocol, including an http.server for creating simple web servers.
    • socket: A low-level networking interface for basic client-server communication. 

    File Formats and Data Persistence

    • csv: A module for reading from and writing to CSV files easily.
    • configparser: Used for parsing INI-style configuration files.
    • pickle: Implements Python object serialization (pickling and unpickling), allowing objects to be saved to a file and loaded back later.
    • sqlite3: A DB-API 2.0 interface for working with SQLite databases. 

    Concurrency and Parallelism

    • threading: Provides a higher-level interface for running code in parallel using threads.
    • multiprocessing: Supports process-based parallelism, bypassing the Global Interpreter Lock (GIL).
    • asyncio: Framework for writing concurrent code using the async/await syntax for asynchronous I/O. 

    Development and Testing

    • unittest: Python’s built-in unit testing framework.
    • doctest: Automatically tests examples embedded within docstrings.
    • pdb: The Python Debugger, providing an interactive debugging environment.
    • argparse: A robust library for parsing command-line options and arguments.

    Standard Library – Syntax & Examples

    Core Modules

    os Module

    Common Functions:

    import os
    
    # Get current working directory
    cwd = os.getcwd()
    print(cwd)  # Output: '/path/to/current/directory'
    
    # Change directory
    os.chdir('/path/to/new/directory')
    
    # List directory contents
    files = os.listdir('.')
    print(files)  # Output: ['file1.txt', 'file2.py', 'folder1']
    
    # Create directory
    os.mkdir('new_folder')
    os.makedirs('parent/child/grandchild')  # Creates nested directories
    
    # Remove file/directory
    os.remove('file.txt')  # Remove file
    os.rmdir('empty_folder')  # Remove empty directory
    
    # Check if path exists
    exists = os.path.exists('file.txt')
    print(exists)  # Output: True or False
    
    # Check if path is file or directory
    is_file = os.path.isfile('file.txt')
    is_dir = os.path.isdir('folder')
    
    # Join paths (cross-platform)
    path = os.path.join('folder', 'subfolder', 'file.txt')
    print(path)  # Output: 'folder/subfolder/file.txt' or 'folder\\subfolder\\file.txt'
    
    # Split path
    dirname, filename = os.path.split('/path/to/file.txt')
    print(dirname)  # Output: '/path/to'
    print(filename)  # Output: 'file.txt'
    
    # Get file size
    size = os.path.getsize('file.txt')
    print(f"Size: {size} bytes")
    
    # Environment variables
    home = os.environ.get('HOME')
    os.environ['MY_VAR'] = 'value'
    
    # Execute system command
    os.system('echo "Hello World"')
    Python

    sys Module

    Common Functions:

    import sys
    
    # Command-line arguments
    print(sys.argv)  # Output: ['script.py', 'arg1', 'arg2']
    script_name = sys.argv[0]
    arguments = sys.argv[1:]
    
    # Exit program
    sys.exit(0)  # Exit with success code
    sys.exit(1)  # Exit with error code
    
    # Python version
    print(sys.version)  # Output: '3.11.0 (main, Oct 24 2022, 18:26:48)...'
    print(sys.version_info)  # Output: sys.version_info(major=3, minor=11, micro=0...)
    
    # Module search path
    print(sys.path)  # List of paths Python searches for modules
    
    # Standard streams
    sys.stdout.write('Hello\n')  # Write to stdout
    sys.stderr.write('Error\n')  # Write to stderr
    line = sys.stdin.readline()  # Read from stdin
    
    # Platform information
    print(sys.platform)  # Output: 'linux', 'darwin', 'win32', etc.
    
    # Get size of object
    import sys
    x = [1, 2, 3, 4, 5]
    print(sys.getsizeof(x))  # Output: size in bytes
    Python

    pathlib Module

    Common Usage:

    from pathlib import Path
    
    # Create Path object
    p = Path('/usr/local/bin')
    home = Path.home()  # User's home directory
    cwd = Path.cwd()  # Current working directory
    
    # Join paths
    config_file = Path.home() / '.config' / 'app' / 'settings.json'
    print(config_file)  # Output: /home/user/.config/app/settings.json
    
    # Path properties
    print(p.name)  # Output: 'bin' (last component)
    print(p.parent)  # Output: '/usr/local'
    print(p.stem)  # Output: filename without extension
    print(p.suffix)  # Output: '.txt' (file extension)
    print(p.parts)  # Output: ('/', 'usr', 'local', 'bin')
    
    # Check path
    print(p.exists())  # True/False
    print(p.is_file())  # True/False
    print(p.is_dir())  # True/False
    
    # Read/write files
    file = Path('data.txt')
    file.write_text('Hello, World!')  # Write string to file
    content = file.read_text()  # Read entire file as string
    
    file.write_bytes(b'binary data')  # Write bytes
    data = file.read_bytes()  # Read as bytes
    
    # Iterate directory
    for item in Path('.').iterdir():
        print(item)
    
    # Glob patterns
    for py_file in Path('.').glob('*.py'):
        print(py_file)
    
    # Recursive glob
    for py_file in Path('.').rglob('*.py'):  # Recursive search
        print(py_file)
    
    # Create directory
    Path('new_folder').mkdir(exist_ok=True)
    Path('parent/child').mkdir(parents=True, exist_ok=True)
    
    # Resolve absolute path
    absolute = Path('relative/path').resolve()
    Python

    shutil Module

    Common Functions:

    import shutil
    
    # Copy file
    shutil.copy('source.txt', 'destination.txt')  # Copy file
    shutil.copy2('source.txt', 'dest.txt')  # Copy with metadata
    
    # Copy directory tree
    shutil.copytree('source_dir', 'dest_dir')
    
    # Move file or directory
    shutil.move('source.txt', 'destination.txt')
    
    # Remove directory tree
    shutil.rmtree('directory_to_delete')
    
    # Archive operations
    shutil.make_archive('archive_name', 'zip', 'folder_to_zip')
    shutil.unpack_archive('archive.zip', 'extract_to')
    
    # Disk usage
    usage = shutil.disk_usage('/')
    print(f"Total: {usage.total / (1024**3):.2f} GB")
    print(f"Used: {usage.used / (1024**3):.2f} GB")
    print(f"Free: {usage.free / (1024**3):.2f} GB")
    
    # Which - find executable
    python_path = shutil.which('python')
    print(python_path)  # Output: '/usr/bin/python'
    Python

    Data Types and Structures

    collections Module

    Common Classes:

    from collections import deque, Counter, defaultdict, namedtuple, OrderedDict
    
    # deque - double-ended queue
    dq = deque([1, 2, 3])
    dq.append(4)  # Add to right
    dq.appendleft(0)  # Add to left
    dq.pop()  # Remove from right
    dq.popleft()  # Remove from left
    dq.rotate(1)  # Rotate right
    print(dq)  # Output: deque([3, 0, 1, 2])
    
    # Counter - count hashable objects
    words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
    counter = Counter(words)
    print(counter)  # Output: Counter({'apple': 3, 'banana': 2, 'cherry': 1})
    print(counter.most_common(2))  # Output: [('apple', 3), ('banana', 2)]
    counter.update(['apple', 'date'])  # Add more counts
    
    # defaultdict - dict with default factory
    dd = defaultdict(list)
    dd['fruits'].append('apple')
    dd['fruits'].append('banana')
    print(dd)  # Output: defaultdict(<class 'list'>, {'fruits': ['apple', 'banana']})
    
    dd_int = defaultdict(int)
    dd_int['count'] += 1
    print(dd_int['count'])  # Output: 1
    
    # namedtuple - tuple with named fields
    Point = namedtuple('Point', ['x', 'y'])
    p = Point(10, 20)
    print(p.x, p.y)  # Output: 10 20
    print(p[0], p[1])  # Output: 10 20
    
    Person = namedtuple('Person', 'name age city')
    person = Person('John', 30, 'NYC')
    print(person.name)  # Output: John
    
    # OrderedDict - maintains insertion order (less relevant in Python 3.7+)
    od = OrderedDict()
    od['first'] = 1
    od['second'] = 2
    od['third'] = 3
    Python

    copy Module

    Functions:

    import copy
    
    # Shallow copy
    original = [1, 2, [3, 4]]
    shallow = copy.copy(original)
    shallow[2][0] = 999
    print(original)  # Output: [1, 2, [999, 4]] - nested list affected!
    
    # Deep copy
    original = [1, 2, [3, 4]]
    deep = copy.deepcopy(original)
    deep[2][0] = 999
    print(original)  # Output: [1, 2, [3, 4]] - original unchanged
    
    # With dictionaries
    original_dict = {'a': 1, 'b': {'c': 2}}
    shallow_dict = copy.copy(original_dict)
    deep_dict = copy.deepcopy(original_dict)
    Python

    array Module

    Usage:

    import array
    
    # Create array (more memory-efficient than list for numeric data)
    # 'i' = signed int, 'd' = double, 'f' = float
    arr = array.array('i', [1, 2, 3, 4, 5])
    print(arr)  # Output: array('i', [1, 2, 3, 4, 5])
    
    # Array operations
    arr.append(6)
    arr.extend([7, 8, 9])
    arr.insert(0, 0)
    print(arr[0])  # Access element
    
    # Convert to list
    lst = arr.tolist()
    
    # From list
    arr2 = array.array('d', [1.1, 2.2, 3.3])
    Python

    Mathematics and Statistics

    math Module

    Common Functions:

    import math
    
    # Constants
    print(math.pi)  # Output: 3.141592653589793
    print(math.e)  # Output: 2.718281828459045
    print(math.inf)  # Infinity
    print(math.nan)  # Not a Number
    
    # Rounding
    print(math.ceil(4.3))  # Output: 5 (round up)
    print(math.floor(4.7))  # Output: 4 (round down)
    print(math.trunc(4.7))  # Output: 4 (truncate)
    
    # Power and logarithm
    print(math.sqrt(16))  # Output: 4.0
    print(math.pow(2, 3))  # Output: 8.0
    print(math.log(10))  # Natural log (base e)
    print(math.log10(100))  # Output: 2.0 (log base 10)
    print(math.log2(8))  # Output: 3.0 (log base 2)
    
    # Trigonometry (radians)
    print(math.sin(math.pi / 2))  # Output: 1.0
    print(math.cos(0))  # Output: 1.0
    print(math.tan(math.pi / 4))  # Output: ~1.0
    print(math.degrees(math.pi))  # Output: 180.0
    print(math.radians(180))  # Output: 3.141592653589793
    
    # Other functions
    print(math.factorial(5))  # Output: 120
    print(math.gcd(48, 18))  # Output: 6 (greatest common divisor)
    print(math.isnan(math.nan))  # Output: True
    print(math.isinf(math.inf))  # Output: True
    Python

    random Module

    Common Functions:

    import random
    
    # Random float [0.0, 1.0)
    print(random.random())  # Output: 0.37444887175646646
    
    # Random integer
    print(random.randint(1, 10))  # Output: random int from 1 to 10 (inclusive)
    print(random.randrange(0, 10, 2))  # Output: random even number 0-8
    
    # Random choice
    colors = ['red', 'green', 'blue']
    print(random.choice(colors))  # Output: random color
    
    # Multiple random choices
    print(random.choices(colors, k=3))  # Output: ['red', 'blue', 'red'] (with replacement)
    print(random.sample(colors, k=2))  # Output: ['green', 'blue'] (without replacement)
    
    # Shuffle list in-place
    deck = [1, 2, 3, 4, 5]
    random.shuffle(deck)
    print(deck)  # Output: [3, 1, 5, 2, 4]
    
    # Random float in range
    print(random.uniform(1.5, 10.5))  # Output: random float between 1.5 and 10.5
    
    # Seed for reproducibility
    random.seed(42)
    print(random.random())  # Always same value with seed 42
    
    # Distributions
    print(random.gauss(0, 1))  # Gaussian distribution (mean=0, std=1)
    print(random.expovariate(1.5))  # Exponential distribution
    Python

    statistics Module

    Common Functions:

    import statistics
    
    data = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9]
    
    # Mean (average)
    print(statistics.mean(data))  # Output: 5.0
    
    # Median (middle value)
    print(statistics.median(data))  # Output: 5.0
    print(statistics.median_low(data))  # Lower median
    print(statistics.median_high(data))  # Higher median
    
    # Mode (most common)
    print(statistics.mode(data))  # Output: 5
    
    # Standard deviation
    print(statistics.stdev(data))  # Sample standard deviation
    print(statistics.pstdev(data))  # Population standard deviation
    
    # Variance
    print(statistics.variance(data))  # Sample variance
    print(statistics.pvariance(data))  # Population variance
    
    # Quantiles
    print(statistics.quantiles(data, n=4))  # Quartiles
    Python

    decimal Module

    Usage:

    from decimal import Decimal, getcontext
    
    # Precision control
    getcontext().prec = 28  # Set precision
    
    # Create Decimal
    d1 = Decimal('0.1')
    d2 = Decimal('0.2')
    print(d1 + d2)  # Output: 0.3 (exact, not 0.30000000000000004)
    
    # From float (not recommended)
    d3 = Decimal(0.1)  # Imprecise
    print(d3)  # Output: 0.1000000000000000055511151231257827021181583404541015625
    
    # From string (recommended)
    d4 = Decimal('0.1')  # Precise
    print(d4)  # Output: 0.1
    
    # Financial calculations
    price = Decimal('19.99')
    tax_rate = Decimal('0.08')
    total = price * (1 + tax_rate)
    print(f"${total:.2f}")  # Output: $21.59
    
    # Rounding
    value = Decimal('3.14159')
    print(value.quantize(Decimal('0.01')))  # Output: 3.14
    Python

    Dates and Times

    datetime Module

    Common Classes:

    from datetime import datetime, date, time, timedelta
    
    # Current date and time
    now = datetime.now()
    print(now)  # Output: 2025-11-24 15:30:45.123456
    
    # Current date
    today = date.today()
    print(today)  # Output: 2025-11-24
    
    # Create specific datetime
    dt = datetime(2025, 11, 24, 15, 30, 45)
    print(dt)  # Output: 2025-11-24 15:30:45
    
    # Create specific date
    d = date(2025, 11, 24)
    print(d)  # Output: 2025-11-24
    
    # Create time
    t = time(15, 30, 45)
    print(t)  # Output: 15:30:45
    
    # Format datetime as string
    formatted = now.strftime('%Y-%m-%d %H:%M:%S')
    print(formatted)  # Output: 2025-11-24 15:30:45
    
    # Parse string to datetime
    dt_string = '2025-11-24 15:30:45'
    dt = datetime.strptime(dt_string, '%Y-%m-%d %H:%M:%S')
    
    # Date/time components
    print(now.year, now.month, now.day)  # 2025 11 24
    print(now.hour, now.minute, now.second)  # 15 30 45
    
    # Timedelta (time difference)
    delta = timedelta(days=7, hours=2, minutes=30)
    future = now + delta
    past = now - delta
    print(future)  # Output: 7 days, 2.5 hours in the future
    
    # Difference between dates
    diff = date(2025, 12, 31) - date(2025, 11, 24)
    print(diff.days)  # Output: 37
    
    # Weekday
    print(today.weekday())  # Output: 0-6 (Monday=0)
    print(today.isoweekday())  # Output: 1-7 (Monday=1)
    Python

    time Module

    Common Functions:

    import time
    
    # Current time (seconds since epoch)
    timestamp = time.time()
    print(timestamp)  # Output: 1700843445.123456
    
    # Sleep (pause execution)
    print("Starting...")
    time.sleep(2)  # Sleep for 2 seconds
    print("Done!")
    
    # Performance counter (high precision timing)
    start = time.perf_counter()
    # ... some operation ...
    time.sleep(0.1)
    end = time.perf_counter()
    elapsed = end - start
    print(f"Elapsed: {elapsed:.6f} seconds")
    
    # Format time
    local_time = time.localtime()
    formatted = time.strftime('%Y-%m-%d %H:%M:%S', local_time)
    print(formatted)  # Output: 2025-11-24 15:30:45
    
    # Parse time string
    time_string = '2025-11-24 15:30:45'
    parsed = time.strptime(time_string, '%Y-%m-%d %H:%M:%S')
    
    # Convert between formats
    print(time.ctime(timestamp))  # Human-readable format
    Python

    calendar Module

    Common Functions:

    import calendar
    
    # Print calendar
    print(calendar.month(2025, 11))  # Print November 2025 calendar
    
    # Full year calendar
    print(calendar.calendar(2025))
    
    # Check if leap year
    print(calendar.isleap(2024))  # Output: True
    print(calendar.isleap(2025))  # Output: False
    
    # Number of leap years in range
    print(calendar.leapdays(2000, 2025))  # Output: 7
    
    # Month range
    first_weekday, num_days = calendar.monthrange(2025, 11)
    print(f"First day: {first_weekday}, Days: {num_days}")  # 5 (Saturday), 30 days
    
    # Weekday (0=Monday)
    weekday = calendar.weekday(2025, 11, 24)
    print(calendar.day_name[weekday])  # Output: Monday
    Python

    String Processing and Pattern Matching

    re Module (Regular Expressions)

    Common Functions:

    import re
    
    # Search for pattern
    text = "Contact: john@example.com or jane@test.com"
    match = re.search(r'\w+@\w+\.\w+', text)
    if match:
        print(match.group())  # Output: john@example.com
    
    # Find all matches
    emails = re.findall(r'\w+@\w+\.\w+', text)
    print(emails)  # Output: ['john@example.com', 'jane@test.com']
    
    # Match from beginning
    pattern = r'^\d{3}-\d{4}$'  # Phone pattern
    print(re.match(pattern, '555-1234'))  # Match object
    print(re.match(pattern, 'Call 555-1234'))  # None
    
    # Substitute/replace
    text = "Hello World"
    new_text = re.sub(r'World', 'Python', text)
    print(new_text)  # Output: Hello Python
    
    # Split by pattern
    text = "apple,banana;cherry:date"
    fruits = re.split(r'[,;:]', text)
    print(fruits)  # Output: ['apple', 'banana', 'cherry', 'date']
    
    # Compile pattern (for reuse)
    pattern = re.compile(r'\d+')
    numbers = pattern.findall('Room 123, Floor 4, Unit 567')
    print(numbers)  # Output: ['123', '4', '567']
    
    # Groups
    text = "John Doe, Age: 30"
    match = re.search(r'(\w+)\s(\w+),\sAge:\s(\d+)', text)
    if match:
        print(match.group(1))  # Output: John
        print(match.group(2))  # Output: Doe
        print(match.group(3))  # Output: 30
        print(match.groups())  # Output: ('John', 'Doe', '30')
    
    # Named groups
    pattern = r'(?P<first>\w+)\s(?P<last>\w+)'
    match = re.search(pattern, 'John Doe')
    print(match.group('first'))  # Output: John
    print(match.groupdict())  # Output: {'first': 'John', 'last': 'Doe'}
    
    # Flags
    text = "Hello WORLD"
    print(re.findall(r'hello', text, re.IGNORECASE))  # Output: ['Hello']
    Python

    textwrap Module

    Common Functions:

    import textwrap
    
    # Wrap text
    long_text = "This is a very long line of text that needs to be wrapped to fit within a certain width for better readability."
    wrapped = textwrap.wrap(long_text, width=40)
    print(wrapped)
    # Output: ['This is a very long line of text that', 'needs to be wrapped to fit within a', ...]
    
    # Fill text (wrap and join)
    filled = textwrap.fill(long_text, width=40)
    print(filled)
    
    # Dedent (remove common leading whitespace)
    code = """
        def hello():
            print("Hello")
    """
    dedented = textwrap.dedent(code)
    print(dedented)
    
    # Indent
    text = "Line 1\nLine 2\nLine 3"
    indented = textwrap.indent(text, '  ')
    print(indented)
    # Output:
    #   Line 1
    #   Line 2
    #   Line 3
    
    # Shorten text
    long_text = "This is a very long piece of text"
    short = textwrap.shorten(long_text, width=20, placeholder='...')
    print(short)  # Output: This is a [...]
    Python

    Internet and Web Programming

    urllib Module

    Common Usage:

    from urllib import request, parse, error
    
    # Fetch URL
    try:
        response = request.urlopen('https://www.python.org')
        html = response.read().decode('utf-8')
        print(html[:100])  # Print first 100 characters
        print(response.status)  # Output: 200
        print(response.headers['Content-Type'])
    except error.URLError as e:
        print(f"Error: {e}")
    
    # POST request with data
    data = parse.urlencode({'key': 'value'}).encode()
    req = request.Request('https://httpbin.org/post', data=data)
    response = request.urlopen(req)
    print(response.read().decode())
    
    # Parse URL
    url = 'https://example.com/path?key=value&foo=bar'
    parsed = parse.urlparse(url)
    print(parsed.scheme)  # Output: https
    print(parsed.netloc)  # Output: example.com
    print(parsed.path)  # Output: /path
    print(parsed.query)  # Output: key=value&foo=bar
    
    # Parse query string
    params = parse.parse_qs('key=value&foo=bar')
    print(params)  # Output: {'key': ['value'], 'foo': ['bar']}
    
    # URL encode
    params = {'name': 'John Doe', 'age': 30}
    encoded = parse.urlencode(params)
    print(encoded)  # Output: name=John+Doe&age=30
    Python

    json Module

    Common Functions:

    import json
    
    # Python dict to JSON string
    data = {
        'name': 'John',
        'age': 30,
        'city': 'New York',
        'hobbies': ['reading', 'coding']
    }
    json_string = json.dumps(data)
    print(json_string)  # Output: {"name": "John", "age": 30, ...}
    
    # Pretty print JSON
    pretty_json = json.dumps(data, indent=2)
    print(pretty_json)
    
    # JSON string to Python dict
    json_string = '{"name": "John", "age": 30}'
    parsed = json.loads(json_string)
    print(parsed['name'])  # Output: John
    
    # Write JSON to file
    with open('data.json', 'w') as f:
        json.dump(data, f, indent=2)
    
    # Read JSON from file
    with open('data.json', 'r') as f:
        loaded = json.load(f)
        print(loaded)
    
    # Custom serialization
    from datetime import datetime
    
    def datetime_handler(obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        raise TypeError(f"Type {type(obj)} not serializable")
    
    data = {'timestamp': datetime.now()}
    json_str = json.dumps(data, default=datetime_handler)
    Python

    http.server Module

    Simple HTTP Server:

    from http.server import HTTPServer, SimpleHTTPRequestHandler
    import threading
    
    # Serve files from current directory
    def run_server():
        server = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
        print("Server running on http://localhost:8000")
        server.serve_forever()
    
    # Run in thread (for script demonstration)
    thread = threading.Thread(target=run_server, daemon=True)
    thread.start()
    
    # Or run from command line:
    # python -m http.server 8000
    Python

    File Formats and Data Persistence

    csv Module

    Common Usage:

    import csv
    
    # Write CSV
    data = [
        ['Name', 'Age', 'City'],
        ['John', 30, 'NYC'],
        ['Jane', 25, 'LA'],
        ['Bob', 35, 'Chicago']
    ]
    
    with open('people.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(data)
    
    # Write with DictWriter
    with open('people.csv', 'w', newline='') as f:
        fieldnames = ['Name', 'Age', 'City']
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerow({'Name': 'John', 'Age': 30, 'City': 'NYC'})
        writer.writerow({'Name': 'Jane', 'Age': 25, 'City': 'LA'})
    
    # Read CSV
    with open('people.csv', 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            print(row)  # Output: ['John', '30', 'NYC']
    
    # Read with DictReader
    with open('people.csv', 'r') as f:
        reader = csv.DictReader(f)
        for row in reader:
            print(row['Name'], row['Age'])  # Access by column name
    Python

    pickle Module

    Object Serialization:

    import pickle
    
    # Serialize (save) object
    data = {
        'name': 'John',
        'scores': [85, 90, 78],
        'metadata': {'class': 'A', 'year': 2025}
    }
    
    with open('data.pickle', 'wb') as f:
        pickle.dump(data, f)
    
    # Deserialize (load) object
    with open('data.pickle', 'rb') as f:
        loaded_data = pickle.load(f)
        print(loaded_data)
    
    # Serialize to bytes
    bytes_data = pickle.dumps(data)
    restored = pickle.loads(bytes_data)
    
    # Multiple objects
    with open('multi.pickle', 'wb') as f:
        pickle.dump(obj1, f)
        pickle.dump(obj2, f)
        pickle.dump(obj3, f)
    
    with open('multi.pickle', 'rb') as f:
        obj1 = pickle.load(f)
        obj2 = pickle.load(f)
        obj3 = pickle.load(f)
    Python

    sqlite3 Module

    Database Operations:

    import sqlite3
    
    # Connect to database (creates if doesn't exist)
    conn = sqlite3.connect('example.db')
    cursor = conn.cursor()
    
    # Create table
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            age INTEGER,
            email TEXT UNIQUE
        )
    ''')
    
    # Insert data
    cursor.execute("INSERT INTO users (name, age, email) VALUES (?, ?, ?)",
                   ('John Doe', 30, 'john@example.com'))
    
    # Insert multiple rows
    users = [
        ('Jane Smith', 25, 'jane@example.com'),
        ('Bob Johnson', 35, 'bob@example.com')
    ]
    cursor.executemany("INSERT INTO users (name, age, email) VALUES (?, ?, ?)", users)
    
    # Commit changes
    conn.commit()
    
    # Query data
    cursor.execute("SELECT * FROM users WHERE age > ?", (25,))
    rows = cursor.fetchall()
    for row in rows:
        print(row)  # Output: (1, 'John Doe', 30, 'john@example.com')
    
    # Fetch one row
    cursor.execute("SELECT * FROM users LIMIT 1")
    row = cursor.fetchone()
    print(row)
    
    # Use Row factory for dict-like access
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users")
    for row in cursor:
        print(row['name'], row['age'])
    
    # Update
    cursor.execute("UPDATE users SET age = ? WHERE name = ?", (31, 'John Doe'))
    conn.commit()
    
    # Delete
    cursor.execute("DELETE FROM users WHERE age < ?", (26,))
    conn.commit()
    
    # Close connection
    conn.close()
    
    # Context manager (auto-commits and closes)
    with sqlite3.connect('example.db') as conn:
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM users")
        print(cursor.fetchall())
    Python

    Concurrency and Parallelism

    threading Module

    Multi-threading:

    import threading
    import time
    
    # Simple thread
    def worker(name, duration):
        print(f"{name} starting")
        time.sleep(duration)
        print(f"{name} finishing")
    
    thread = threading.Thread(target=worker, args=('Thread-1', 2))
    thread.start()
    thread.join()  # Wait for thread to complete
    
    # Multiple threads
    threads = []
    for i in range(5):
        t = threading.Thread(target=worker, args=(f'Thread-{i}', 1))
        threads.append(t)
        t.start()
    
    for t in threads:
        t.join()
    
    # Thread with return value using Queue
    from queue import Queue
    
    def worker_with_result(n, result_queue):
        result = n * n
        result_queue.put(result)
    
    result_queue = Queue()
    thread = threading.Thread(target=worker_with_result, args=(5, result_queue))
    thread.start()
    thread.join()
    result = result_queue.get()
    print(result)  # Output: 25
    
    # Lock for thread safety
    lock = threading.Lock()
    counter = 0
    
    def increment():
        global counter
        for _ in range(100000):
            with lock:  # or lock.acquire() ... lock.release()
                counter += 1
    
    threads = [threading.Thread(target=increment) for _ in range(10)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    print(counter)  # Output: 1000000
    Python

    multiprocessing Module

    Process-based Parallelism:

    from multiprocessing import Process, Pool, Queue, Value, Array
    import os
    
    # Simple process
    def worker(name):
        print(f"{name} running in process {os.getpid()}")
    
    process = Process(target=worker, args=('Process-1',))
    process.start()
    process.join()
    
    # Process pool for parallel execution
    def square(n):
        return n * n
    
    with Pool(processes=4) as pool:
        results = pool.map(square, [1, 2, 3, 4, 5])
        print(results)  # Output: [1, 4, 9, 16, 25]
    
    # Shared memory
    def increment(shared_value, shared_array):
        shared_value.value += 1
        for i in range(len(shared_array)):
            shared_array[i] *= 2
    
    shared_val = Value('i', 0)  # Shared integer
    shared_arr = Array('i', [1, 2, 3, 4, 5])  # Shared array
    
    processes = [Process(target=increment, args=(shared_val, shared_arr)) for _ in range(5)]
    for p in processes:
        p.start()
    for p in processes:
        p.join()
    
    print(shared_val.value)  # Output: 5
    print(list(shared_arr))  # Output: [32, 64, 96, 128, 160]
    Python

    asyncio Module

    Async/Await:

    import asyncio
    
    # Simple async function
    async def say_hello(name, delay):
        await asyncio.sleep(delay)
        print(f"Hello, {name}!")
        return f"Greeted {name}"
    
    # Run single coroutine
    async def main():
        result = await say_hello("World", 1)
        print(result)
    
    asyncio.run(main())
    
    # Run multiple coroutines concurrently
    async def main_concurrent():
        results = await asyncio.gather(
            say_hello("Alice", 1),
            say_hello("Bob", 2),
            say_hello("Charlie", 1)
        )
        print(results)
    
    asyncio.run(main_concurrent())
    
    # Create tasks
    async def main_tasks():
        task1 = asyncio.create_task(say_hello("Task1", 2))
        task2 = asyncio.create_task(say_hello("Task2", 1))
    
        await task1
        await task2
    
    asyncio.run(main_tasks())
    
    # Async with timeout
    async def main_timeout():
        try:
            await asyncio.wait_for(say_hello("Timeout", 5), timeout=2.0)
        except asyncio.TimeoutError:
            print("Operation timed out!")
    
    asyncio.run(main_timeout())
    Python

    Development and Testing

    unittest Module

    Unit Testing:

    import unittest
    
    # Test class
    class TestMathOperations(unittest.TestCase):
    
        def setUp(self):
            # Run before each test
            self.a = 10
            self.b = 5
    
        def tearDown(self):
            # Run after each test
            pass
    
        def test_addition(self):
            result = self.a + self.b
            self.assertEqual(result, 15)
    
        def test_subtraction(self):
            result = self.a - self.b
            self.assertEqual(result, 5)
    
        def test_multiplication(self):
            result = self.a * self.b
            self.assertEqual(result, 50)
    
        def test_division(self):
            result = self.a / self.b
            self.assertAlmostEqual(result, 2.0)
    
        def test_division_by_zero(self):
            with self.assertRaises(ZeroDivisionError):
                result = self.a / 0
    
    # Run tests
    if __name__ == '__main__':
        unittest.main()
    
    # Common assertions:
    # assertEqual(a, b)
    # assertNotEqual(a, b)
    # assertTrue(x)
    # assertFalse(x)
    # assertIs(a, b)
    # assertIsNone(x)
    # assertIn(a, b)
    # assertIsInstance(a, SomeClass)
    # assertRaises(SomeException)
    Python

    argparse Module

    Command-line Argument Parsing:

    import argparse
    
    # Create parser
    parser = argparse.ArgumentParser(description='Process some integers.')
    
    # Add arguments
    parser.add_argument('integers', metavar='N', type=int, nargs='+',
                        help='an integer for the accumulator')
    parser.add_argument('--sum', dest='accumulate', action='store_const',
                        const=sum, default=max,
                        help='sum the integers (default: find the max)')
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='verbose output')
    parser.add_argument('-o', '--output', type=str, default='output.txt',
                        help='output file name')
    
    # Parse arguments
    args = parser.parse_args()
    
    # Use arguments
    print(args.accumulate(args.integers))
    if args.verbose:
        print(f"Writing to {args.output}")
    
    # Example usage:
    # python script.py 1 2 3 4 --sum -v
    # python script.py 1 2 3 4 -o results.txt
    Python

    Discover more from Altgr Blog

    Subscribe to get the latest posts sent to your email.

    Leave a Reply

    Your email address will not be published. Required fields are marked *