Skip to main content
Chemistry LibreTexts

2: Data Types and Structures

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

    A data type sort of has two different meanings.  The first relates to how the data is stored, that is all data as a sequence of bites (ones and zeros) and the way these are stored in computer memory depends on the data type.  So an integer number is stored differently than a decimal number, which in turn is stored differently than a letter of the alphabet.  In python there is a built in function called "type" and if you run the following code you will see that it returns a class value.

    print(type(1))
    print(type(1.0))
    print(type("1"))
    Hello world!

    Python is an object oriented programming language and a class is a type of object, which has associated attributes and functions (called methods) and so you can do different things with different data types.  For example 1+1=2 if the data is of class integer but 1+1=11 if the data is of type string.  Furthermore, there are sort of two types of data types, atomic and container (no pun intended).  An atomic data type refers to individual (atomic) values and how the data is stored in memory, while a container can be a collection of values and the different ways those are stored. In python a string is defined as both and that can be a bit confusing, with the first type being a letter and the second the letters of a word.  We will list them all here and come back to this section as needed.  

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

    Dynamic vs. Static Type

    In python you can convert the type of a variable after it has been defined, and this is known as dynamic type.  In many programming languages you can not change the type a variable's data is after you have assigneda value to the variable.

     

    In the following code we are using the functions int() and float() to convert values to integer or float.  

    print(f"Here one is a string{type('1')}")
    print(f"Here the string one is converted to an integer{type(int('1'))}\n")
    
    print(f"Here one is an integer{type(1)}")
    print(f"Here the integer one is converted to a float {type(float(1))}")
    Hello world!

    Here we are converting a number to a string

    print(1)
    print(type(1))
    
    print(str(1))
    print(type(str(1)))
            hello world
          

     

     


    This page titled 2: Data Types and Structures is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?