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.
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.
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.
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.