Skip to content

Managing Information

Managing Information



In This Chapter

▶ Understanding the Python view of data

▶ Using operators to assign, modify, and compare data

▶ Organizing code using functions

▶ Interacting with the user







hether you use the term information or data to refer to the content that applications manage, the fact is that you must provide some

means of working with it or your application really doesn’t have a purpose. Throughout the rest of the book, you see information and data used inter- changeably because they really are the same thing, and in real-world situ- ations, you’ll encounter them both, so getting used to both is a good idea. No matter which term you use, you need some means of assigning data to variables, modifying the content of those variables to achieve specific goals, and comparing the result you receive with desired results. This chapter addresses all three requirements so that you can start to control data within your applications.


It’s also essential to start working through methods of keeping your code understandable. Yes, you could write your application as a really long proce- dure, but trying to understand such a procedure is incredibly hard, and you’d find yourself repeating some steps because they must be done more than once. Functions are one way for you to package code so that it’s easier to understand and reuse as needed.


Applications also need to interact with the user. Yes, some perfectly usable applications are out there that don’t really interact with the user, but they’re extremely rare and don’t really do much, for the most part. In order to pro- vide a useful service, most applications interact with the user to discover how the user wants to manage data. You get an overview of this process in this chapter. Of course, you visit the topic of user interaction quite often throughout the book because it’s an important topic.



Controlling How Python Views Data

As discussed in Chapter 5, all data on your computer is stored as 0s and 1s. The computer doesn’t understand the concept of letters, Boolean values, dates, times, or any other kind of information except numbers. In addition, a computer’s capability to work with numbers is both inflexible and rela- tively simplistic. When you work with a string in Python, you’re depending on Python to translate the concept of a string into a form the computer can

understand. The storage containers that your application creates and uses in the form of variables tell Python how to treat the 0s and 1s that the computer has stored. So, it’s important to understand that the Python view of data isn’t the same as your view of data or the computer’s view of data — Python acts as an intermediary to make your applications functional.


To manage data within an application, the application must control the way in which Python views the data. The use of operators, packaging methods such  as functions, and the introduction of user input all help applications control data. All these techniques rely, in part, on making comparisons. Determining what to do next means understanding what state the data is in now as com- pared to some other state. If the variable contains the name John now, but you really want it to contain Mary instead, then you first need to know that it does in fact contain John. Only then can you make the decision to change the con- tent of the variable to Mary.



Making comparisons

Python’s main method for making comparisons is through the use of opera- tors. In fact, operators play a major role in manipulating data as well. The upcoming “Working with Operators” section discusses how operators work and how you can use them in applications to control data in various ways. Later chapters use operators extensively as you discover techniques for creating applications that can make decisions, perform tasks repetitively, and interact with the user in interesting ways. However, the basic idea behind operators is that they help applications perform various types of comparisons.


In some cases, you use some fancy methods for performing comparisons in an application. For example, you can compare the output of two functions (as described in the “Comparing function output” section, later in this chapter). With Python, you can perform comparisons at a number of levels so that you can manage data without a problem in your application. Using these tech- niques hides detail so that you can focus on the point of the comparison and define how to react to that comparison rather than become mired in detail.



Your choice of techniques for performing comparisons affects the manner in which Python views the data and determines the sorts of things you can do to manage the data after the comparison is made. All this functionality might seem absurdly complex at the moment, but the important point to remem- ber is that applications require comparisons in order to interact with data correctly.



Understanding how computers make comparisons

Computers don’t understand packaging, such as functions, or any of the other structures that you create with Python. All this packaging is for your benefit, not the computer’s. However, computers do directly support the concept of operators. Most Python operators have a direct corollary with a command that the computer understands directly. For example, when you ask whether one number is greater than another number, the computer can actually perform this computation directly, using an operator. (The upcoming section explains operators in detail.)


Some comparisons aren’t direct. Computers work only with numbers. So, when you ask Python to compare two strings, what Python actually does is compare the numeric value of each character in the string. For example, the letter A is actually the number 65 in the computer. A lowercase letter a has a different numeric value — 97. As a result, even though you might see ABC as being equal to abc, the computer doesn’t agree — it sees them as different because the numeric values of their individual letters are different.