Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using the Read-Evaluate-Print Loop (REPL)


Once we have installed Python 3, we can do some minimal interaction with Python to assure ourselves that things are working. In the long run, we'll use a number of other tools to create Python programs. To start out, we'll interact directly on the command line.

Python's Read-Evaluate-Print Loop (REPL) is the foundation for Python programming. More sophisticated things—such as writing application scripts or web servers—are essentially the same as interaction with the REPL: the Python program reads statements from our application script file or web server script file and evaluates those statements.

This fundamental rule is one of the very appealing features of Python. We can write sophisticated scripts, or we can interact with the language in the REPL; the language is the same.

Confirming that things are working

To confirm that things are working, we'll start the Python interpreter from a command-line prompt. It might similar to like this:

MacBookPro-SLott:~ slott$ python3
Python 3.3.4 (v3.3.4:7ff62415e426, Feb  9 2014, 00:29:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

The details of getting to a command prompt vary from OS to OS. We've shown the Mac OS X Terminal tool in this example. We entered the python3 command to be sure we ran our new version of Python, not the built-in Python 2.

The introductory message lists four special-purpose objects that are incorporated into the interactive Python environment. There are two more, quit and exit, which are also available. These are only present in the REPL interactive environment; they cannot be used in programs.

We'll look at how we get help later in a separate section, Interacting with the help subsystem. The other objects, however, produce useful tidbits of information and are the ideal way to be sure things are working. Enter copyright, credits, or license at the >>> prompt to confirm that Python is working.

Doing simple arithmetic

The REPL loop prints the results of each statement, allowing us to work with Python interactively. To be clear on what this means, we should define what constitutes a statement in the language. We'll avoid the strict formality of the Python language definition and provide a quick, informal definition of the relevant statement type.

The Python language has 20 or so kinds of statements. An expression—by itself—is a statement. Unless the value of the expression is None, the REPL will show the value of the expression. We'll often use an expression statement to evaluate functions that perform input and output.

This simple expression statement allows us to do things such as the following at the Python >>> prompt:

>>> 355/113
3.1415929203539825

We can enter any arithmetic expression. Python evaluates the expression, and if the result isn't None, we'll see the result. We've shown the true division operator, /, in this example.

We'll look at the various data types and operators in Chapter 2, Simple Data Types. For the moment, we'll identify a few features of Python. We have numbers in a variety of flavors, including integers, floating point, and complex values. Most values will be properly coerced to add precision. Have a look at these examples:

>>> 2 * 3.14 * 8j
50.24j
>>> _ **2
(-2524.0576+0j)

The first expression computed a value that includes an integer, 2; a floating point value, 3.14; and a complex value, 8j. We used the * operator for multiplication. The result is complex, 50.24j.

The second expression uses the _ variable. This is a handy feature that's unique to the REPL. The result of each expression is implicitly assigned to this variable. We can use _ in an expression to refer to the result of the previous expression. This only works in the REPL; it's never a part of a script.

When we computed _ **2, we squared 50.24j. This is -2524.0576. Since the source value was a complex number, the result is also a complex value even though the imaginary component of that complex value is zero. This is typical of Python—the data types of the operand values generally dictate the data types of the result of the operator. When there are different kinds of numbers, values are coerced according to the rules we'll look at in Chapter 2, Simple Data Types.

There's one notable exception to the rule that the types of the operands match the type of the result. The true division operator, /, produces floating point results from integer operands. The floor division operator, //, on the other hand, reflects the types of the operands. For example:

>>> 355 / 113
3.1415929203539825
>>> 355 // 113
3

We have these two division operators so that we can unambiguously specify what kind of division we'd like to perform. It saves us from having to write extra code to explicitly coerce results.

Assigning results to variables

The simple assignment statement produces no visible output:

>>> v = 23

This will create the variable v and assign the value of 23 to it. We can check this by using a very small expression statement. The expression is just the variable name:

>>> v
23

When we evaluate a very simple expression, such as v, we see the value of the variable.

Python's REPL has far-reaching consequences. Perhaps the most important consequence is that almost all examples of Python programming are provided as if we're entering the code at the >>> prompt. The documentation for very complex and sophisticated packages will be written as though we're going to use that package interactively. In most cases, we'll be writing application programs; we won't really do very much at the >>> prompt. But the idea of cutting through the complexity to arrive at something that can be done interactively is pervasive throughout the Python community.

Using import to add features

One significant part of Python is the presence of a vast library of additional features. Using an external library means that the core language can be kept quite simple. We can import any additional features we need, avoiding the clutter and complication of unused features.

The import statement is used to incorporate additional functions, classes, and objects into a program or the interactive environment. There are a number of variations of this statement. For example, we might want to use some of the more sophisticated math functions. We can search the Python documentation and discover that these are defined in the math library. We can include and use them like this:

>>> import math
>>> math.pi
3.141592653589793
>>> math.sin( math.pi/6 )
0.49999999999999994

In this example, we imported the math library. We evaluated math.pi to see one of the constants defined in this library. We evaluated .The result was almost (but not exactly) 1/2.

This also shows us an important thing about floating point numbers—they're just an approximation. This has nothing to do with Python specifically—it's a general feature of digital computing. It's very important to emphasize this fact about floating point numbers.

Tip

Floating point numbers are only an approximation. They're not exact. They are not the abstract mathematical ideal of an irrational number with infinite precision.

We'll return to the topic of floating point numbers in Chapter 2, Simple Data Types. For now, we want to focus on external libraries.

One important library module that is part of Python is named this. To see the this module, enter import this at the >>> prompt, like so:

>>> import this

Another equally important module is antigravity.

>>> import antigravity

We'll leave the exploration of these modules as exercises for the reader. We don't want to spoil the fun! More handwaving explanation isn't as helpful as hands-on experience. See http://xkcd.com/413/ for more on this topic.

We'll summarize by noting that the name "Python" has much to do with Monty Python and nothing to do with serpents.