Skip to main content
Chemistry LibreTexts

2: Built-In Functions

  • Page ID
    419687
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Built-in functions are loaded into memory when you start Python, and are available to use without having to import them

    A list of built-in functions can be found at docs.python.org.

    We will look at some common built-in functions.

    float()

    • convert string or integer to a floating decimal number
    a="1"
    print(a)
    print(type(a))
    a=float(a)
    print(10*"*")
    print(a)
    print(type(a))
    Hello world!

     

    input()

    • Allows you to input a value
    • All inputs are strings
    a=input("Enter a number")
    print(a)
    print(type(a))
    b=input("write a sentence")
    print(b)
    Enter a number 3
    

     

    int()

    • Converts a string or float to an integer
    a="1"
    print(a)
    print(type(a))
    a=int(a)
    print(10*"*")
    print(a)
    print(type(a))
    Hello world!

     

    len()

    • returns length of an object
    print(len('hello world'))
    print(len("123456789"))
            hello world
          

     

    Math Functions

    abs()

    • Returns absolute value of a number
    print(abs(-34.5))
    print(abs(34.5))
            hello world
          

    pow(base,exponent)

    • returns base to the power of the exponent
    print(pow(2,3))
    print(pow(10,3))
            hello world
          

    round()

    • Rounds up or down a number
    print(round(33.545))
    print(round(33.545,1))
    print(round(33.545,2))
    print(round(33.545,-1))
    Hello world!

    max()

    • gives largest item of an iterable, or smallest of two or more values
      print(max([2,8,4]))
      print(max(["alpha","gamma","beta"]))
      Hello world!

       

    min()

    • gives smallest item of an iterable, or smallest of two or more values
    print(min([2,8,4]))
    print(min(["alpha","gamma","beta"]))
    Hello world!

     

    print()

    • strings must be in parenthesis
    • \n gives new line
    • \t is tab
    • multiple format options
    Note: Print Formatting

    Note, there are multiple ways you can format the output of the print statement and because this is very important we have a section devoted purely to print formatting.

    str()

    • converts an integer or floating decimal to a string

    type()

    • returns the type of an object

    This page titled 2: Built-In Functions is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?