Skip to main content
Chemistry LibreTexts

3: Functions

  • Page ID
    282805
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    There are four types of Functions in Python

    1. Default (system) Functions
    2. Imported Functions (called from libraries or packages)
    3. User Defined Functions
    4. Anonymous Fuctions (lambda functions

    Functions and Methods

    Methods are Functions that are part of a class, and can be accessed when you access an object of the class.  All methods are functions, but not all functions are methods.

    Default Functions

    print()

    type() - tells the type of data a variable is

    Methods

    Methods are like functions that apply to an object.  Each object has a type (str, float, int, list...), and different types have different methods.

    List methods

    consider the list "molecules" that has aspirin (aspirin is the element of the list you want the index for)

    molecule =["aspirin","caffeine","aspirin"]
    print(molecule)
    print(type(molecule))
    print(molecule.index("aspirin"))
    print(molecule.count("aspirin"))
    molecule.append("acetic acid")
    print(molecule)

     

    String methods

    molecule ="aspirin"
    print(molecule.capitalize())
    print(molecule.replace("in","ins"))
    print(molecule.index("r"))
     

     

     

     


    3: Functions is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?