Skip to content

Storing and Modifying Information

Storing and Modifying Information



In This Chapter

▶ Understanding data storage

▶ Considering the kinds of data storage

▶ Adding dates and times to applications







hapter 3 introduces you to CRUD, Create, Read, Update, and Delete — not that Chapter 3 contains cruddy material. This acronym provides an easy

method to remember precisely what tasks all computer programs perform with information you want to manage. Of course, geeks use a special term for information — data, but either information or data works fine for this book.


In order to make information useful, you have to have some means of storing it permanently. Otherwise, every time you turned the computer off, all your information would be gone and the computer would provide limited value. In

addition, Python must provide some rules for modifying information. The alter- native is to have applications running amok, changing information in any and every conceivable manner. This chapter is about controlling information — defining how information is stored permanently and manipulated by applica- tions you create.



Storing Information

An application requires fast access to information or else it will take a long time to complete tasks. As a result, applications store information in memory. However, memory is temporary. When you turn off the machine, the informa- tion must be stored in some permanent form, such as on your hard drive, a Universal Serial Bus (USB) flash drive, or a Secure Digital (SD) card. In addi- tion, you must also consider the form of the information, such as whether it’s a number or text. The following sections discuss the issue of storing informa- tion as part of an application in more detail.



Seeing variables as storage boxes

When working with applications, you store information in variables. A variable is a kind of storage box. Whenever you want to work with the information, you access it using the variable. If you have new information you want to store,  you put it in a variable. Changing information means accessing the variable first and then storing the new value in the variable. Just as you store things in boxes in the real world, so you store things in variables (a kind of storage box) when working with applications.


Computers are actually pretty tidy. Each variable stores just one piece of infor- mation. Using this technique makes it easy to find the particular piece of infor- mation you need — unlike in your closet, where things from ancient Egypt could be hidden. Even though the examples you work with in previous chapters don’t use variables, most applications rely heavily on variables to make working with information easier.



Using the right box to store the data

People tend to store things in the wrong sort of box. For example, you might find a pair of shoes in a garment bag and a supply of pens in a shoebox.

However, Python likes to be neat. As a result, you find numbers stored in one sort of variable and text stored in an entirely different kind of variable. Yes, you use variables in both cases, but the variable is designed to store a particular kind of information. Using specialized variables makes it possible to work with the information inside in particular ways. You don’t need to worry about the details just yet — just keep in mind that each kind of infor- mation is stored in a special kind of variable.


Python uses specialized variables to store information to make things easy for the programmer and to ensure that the information remains safe. However, computers don’t actually know about information types. All that the computer knows about are 0s and 1s, which is the absence or presence of a voltage. At

a higher level, computers do work with numbers, but that’s the extent of what computers do. Numbers, letters, dates, times, and any other kind of informa- tion you can think about all come down to 0s and 1s in the computer system. For example, the letter A is actually stored as 01000001 or the number 65. The computer has no concept of the letter A or of a date such as 8/31/2014.



Defining the Essential Python Data Types

Every programming language defines variables that hold specific kinds of information, and Python is no exception. The specific kind of variable is called a data type. Knowing the data type of a variable is important because it tells you what kind of information you find inside. In addition, when you want to store information in a variable, you need a variable of the correct data type to do it. Python doesn’t allow you to store text in a variable designed

to hold numeric information. Doing so would damage the text and cause problems with the application. You can generally classify Python data types as numeric, string, and Boolean, although there really isn’t any limit on just how you can view them. The following sections describe each of the standard Python data types within these classifications.



Putting information into variables

To place a value into any variable, you make an assignment using the assign- ment operator (=). Chapter 6 discusses the whole range of basic Python operators in more detail, but you need to know how to use this particular operator to some extent now. For example, to place the number 5 into a variable named myVar, you type myVar = 5 and press Enter at the Python prompt. Even though Python doesn’t provide any additional information to you, you can always type the variable name and press Enter to see the value it contains, as shown in Figure 5-1.



Figure 5-1: Use the assignment operator to place infor­ mation into a variable.



Understanding the numeric types

Humans tend to think about numbers in general terms. We view 1 and 1.0 as being the same number — one of them simply has a decimal point. However, as far as we’re concerned, the two numbers are equal and we could easily use them interchangeably. Python views them as being different kinds of numbers



because each form requires a different kind of processing. The following sec- tions describe the integer, floating-point, and complex number classes of data types that Python supports.



Any whole number is an integer. For example, the value 1 is a whole number, so it’s an integer. On the other hand, 1.0 isn’t a whole number; it has a deci- mal part to it, so it’s not an integer. Integers are represented by the int data type.


As with storage boxes, variables have capacity limits. Trying to stuff a value that’s too large into a storage box results in an error. On most plat- forms, you can store numbers between –9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 within an int (which is the maximum value that fits in a 64-bit variable). Even though that’s a really large number, it isn’t infinite.


