Introduction to Computers and Python
INTRODUCTION
Welcome to Python—one of the world’s most widely used computer programming languages and, according to the Popularity of Programming Languages (PYPL) Index, the world’s most popular.
- https://pypl.github.io/PYPL.html (as of January 2020).
Here, we introduce terminology and concepts that lay the ground-work for the Python programming you’ll learn in Chapters 2–10 and the bigdata, artificialintelligence and cloud-based case studies we present in Chapters 11–16.
We’ll review object-oriented programming terminology and concepts. You’ll learn why Python has become so popular. We’ll introduce the Python Standard Library and various data-science libraries that help you avoid “reinventing the wheel.” You’ll use these libraries to create software objects that you’ll interact with to perform significant tasks with modest numbers of instructions.
Next, you’ll work through three test-drives showing how to execute Python code:
- In the first, you’ll use IPython to execute Python instructions interactively and immediately see their results.
- In the second, you’ll execute a substantial Python application that will display an animated bar chart summarizing rolls of a sixsided die as they occur. You’ll see the “ Law of Large Numbers” in action. In Chapter 6, you’ll build this application with the Matplotlib visualization library.
- In the last, we’ll introduce Jupyter Notebooks using JupyterLab—an interactive, web-browser-based tool in which you can conveniently write and execute Python instructions. Jupyter Notebooks enable you to include text, images, audios, videos, animations and code.
In the past, most computer applications ran on standalone computers (that is, not networked together). Today’s applications can be written with the aim of communicating among the world’s billions of computers via the Internet. We’ll introduce the Cloud and the Internet of Things (IoT), laying the ground-work for the contemporary applications you’ll develop in Chapters 11–16.
You’ll learn just how big “big data” is and how quickly it’s getting even bigger. Next, we’ll present a bigdata case study on the Waze mobile navigation app, which uses many current technologies to provide dynamic driving directions that get you to your destination as quickly and as safely as possible. As we walk through those technologies, we’ll mention where you’ll use many of them in this article. The chapter closes with our first Intro to Data Science section in which we discuss a key intersection between computer science and data science—artificial intelligence.
A QUICK REVIEW OF OBJECT TECHNOLOGY BASICS
As demands for new and more powerful software are soaring, building software quickly, correctly and economically is important. Objects, or more precisely, the classes objects come from, are essentially reusable software components. There are date objects, time objects, audio objects, video objects, automobile objects, people objects, etc. Almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating). Softwaredevelopment groups can use a modular, object-oriented design-andimplementation approach to be much more productive than with earlier popular techniques like “structured programming.” Object-oriented programs are often easier to understand, correct and modify.
Automobile as an Object
To help you understand objects and their contents, let’s begin with a simple analogy. Suppose you want to drive a car and make it go faster by pressing its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to design it. A car typically begins as engineering drawings, similar to the blueprints that describe the design of a house. These drawings include the design for an accelerator pedal.
The pedal hides from the driver the complex mechanisms that make the car go faster, just as the brake pedal “hides” the mechanisms that slow the car, and the steering wheel “hides” the mechanisms that turn the car. This enables people with little or no knowledge of how engines, braking and steering mechanisms work to drive a car easily. Just as you cannot cook meals in the blueprint of a kitchen, you cannot drive a car’s engineering drawings. Before you can drive a car, it must be built from the engineering drawings that describe it. A completed car has an actual accelerator pedal to make it go faster, but even that’s not enough—the car won’t accelerate on its own (hopefully!), so the driver must press the pedal to accelerate the car.
Methods and Classes
Let’s use our car example to introduce some key object-oriented programming concepts. Performing a task in a program requires a method. The method houses the program statements that perform its tasks. The method hides these statements from its user, just as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster. In Python, a program unit called a class houses the set of methods that perform the class’s tasks. For example, a class that represents a bank account might contain one method to deposit money to an account, another to withdraw money from an account and a third to inquire what the account’s balance is. A class is similar in concept to a car’s engineering drawings, which house the design of an accelerator pedal, steering wheel, and so on.
Instantiation
Just as someone has to build a car from its engineering drawings before you can drive a car, you must build an object of a class before a program can perform the tasks that the class’s methods define. The process of doing this is called instantiation. An object is then referred to as an instance of its class.
Reuse
Just as a car’s engineering drawings can be reused many times to build many cars, you can reuse a class many times to build many objects. Reuse of existing classes when building new classes and programs saves time and effort. Reuse also helps you build more reliable and effective systems because existing classes and components often have undergone extensive testing, debugging and performance tuning. Just as the notion of interchangeable parts was crucial to the Industrial Revolution, reusable classes are crucial to the software revolution that has been spurred by object technology.
In Python, you’ll typically use a buildingblock approach to create your programs. To avoid reinventing the wheel, you’ll use existing high-quality pieces wherever possible. This software reuse is a key benefit of objec-toriented programming.
Messages and Method Calls
When you drive a car, pressing its gas pedal sends a message to the car to perform a task—that is, to go faster. Similarly, you send messages to an object. Each message is implemented as a method call that tells a method of the object to perform its task. For example, a program might call a bankaccount object’s deposit method to increase the account’s balance.
Attributes and Instance Variables
A car, besides having capabilities to accomplish tasks, also has attributes, such as its color, its number of doors, the amount of gas in its tank, its current speed and its record of total miles driven (i.e., its odometer reading). Like its capabilities, the car’s attributes are represented as part of its design in its engineering diagrams (which, for example, include an odometer and a fuel gauge). As you drive an actual car, these attributes are carried along with the car. Every car maintains its own attributes. For example, each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars.
An object, similarly, has attributes that it carries along as it’s used in a program. These attributes are specified as part of the object’s class. For example, a bank-account object has a balance attribute that represents the amount of money in the account. Each bank-account object knows the balance in the account it represents, but not the balances of the other accounts in the bank. Attributes are specified by the class’s instance variables. A class’s (and its object’s) attributes and methods are intimately related, so classes wrap together their attributes and methods.
Inheritance
A new class of objects can be created conveniently by inheritance—the new class (called the subclass) starts with the characteristics of an existing class (called the superclass), possibly customizing them and adding unique characteristics of its own. In our car analogy, an object of class “convertible” certainly is an object of the more general class “automobile,” but more specifically, the roof can be raised or lowered.
Object-Oriented Analysis and Design (OOAD)
Soon you’ll be writing programs in Python. How will you create the code for your programs? Perhaps, like many programmers, you’ll simply turn on your computer and start typing. This approach may work for small programs (like the ones we present in the early chapters of the article), but what if you were asked to create a software system to control thousands of automated teller machines for a major bank? Or suppose you were asked to work on a team of 1,000 software developers building the next generation of the U.S. air traffic control system? For projects so large and complex, you should not simply sit down and start writing programs.
To create the best solutions, you should follow a detailed analysis process for determining your project’s requirements (i.e., defining what the system is supposed to do), then develop a design that satisfies them (i.e., specifying how the system should do it). Ideally, you’d go through this process and carefully review the design (and have your design reviewed Why other software professionals) before writing any code. If this process involves analyzing and designing your system from an objectoriented point of view, it’s called an object-oriented analysisanddesign (OOAD) process. Languages like Python are object-oriented. Programming in such a language, called object-oriented programming (OOP), allows you to implement an objectoriented design as a working system.