Commonly used built-in classes and methods in Python

1. Built-in class

Numeric types: int (integer), float (floating-point number), complex (complex number);

Sequence types: list, tuple, str, bytes (byte sequence), bytearray (mutable byte sequence), memoryview (memory view);

Collection types: set (mutable collection), frozenset (immutable collection); 

Mapping type: dict (dictionary);

Other: bool (a subclass of int), NoneType (accessed via type(None)).

2. Built-in methods

Built-in methods in Python are typically methods of a built-in data type class (such as str, list, dict, int, float, etc.). Each of Python’s built-in data types has its own class definition, which encapsulates operations related to the data type.

Note: Python’s built-in methods are typically implemented in C.

Python’s built-in methods are typically implemented in C and encapsulated within Python’s built-in data type classes. Since the Python interpreter (CPython) is written in C, the implementation of these built-in methods is highly efficient. For example, in CPython, strclass  upper()methods are implemented using C’s string manipulation functions. These low-level implementations are encapsulated in Python’s built-in data type classes, allowing users to directly call these methods without needing to understand the underlying implementation details.

Commonly used built-in methods for strings include: upper, lower, capitalize, tittle,  find ,  replace , format,  split , and  strip.

Commonly used built-in methods for lists include: append , insert, remove, pop, sort, reverse,  index , and count.

Commonly used built-in methods of dictionaries (dict): update, pop, popitem, clear, keys, values, items, get;

TIPS: Different business needs and different programming habits will lead to different commonly used methods. Gradually accumulate and build your own repository as needed.

2.1 Commonly used methods of the string class

Case conversion, find and replace, formatting, splitting and removing, conditional judgment, etc.

2.1.1 Case Conversion
  • .upper(): Converts all characters in a string to uppercase.
  • .lower(): Converts all characters in a string to lowercase.
  • .capitalize(): Converts the first character of the string to uppercase and the rest to lowercase.
  • .title(): Converts the first letter of each word in the string to uppercase.
2.1.2 Find and Replace
  • .find(sub): Returns subthe index of the first occurrence of the substring in the string, or returns 0 if not found -1.
  • .rfind(sub): Returns subthe index of the last occurrence of the substring in the string, or returns 0 if not found -1.
  • .replace(old, new)old: Replaces a substring in a string with new.
  • .index(sub): Retrieves whether a specified substring is contained, similar to find, but throws an exception if not found.
2.1.3 Formatting
  • .format(): Used for formatting strings.
2.1.4 Segmentation and Removal
  • .split(sep=None)sep: Splits the string into a list according to the specified delimiter . The default is to split by spaces.
  • .strip()Removes leading and trailing whitespace characters from a string.
  • .rstrip()Remove trailing whitespace characters from a string.
  • .lstrip()Removes leading whitespace characters from a string.
2.1.5 Conditional Judgment
  • .isdigit(): Determine if a string contains only numbers.
  • .isalpha(): Determine if a string contains only letters.
  • .isalnum(): Determine if a string contains only letters and numbers.
  • .isspace(): Determines whether a string contains only whitespace characters.
  • .startswith(prefix): Determines whether a string begins with prefixa certain character.
  • .endswith(suffix): Determines whether a string suffixends.

2.2 Commonly Used Methods of the List Class

Add and delete, sort and reverse order, search and count

2.2.1 Adding and Deleting
  • .append(x)Add an element to the end of the list x.
  • .extend(iterable): Adds elements from the iterable object to the end of the list.
  • .insert(i, x)iInsert an element at the specified position x.
  • .remove(x): Delete the first xelement in the list whose value is 0.
  • .pop(i=-1): Deletes and returns ithe element at the specified position in the list. By default, it deletes the last element.
2.2.2 Sorting and Reversing Order
  • .sort()Sort the list.
  • .reverse(): Reverse the order of elements in the list.
2.2.3 Searching and Counting
  • .index(x)x: Returns the index of the first element in the list whose value is .
  • .count(x): Returns xthe number of elements in the list whose value is .

2.3 Commonly Used Methods of the dict Class

Add and delete, search and traverse

2.3.1 Adding and Deleting
  • .update(iterable)Updates the dictionary with key-value pairs from the iterable object.
  • .pop(key): Deletes the key-value pair with the key in the dictionary keyand returns the corresponding value.
  • .popitem(): Removes and returns the last key-value pair in the dictionary (Python 3.7+ guarantees the order).
  • .clear()Clear the dictionary.
2.3.2 Searching and Traversal
  • .keys(): Returns a view of all keys in the dictionary.
  • .values(): Returns a view of all values ​​in the dictionary.
  • .items(): Returns a view of all key-value pairs in the dictionary.
  • .get(key, default=None)Returns keythe value of the key in the dictionary; if the key does not exist, it returns 0 default.

Built-in methods are provided for built-in classes and are not applicable to all classes. Different data types may use different methods even when performing similar operations.