# 4.2: Data Types

$$\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}}}$$

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
mass_solute=2
print(f"solute inside function: {mass_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
global mass_solute
mass_solute=2
print(f"solute inside function: {mass_solute}")

print(f"solute outside function: {mass_solute}")

        hello world


Edit the following code for a titration so it works

vol_titrant=0

vol_titrant += 1
print(f"total vol titrant inside function is: {vol_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
print(f"total vol titrant inside function is: {vol_titrant}")
return vol_titrant+1

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.