Online Book Reader

Home Category

Learning Python - Mark Lutz [95]

By Root 1317 0
we can apply to them. Along the way, we met the standard integer and floating-point types, as well as some more exotic and less commonly used types such as complex numbers, fractions, and sets. We also explored Python’s expression syntax, type conversions, bitwise operations, and various literal forms for coding numbers in scripts.

Later in this part of the book, I’ll fill in some details about the next object type, the string. In the next chapter, however, we’ll take some time to explore the mechanics of variable assignment in more detail than we have here. This turns out to be perhaps the most fundamental idea in Python, so make sure you check out the next chapter before moving on. First, though, it’s time to take the usual chapter quiz.

Test Your Knowledge: Quiz

What is the value of the expression 2 * (3 + 4) in Python?

What is the value of the expression 2 * 3 + 4 in Python?

What is the value of the expression 2 + 3 * 4 in Python?

What tools can you use to find a number’s square root, as well as its square?

What is the type of the result of the expression 1 + 2.0 + 3?

How can you truncate and round a floating-point number?

How can you convert an integer to a floating-point number?

How would you display an integer in octal, hexadecimal, or binary notation?

How might you convert an octal, hexadecimal, or binary string to a plain integer?

Test Your Knowledge: Answers

The value will be 14, the result of 2 * 7, because the parentheses force the addition to happen before the multiplication.

The value will be 10, the result of 6 + 4. Python’s operator precedence rules are applied in the absence of parentheses, and multiplication has higher precedence than (i.e., happens before) addition, per Table 5-2.

This expression yields 14, the result of 2 + 12, for the same precedence reasons as in the prior question.

Functions for obtaining the square root, as well as pi, tangents, and more, are available in the imported math module. To find a number’s square root, import math and call math.sqrt(N). To get a number’s square, use either the exponent expression X ** 2 or the built-in function pow(X, 2). Either of these last two can also compute the square root when given a power of 0.5 (e.g., X ** .5).

The result will be a floating-point number: the integers are converted up to floating point, the most complex type in the expression, and floating-point math is used to evaluate it.

The int(N) and math.trunc(N) functions truncate, and the round(N, digits) function rounds. We can also compute the floor with math.floor(N) and round for display with string formatting operations.

The float(I) function converts an integer to a floating point; mixing an integer with a floating point within an expression will result in a conversion as well. In some sense, Python 3.0 / division converts too—it always returns a floating-point result that includes the remainder, even if both operands are integers.

The oct(I) and hex(I) built-in functions return the octal and hexadecimal string forms for an integer. The bin(I) call also returns a number’s binary digits string in Python 2.6 and 3.0. The % string formatting expression and format string method also provide targets for some such conversions.

The int(S, base) function can be used to convert from octal and hexadecimal strings to normal integers (pass in 8, 16, or 2 for the base). The eval(S) function can be used for this purpose too, but it’s more expensive to run and can have security issues. Note that integers are always stored in binary in computer memory; these are just display string format conversions.

Chapter 6. The Dynamic Typing Interlude

In the prior chapter, we began exploring Python’s core object types in depth with a look at Python numbers. We’ll resume our object type tour in the next chapter, but before we move on, it’s important that you get a handle on what may be the most fundamental idea in Python programming and is certainly the basis of much of both the conciseness and flexibility of the Python language—dynamic typing, and the polymorphism

Return Main Page Previous Page Next Page

®Online Book Reader