When working with the int type, you have access to a number of interesting features. Many of them appear later in the book, but one feature is the ability to use different numeric bases:


  • Base 2: Uses only 0 and 1 as
  • Base 8: Uses the numbers 0 through
  • Base 10: Uses the usual numeric
  • Base 16: Is also called hex and uses the numbers 0 through 9 and the let- ters A through F to create 16 different possible


To tell Python when to use bases other than base 10, you add a 0 and a spe- cial letter to the number. For example, 0b100 is the value one-zero-zero in base 2. Here are the letters you normally use:


  • b: Base 2
  • o: Base 8
  • x: Base 16


It’s also possible to convert numeric values to other bases using the bin(), oct(), and hex() commands. So, putting everything together, you can see how to convert between bases using the commands shown in Figure 5-2.

Try the command shown in the figure yourself so that you can see how the various bases work. Using a different base actually makes things easier in many situations, and you’ll encounter some of those situations later in the book. For now, all you really need to know is that integers support different numeric bases.










Figure 5-2: Integers have many interesting features, including the capabil­ ity to use different numeric bases.



Floating-point values

Any number that includes a decimal portion is a floating-point value. For example, 1.0 has a decimal part, so it’s a floating-point value. Many people get confused about whole numbers and floating-point numbers, but the difference is easy to remember. If you see a decimal in the number, then it’s a floating- point value. Python stores floating-point values in the float data type.


Floating-point values have an advantage over integer values in that you can store immensely large or incredibly small values in them. As with integer vari- ables, floating-point variables have a storage capacity. In their case, the maxi- mum value that a variable can contain is ±1.7976931348623157 × 10308 and the minimum value that a variable can contain is ±2.2250738585072014 × 10-308 on most platforms.


When working with floating-point values, you can assign the information to the variable in a number of ways. The two most common methods are to pro- vide the number directly and to use scientific notation. When using scientific notation, an e separates the number from its exponent. Figure 5-3 shows both methods of making an assignment. Notice that using a negative exponent results in a fractional value.








Figure 5-3: Floating­ point values

provide multiple assignment techniques.



Complex numbers

You may or may not remember complex numbers from school. A complex number consists of a real number and an imaginary number that are paired together. Just in case you’ve completely forgotten about complex numbers, you can read about them at complex-numbers.html. Real-world uses for complex numbers include:


  • Electrical engineering
  • Fluid dynamics
  • Quantum mechanics
  • Computer graphics
  • Dynamic systems


Complex numbers have other uses, too, but this list should give you some ideas. In general, if you aren’t involved in any of these disciplines, you prob- ably won’t ever encounter complex numbers. However, Python is one of the few languages that provides a built-in data type to support them. As you progress through the book, you find other ways in which Python lends itself especially well to science and engineering.


The imaginary part of a complex number always appears with a j after it. So, if you want to create a complex number with 3 as the real part and 4 as the imaginary part, you make an assignment like this:



If you want to see the real part of the variable, you simply type myComplex. real at the Python prompt and press Enter. Likewise, if you want to see the imaginary part of the variable, you type myComplex.imag at the Python prompt and press Enter.




Understanding the need for multiple number types


A lot of new developers (and even some older ones) have a hard time understanding why there is a need for more than one numeric type. After all, humans can use just one kind of number. To understand the need for multiple number types, you have to understand a little about how a computer works with numbers.

An integer is stored in the computer as simply a series of bits that the computer reads directly. A value of 0100 in binary equates to a value of 4 in decimal. On the other hand, numbers that have decimal points are stored in an entirely different manner. Think back to all those classes you slept through on exponents in school — they actu­ ally come in handy sometimes. A floating­point number is stored as a sign bit (plus or minus), mantissa (the fractional part of the number), and exponent (the power of 2). (Some texts use the term significand in place of mantissa — the terms are interchangeable.) To obtain the floating­point value, you use the equation:

Value = Mantissa * 2^Exponent

At one time, computers all used different floating­point representations, but they all use the IEEE­754 standard now. You can read about this standard at http://grouper. A full explana­ tion of precisely how floating­point numbers work is outside the scope of this book, but

you can read a fairly understandable descrip­ tion at http://www.cprogramming. com/tutorial/floating_point/ understanding_floating_point_ representation.html. Nothing helps you understand a concept like playing with the values. You can find a really interesting float­ ing­point number converter at http://www. IEEE754.html, where you can click the individual bits (to turn them off or on) and see the floating­point number that results.

As you might imagine, floating­point num­ bers tend to consume more space in memory because of their complexity. In addition, they use an entirely different area of the processor — one that works more slowly than the part used for integer math. Finally, integers are precise, as contrasted to floating­point numbers, which can’t precisely represent some numbers, so you get an approximation instead. However, floating­ point variables can store much larger numbers. The bottom line is that decimals are unavoid­ able in the real world, so you need floating­point numbers, but using integers when you can reduces the amount of memory your application consumes and helps it work faster. There are many trade­offs in computer systems, and this one is unavoidable.