Skip to main content
Chemistry LibreTexts

OOP and Python Data Types

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

     

    Data and type() command

    At this point in our class we are looking at data types in python, and we see both atomic (string, integer, float, Boolean) and container (list, string, tuple, dictionary, set) types and that the way the data is stored on the computer varies from type to type.  If we want to know the type of a peice of date we can use the type() command and print the response to the screen.  Run the following code

    mynumber = input("enter a number between 1 and 9: ")
    addition=mynumber+mynumber
    print(f"{mynumber}+{mynumber}={addition}")
    print(type(addition))
    print(25*"*")
    mynumber=float(mynumber)
    addition=mynumber+mynumber
    print(f"{mynumber}+{mynumber}={addition}")
    print(type(addition))
            hello world
          

    OK, what happened?  In the first line we used the input function to enter a number between 1 and 9 and assigned it to the variable "mynumber" and then we added it, but did not get the result of addition, so we used the type command to find out what was the data type, and discovered that the input function creates string variable types. We then converted the string to a float and got the answer we expected, and showed the data was now a float.  What we will see is the way the data is stored (digitized) in computer memory depends on the type of data it is, and that a function like addition does one thing if the number is a letter, and another if it is an actual number.

    So what is the difference between the number as a "string" or a "float". When we look closer at the output we see it states that the data type is really a python class, so what is a class? To answer this we need to look into what is Object Oriented Programming (OOP)

    Object Oriented Programming (OOP)

     


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

    • Was this article helpful?