\documentclass[../../book.tex]{subfiles}
\graphicspath{{\subfix{.}}}
\begin{document}

\chapter{Temporary Data Storage in the Memory}

Programs routinely need to store data for future use, whether this data is intermediate results of a computation, data acquired from an instrument or input, settings and preferences controlling program appearance and execution, the values of common constants, or any number of other things.  Storage devices can be broadly classified as \textbf{volatile} or \textbf{non-volatile}, which classify whether or not the data will still be available after the program ends or the computer is turned off.  

In this chapter, we will deal with volatile storage, data that is known only to specific programs and that disappears when that program ceases to execute.  Most commonly, this takes the form of \textbf{main memory}, also commonly referred to as Random Access Memory (RAM), or colloquially, just memory.  At the time of this writing, a typical consumer desktop computer has 8 gigabytes (GB, 1 billion bytes), which is a technical upper limit to how much data the computer can store temporarily, although in practice, computers can annex some permanent storage to make a fake ``virtual memory" if needed.

\section{Memory Structure}

In the abstract, you can think of main memory as simply a sea of bits that the computer can use as it sees fit.  This sea is divided into one-byte chunks (1 byte = 8 bits), where each byte has a corresponding ``address" that the computer uses to access the data in a given chunk or overwrite data that is already there.  Addresses are simply binary numbers, typically (but not always) the number of bits used by your computer processor (64-bit is the most common as of this writing).  This number of bits sets a theoretical upper limit on the size of your memory, since you will run out of binary numbers to use as an address after $2^{64}$.  Practically, for most of us, this is not a concern, as that works out to more than 18 million TB!  However, even relatively recently, 32 bit processors were common, with a theoretical limit of 4 GB.

Memory addresses are commonly expressed as hexadecimal numbers, where each digit represents four bytes.  So, a 64-bit memory address can be written as \texttt{0x8f397a395dbe06b6}, where \texttt{0x} signifies that we should expect a hexadecimal number and the memory address appears as a 16-digit, 64-bit hexadecimal number.  You may see such numbers flash on your screen when your computer encounters a particularly nasty error.  In principle, these memory address numbers in the error message carry information that could be used to debug the error; in practice, they carry only sadness.

\section{Storing Data in Memory}

It might seem that a lot of work will have to go into remembering what data is stored in what memory address.  Fortunately, this can be abstracted using by using \textbf{variables}, which essentially are mnemonics that you get to define that remember the addresses for you.  Don't confuse variables in coding with variables in math, there are some important differences.  In coding, variables are bits of text that (hopefully) succinctly describes the nature of the information that is stored in a specific part of the memory.  

In an abstract sense, you can think of variables as ``holding'' data for you, even if that is not really what is happening behind the scenes.  A variable must always have some value, and as a programmer you can either use that value in a computation, create a new variable with some value, or replace the current value in a pre-existing variable with a new one (in most cases).  

Pointing analogy - key is behavior is different in different scenarios.  Introduce mutability and immutability

The two main uses of variables are in \textbf{expressions}, which are just mathematical instructions, and \textbf{assignment operations}, which produce a result that you want the computer to store at least temporarily.  In Chapter \ref{}, you can see how to use the values in a variable, by writing a mathematical expression in which the variable's name is used rather than explicitly writing its value.  To create or change the value stored in a variable, you must use as assignment operation, in which the variable name into which you would like to store the result is always on the left hand side of the assignment operator (equals sign), and the expression you would like to evaluate is on the right hand side.  When executing this line of code, the computer will first evaluate the expression on the right hand side, and then store the resulting value in the memory referred to by the variable on the left hand side.

\section{Type of Data Stored in a Variable}

In some programming languages, you have to decide what type of data (i.e. text, numeric values, etc., more on that in Chapter \ref{}) will be stored in the variable when you first create it, a concept called \textbf{static typing}.  However, in Python, the type of data that can be stored in a variable can change.  You can assign the number 1.507 to the variable \texttt{sample\_mass} and then later change that value to the text \texttt{`puppies`} and Python will not complain.  This can be both a good and a bad thing, but it is something you should be aware of.

\section{Mutability}

We will get into more detail about this in later chapters, but for now, you should know that it is not always possible to change the value in a variable after it is created.  These types of variables are called \textbf{immutable}.  Conversely, \textbf{mutable} variables can be changed later.  Why would anyone want to not be able to change the value in a variable?  There are a number of reasons, most of which involve making it difficult to unintentionally introduce an error into your program!  For now, we will just appreciate that this is the case and not ask why.

\section{Rules for Variable Names}

You can give the variable whatever (within a few important rules) name you want, like \texttt{Avogadro} to hold Avogadro's number, for instance.  We will discuss the best practices for variable naming in Chapter \ref{}, but here we will lay out the ground rules (at least for Python):

\begin{itemize}

    \item Lower and upper case letters and numbers are all allowed in variable names.

    \item Variable names are case-sensitive, i.e. \texttt{Mass} is different than \texttt{mass}.
    
    \item Variable names can not include spaces.  Spaces tell the computer to expect a new variable name, operator, or other entity.  Use `\_` in place of spaces, for instance, \texttt{sample\_mass} if you want the variable name to be ``sampe mass."

    \item Variable names can not contain any special characters other than `\_` as these are mostly used as operators or other signifiers of the structure of an expression.

    \item Variable names must begin with a letter or `\_` instead of a number.
    
\end{itemize}



\end{document}