Skip to main content
Chemistry LibreTexts

4.2: Data Types

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

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    In many ways programming can be called data processing, and we need to look at data and variables before proceeding.  Data are the values that are assigned to a variable by the assignment operator (=) and there are different data types

    Data Types

    and we have a book 5.2 is devoted to Data Types and Structures and we will go into these in more detail as the course proceeds.  But let's start with three basic data types, string (these are like letters of the alphabet), integers and floats (have decimal values). We will see that these are stored differently in the computer, and they behave differently.

    In the following code we define a=b=c=d=1, and then we add a+b and then c+d, and the answers are different.  Do you know what is going on?  (Do not worry about the print statement, we will discuss that later).

    a="1"
    b="1"
    c=1
    d=1
    print(f"a+b={a+b}")
    print(f"c+d={c+d}")
    Hello world!

    a and b were variables assigned data of the type string, and so they were treated as letters and not numbers, and you effectively concatenated them getting 11, while c and d were numbers.  Python has a built-in function called "type" that will give you the type of data a variable is.

    a="1"
    b="1"
    c=1
    d=1
    print(f"a is type: {type(a)}")
    print(f"c is type: {type(c)}")
    
    Hello world!

    Notice how the output of the above code is

    a is type: <class 'str'>
    c is type: <class 'int'>

    Python is an Object Oriented Programming language and a class is a type of objects that has attributes (features) and methods (functions), and so a data type is actually more than just how you store a data value on a computer, and in fact there are sort of two data types, scalar and container.  Strings are actually a bit tricky in python as they are sort of both.

    Scalar (atomic) data types

    These are related to how the individual values are stored in a computer (see book 5 section 2). These are

    • str: string-single character in a sequence of characters of type string
    • float: floating point numbers (3.14, -0.005), 2.004...)
    • int: integer values (4, -3, 100...)
    • bool: Boolean values (True or False)

    Container (collection) data types

    These are collections of the other data types.  Note that string is actually in both forms, as 

    • string: sequence of characters of type string (these can be treated like a set)
    • list: ordered (mutable) collection of items
    • tuple: ordered (immutable) collection of items
    • set: unordered (mutable) collection of unique items
    • dictionary: mutable collection of key-value pairs

    Scope and Namespace

    When assigning a value to a variable there is a scope for which that value is valid, even within a program.  For example, an assignment within a function (local scope) is not valid outside of the function (global scope).  Look at the following code.  You would think you start with mass_solute =1 and then it changes to 2, but the function does not effect the value of the first assignment.  that is, you have two different variables with the same name.

    mass_solute=1
    def add_solute():
        mass_solute=2
        print(f"solute inside function: {mass_solute}")
    
    add_solute()
    print(f"solute outside function: {mass_solute}")
    
    Hello world!

    If you want the function to change the value of a variable defined outside of the function, you need to add the global command to extend the scope outside of the function

    mass_solute=1
    def add_solute():
        global mass_solute
        mass_solute=2
        print(f"solute inside function: {mass_solute}")
    
    add_solute()
    print(f"solute outside function: {mass_solute}")
    
            hello world
          

    Edit the following code for a titration so it works

    vol_titrant=0
    def add_titrant():
        
        vol_titrant += 1
        print(f"total vol titrant inside function is: {vol_titrant}")
    
    add_titrant()
    print(f"total vol titrant outside function: {vol_titrant}")
    Hello world!

    The problem with the above is that you are making a variable global within a function and this could cause problems if you use the function is some program where you were not aware of that.  A better way is to use the return statement and make the assignment based on that.

    vol_titrant=0
    def add_titrant():
        print(f"total vol titrant inside function is: {vol_titrant}")
        return vol_titrant+1
        
    vol_titrant = add_titrant()
    print(f"total vol titrant outside function: {vol_titrant}")
    
    
    Hello world!

    Where you want to use global variables is for constants, and these should be in upper case.  For example, your web address, or a constant like the number pi

    PI=3.14
    URL='https://ioct.tech'
    

     

    Local vs. Remote Data

    local 

     


    4.2: Data Types is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?