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}}}\)
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 )
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.
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, 0float: 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
intdata type andfloatdata type is evaluated, the result is afloat.
- When evaluating expressions with
floatdata 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.
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 )
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.
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.
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:
-
Tell Python to look at the
mathmodule.
import math
A module is a grouping of similar functions. Themathmodule contains many useful math functions. -
Use the
sqrtfunction of themathmodule:
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.
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.
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
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?
Comparing strings
Strings can also be compared. When two strings are compared:
- They are compared alphabetically.
The stringabcomes beforeacor 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 stringabcomes beforeabcor is "less than"abc. - Uppercase letters come before lowercase letters.
The stringabcomes afterAbor 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 stringabcomes beforeacor 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 stringabcomes beforeabcor is "less than"abc. - Uppercase letters come before lowercase letters.
The stringabcomes afterAbor is "greater than"Ab.
Can you predict the output of the following code?
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.


