Course Outline (Part 41)

This final part serves as a quick reference cheatsheet for the most commonly used methods, functions, and exceptions in Python.


1. Built-in Functions List

Python has a set of built-in functions available without importing any modules.

  • print(): Prints to the standard output device.
  • type(): Returns the type of an object.
  • len(): Returns the length (number of items) of an object.
  • int(), float(), str(), bool(), list(), dict(), set(), tuple(): Casting/Constructor functions.
  • input(): Allows user input.
  • range(): Returns a sequence of numbers.
  • sum(): Sums the items of an iterator.
  • max(), min(): Returns the largest/smallest item in an iterable.
  • abs(): Returns the absolute value of a number.
  • round(): Rounds a number.
  • isinstance(): Returns True if a specified object is an instance of a specified object.

2. String Methods List

  • capitalize(): Converts the first character to upper case.
  • lower(), upper(): Converts a string to lower/upper case.
  • strip(): Returns a trimmed version of the string.
  • replace(): Returns a string where a specified value is replaced with a specified value.
  • split(): Splits the string at the specified separator, and returns a list.
  • join(): Joins the elements of an iterable to the end of the string.
  • find(): Searches the string for a specified value and returns the position.
  • startswith(), endswith(): Returns true if the string starts/ends with the specified value.

3. List Methods List

  • append(): Adds an element at the end of the list.
  • clear(): Removes all the elements from the list.
  • copy(): Returns a copy of the list.
  • count(): Returns the number of elements with the specified value.
  • extend(): Add the elements of a list (or any iterable), to the end of the current list.
  • index(): Returns the index of the first element with the specified value.
  • insert(): Adds an element at the specified position.
  • pop(): Removes the element at the specified position.
  • remove(): Removes the first item with the specified value.
  • reverse(): Reverses the order of the list.
  • sort(): Sorts the list.

4. Dictionary Methods List

  • clear(): Removes all the elements from the dictionary.
  • copy(): Returns a copy of the dictionary.
  • get(): Returns the value of the specified key.
  • items(): Returns a list containing a tuple for each key value pair.
  • keys(): Returns a list containing the dictionary’s keys.
  • pop(): Removes the element with the specified key.
  • popitem(): Removes the last inserted key-value pair.
  • update(): Updates the dictionary with the specified key-value pairs.
  • values(): Returns a list of all the values in the dictionary.

5. Tuple and Set Methods List

Tuple Methods:

  • count(): Returns the number of times a specified value occurs in a tuple.
  • index(): Searches the tuple for a specified value and returns the position.

Set Methods:

  • add(): Adds an element to the set.
  • clear(): Removes all elements.
  • discard() / remove(): Remove the specified item.
  • intersection(): Returns a set, that is the intersection of two or more sets.
  • union(): Return a set containing the union of sets.

6. File Methods List

  • close(): Closes the file.
  • read(): Returns the file content.
  • readline(): Returns one line from the file.
  • readlines(): Returns a list of lines from the file.
  • write(): Writes the specified string to the file.
  • writelines(): Writes a list of strings to the file.
  • seek(): Sets the file’s current position.

7. Exception List

  • Exception: Base class for all exceptions.
  • AttributeError: Raised when attribute reference or assignment fails.
  • ImportError: Raised when the imported module is not found.
  • IndexError: Raised when an index of a sequence is out of range.
  • KeyError: Raised when a key is not found in a dictionary.
  • NameError: Raised when a variable is not found in local or global scope.
  • TypeError: Raised when a function or operation is applied to an object of incorrect type.
  • ValueError: Raised when a function gets an argument of correct type but improper value.

8. Key Modules Cheatsheet

  • os: Operating system interfaces (file paths, directories).
  • sys: System-specific parameters and functions (CLI arguments, path).
  • math: Mathematical functions.
  • datetime: Basic date and time types.
  • json: JSON encoder and decoder.
  • re: Regular expression operations.
  • random: Generate pseudo-random numbers.
  • csv: CSV File Reading and Writing.
  • requests (Third-party): HTTP for Humans.

Discussion

Loading comments...