Skip to main content
Workforce LibreTexts

3: Data Types

  • Page ID
    45256
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

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

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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{\longvect}{\overrightarrow}\)

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

    \(\newcommand{\ket}[1]{\left| #1 \right>}\)
    \(\newcommand{\bra}[1]{\left< #1 \right|}\)
    \(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
    \(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
    \(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
    \(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
    \(\newcommand{\expect}[1]{\langle #1 \rangle}\)
    \(\newcommand{\dfn}[1]{\emph{\textbf{#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}\)

    We've seen that it's good to be able to store intermediate results in a program, so that we can use these results later in the program. Now we'll also see why it's important to know the type of data being stored. There are 4 main data types that we'll work with:

    • Numbers
    • Strings
    • Sequences
    • Tables

    So far we've seen numbers and strings at work. In the print statement:

    print("The 2 numbers are:", 5, "and", 6)

    There are four inputs to the print function:

    • two text strings with quotes: "The numbers are:", "and"
    • two numbers: 5, 6

    The reason why we want to be aware of the data type of a variable is because Python has many useful functions for each type of data. Recall that a function is a block of code that does some work. If we know the data type of a variable, then we can take advantage of these useful functions and ask Python to do the work on the data for us, so that we don't have to write code to do the work ourselves.

    To check the data type of a variable, we can use the function type:

    type( variable_name )

    num1 = 82
    num2 = 7.5
    
    print("Type of num1 data:", type(num1))
    print("Type of num2 data:", type(num2))
    Output?

    The data type int means that the data are integers or whole numbers.
    The data type float means that the data are floating point numbers or decimal numbers.

    # Can you predict the output of this code?
    num1 = num2
    print("Type of num1 data:", type(num1))
    print("Type of num2 data:", type(num2))
    Output?
    Type of num1 data: <class 'float'>
    Type of num2 data: <class 'float'>
    

    Numbers and Strings

    It's easy to differentiate between the number data type and string data type.

    • A number is made of one or more digits 0-9, and an optional decimal point .
    • A string is a sequence of text characters that are surrounded by double quotes or single quotes

    Numbers and Their Properties

    Python divides the numbers into two basic types:

    • int: short for integers, includes all whole numbers such as: 12, -4, 67823, -37, 0
    • float: short for floating point numbers, includes all numbers with a decimal point such as: 5.3813, -0.002841, 2683.39, 0.0

    Evaluating Expressions with Numbers

    • When an expression with both int data type and float data type is evaluated, the result is a float.
    num1 = 6.2
    num2 = 5
    difference = num1 - num2
    print("Type of num1:", type(num1))
    print("Type of num2:", type(num2))
    print("Type of the difference:", type(difference))
    Output?
    • When evaluating expressions with float data types, the result may be off by the last one or two digits. This is because some decimal numbers can have infinitely many digits, but data on a computer can only be stored with a finite number of bits (typically 64 bits), so some decimal numbers will have a tiny rounding error.

    For most calculations in data science, the tiny rounding error is not a problem since the input data is limited to a certain number of significant digits, therefore we always round the output to the same number of significant digits, which means the last one or two digits in the output can be ignored.

    import math
    
    # Suppose we have an original input number which is precise to
    # 1 digit after the decimal point:
    num = 2.0
    
    # Using Python's square root function, we find the square root of num.
    # Note that we don't need to write code to calculate the square root,
    # which involves many lines of code and a complex algorithm.
    # This is because we recognize that num is a number data type, and
    # Python already provides the square root function for numbers.
    square_root = math.sqrt(num)
    
    # Now we take the square root and square it, which mathematically
    # should result in the exact original num value:
    print("The square is", square_root ** 2)
    Output?
    The square is 2.0000000000000004
    

    We see that the output is not exactly the same as the original num value. However, since num has a precision of one digit after the decimal point, if we also consider only one digit after the decimal point for the output, then the output exactly matches num.

    Common Functions for Numbers

    Python has many useful functions for the number data type. We'll cover some of the most common ones here and we'll discuss other functions as we continue further into data science.

    Rounding a Number

    To round a number to a certain number of digits after the decimal point, we use the round function:

    round ( input number, number of digits after decimal point )

    # Continuing with the squaring of the square root calculation above,
    # Original print statement:
    print("The square is", square_root ** 2)
    
    # With rounding to 1 digit after the decimal point:
    print("With rounding, the square is", round(square_root ** 2, 1))
    Output?
    The square is 2.0000000000000004
    With rounding, the square is 2.0
    

    Absolute Value of a Number

    To take the absolute value, or to find the positive component of a number, we use the abs function.

    abs( input number )

    A common use of the absolute value in data science is to find the difference between two numbers.

    num1 = 2.5
    num2 = 3
    # What's the difference between the 2 numbers?
    
    # Without using the abs function, we could end up with 2 different results,
    # depending on how the difference is calculated:
    print("The difference is", num1 - num2)
    print("The difference is", num2 - num1)
    
    print("\nWith absolute value:")
    # But if we use the absolute value of result, then we always get the
    # true difference between the 2 numbers:
    print("The difference is", abs(num1 - num2))
    print("The difference is", abs(num2 - num1))
    Output?
    The difference is -0.5
    The difference is 0.5
    
    With absolute value:
    The difference is 0.5
    The difference is 0.5
    

    Largest and Smallest of Several Numbers

    When we have several numbers and want to find the largest or smallest of them, we can use the max and min functions.

    max( two or more input numbers )
    min( two or more input numbers )

    where the input numbers are separated by commas.

    print("The largest value of 1,5,0,-2,4 is", max(1,5,0,-2,4))
    print("The smallest value of 1,5,0,-2,4 is", min(1,5,0,-2,4))
    Output?
    The largest value of 1,5,0,-2,4 is 5
    The smallest value of 1,5,0,-2,4 is -2
    

    Square Root of a Positive Number

    Finding the square root of a positive number takes two steps:

    1. Tell Python to look at the math module.
      import math
      A module is a grouping of similar functions. The math module contains many useful math functions.

    2. Use the sqrt function of the math module:
      math.sqrt( input number )

    Note: Finding the square root of a negative number is part of a branch of math called Complex Analysis, which is not used in beginning data science.

    import math
    
    num = 5
    print("The square root of num:", math.sqrt(num))
    print("The square root of num:", round(math.sqrt(num),2))
    Output?
    The square root of num: 2.23606797749979
    The square root of num: 2.24
    

    Strings and Their Properties

    Any text enclosed in double quotes or single quotes is considered a string data type in Python. The string data type is shown as str in Python.

    # Number which is an int data type
    num = 5
    print(num, "is type", type(num))
    
    # Number which is a string data type
    num = "5"
    print(num, "is type", type(num))
    
    # Number which is a string data type
    num = '5'
    print(num, "is type", type(num))
    
    # Strings typically are one or more characters, making up words or sentences
    word = "Hello"
    sentence = "Hello, World!"
    print(word, "is type", type(word))
    print(sentence, "is type", type(sentence))
    Output?
    5 is type <class 'int'>
    5 is type <class 'str'>
    5 is type <class 'str'>
    Hello is type <class 'str'>
    Hello, World! is type <class 'str'>
    

    Common Functions for Strings

    Just like with numbers, Python provides many useful functions for strings. Here we cover some of the most common ones.

    Joining Two Strings

    To join two strings together, one after the other, is to concatenate the strings. To concatenate two strings, we use the + operator, just like with numbers, but the result is not an addition, it's a concatenation of the strings.

    first string + second string

    string1 = "Python"
    string2 = "Language"
    
    print("Concatenate:", string1 + string2)
    print("Concatenate with space in the middle:", string1 + " " + string2)
    print("Concatenate with a new string in the middle:", string1 + ' **--** ' + string2)
    Output?
    # Can you predict the output of this program?
    
    str1 = "hello"
    str2 = "good bye"
    output_string = str1 + " and " + str2
    print(output_string)
    Output?
    hello and good bye
    

    Comparisons

    In addition to doing math calculations with numbers, many times we need to compare numbers when we analyze the data. For example, if 90% is a cutoff for an A grade, we will need to compare a student's overall percentage with 90 to see whether the student earned an A grade.

    Just like with arithmetic operators, the Python language also uses the same relational operators or comparison operators as math when comparing two numbers or two strings.

    Relational operators

    There are 6 relational operators:

    > : greater than
    < : less than
    >= : greater than or equal to
    <= : less than or equal to
    == : equal to
    != : not equal to

    When Python evaluates a relational expression, the result is the value True or False.

    Comparing numbers

    Comparing numbers works as expected in math.
    Can you predict the output of the following code?

    num1 = 5
    num2 = 8
    
    print("num1 > num2?", num1 > num2)
    print("num1 < num2?", num1 < num2)
    print("num1 >= num1 - 1?", num1 >= num1 - 1)
    print("num1 <= num1?", num1 <= num1)
    print("num1 == num2?", num1 == num2)
    print("num1 != num2?", num1 != num2)
    Output?

    Comparing strings

    Strings can also be compared. When two strings are compared:

    • They are compared alphabetically.
      The string ab comes before ac or is "less than" ac.
    • If the two strings have the same starting characters, but one string has extra characters at the end, then the shorter string comes before the longer string or is "less than" the longer string.
      The string ab comes before abc or is "less than" abc.
    • Uppercase letters come before lowercase letters.
      The string ab comes after Ab or is "greater than" Ab.

    Can you predict the output of the following code?

    num1 > num2? False
    num1 < num2? True
    num1 >= num1 - 1? True
    num1 <= num1? True
    num1 == num2? False
    num1 != num2? True
    

    Comparing strings

    Strings can also be compared. When two strings are compared:

    • They are compared alphabetically.
      The string ab comes before ac or is "less than" ac.
    • If the two strings have the same starting characters, but one string has extra characters at the end, then the shorter string comes before the longer string or is "less than" the longer string.
      The string ab comes before abc or is "less than" abc.
    • Uppercase letters come before lowercase letters.
      The string ab comes after Ab or is "greater than" Ab.

    Can you predict the output of the following code?

    string1 = "Python"
    string2 = "Programming"
    string3 = "PYthon"
    string4 = "PY"
    
    print("string1 < string2?", string1 < string2)
    print("string1 < string3?", string1 < string3)
    print("string3 < string4?", string3 < string4)
    Output?
    string1 < string2? False
    string1 < string3? False
    string3 < string4? False
    

    Summary

    In this chapter we learn some useful Python functions for the number and string data types so that we can keep our coding short and still do some complex work. For example, using math.sqrt(num) is much shorter than writing code to calculate the square root of num.

    However, data science is a field that works with large datasets, so in the next chapter we will discover two more data types that make it much easier to work with large datasets: sequences and tables. These data types can store many data values under one variable name, making it easier for us to work with large datasets.


    This page titled 3: Data Types was last modified on Sat, 26 Sep 2026 07:26:57 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Clare Nguyen.