Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 29 Unicode Strings The final type of string constant is the Unicode string (or Unicode object—they don’t really belong to the same type as strings). If you don’t know what Unicode is, you probably don’t need to know about this. (If you want to find out more about it, you can go to the Unicode Web site, www.unicode.org.) Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode. This allows for a more varied set of characters, including special characters from most languages in the world. I’ll restrict my treatment of Unicode strings to the following: >>> u'Hello, world!' u'Hello, world!' As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r. A Quick Summary This chapter covers quite a bit of material. Let’s take a look at what you’ve learned before moving on. Algorithms. An algorithm is a recipe telling you exactly how to perform a task. When you program a computer, you are essentially describing an algorithm in a language the computer can understand, such as Python. Such a machine-friendly description is called a program, and it mainly consists of expressions and statements. Expressions. An expression is a part of a computer program that represents a value. For example, 2+2 is an expression, representing the value 4. Simple expressions are built from literal values (such as 2 or "Hello") by using operators (such as + or %) and functions (such as pow). More complicated expressions can be created by combining simpler expressions (e.g., (2+2)*(3-1)). Expressions may also contain variables. Variables. A variable is a name that represents a value. New values may be assigned to variables through assignments such as x = 2. An assignment is a kind of statement. Statements. A statement is an instruction that tells the computer to do something. That may involve changing variables (through assignments), printing things to the screen (such as print "Hello, world!"), importing modules, or a host of other stuff. Functions. Functions in Python work just like functions in mathematics: They may take some arguments, and they return a result. (They may actually do lots of interesting stuff before returning, as you will find out when you learn to write your own functions in Chapter 6.) Modules. Modules are extensions that can be imported into Python to extend its capabilities. For example, several useful mathematical functions are available in the math module. Programs. You have looked at the practicalities of writing, saving, and running Python programs.
30 CHAPTER 1 ■ INSTANT HACKING: THE BASICS Strings. Strings are really simple—they are just pieces of text. And yet there is a lot to know about them. In this chapter, you’ve seen many ways to write them, and in Chapter 3 you learn many ways of using them. New Functions in This Chapter Function Description abs(number) Returns the absolute value of a number cmath.sqrt(number) Square root, also for negative numbers float(object) Converts a string or number to a floating-point number help() Offers interactive help input(prompt) Gets input from the user int(object) Converts a string or number to an integer long(object) Converts a string or number to a long integer math.ceil(number) Returns the ceiling of a number as a float math.floor(number) Returns the floor of a number as a float math.sqrt(number) Square root, not for negative numbers pow(x, y[, z]) x to the power of y (modulo z) raw_input(prompt) Gets input from the user, as a string repr(object) Returns a string-representation of a value round(number[, ndigits]) Rounds a number to a given precision str(object) Converts a value to a string What Now? Now that you know the basics of expressions, let’s move on to something a bit more advanced: data structures. Instead of dealing with simple values (such as numbers), you’ll see how to bunch them together in more complex structures, such as lists and dictionaries. In addition, you’ll take another close look at strings. In Chapter 5, you learn more about statements, and after that you’ll be ready to write some really nifty programs.
- Page 9 and 10: viii ■CONTENTS Strings . . . . .
- Page 11 and 12: x ■CONTENTS ■CHAPTER 5 Conditio
- Page 13 and 14: xii ■CONTENTS ■CHAPTER 8 Except
- Page 15 and 16: xiv ■CONTENTS ■CHAPTER 11 Files
- Page 17 and 18: xvi ■CONTENTS Dynamic Web Pages w
- Page 19 and 20: xviii ■CONTENTS ■CHAPTER 20 Pro
- Page 21 and 22: xx ■CONTENTS Second Implementatio
- Page 23 and 24: xxii ■CONTENTS Preparations . . .
- Page 26: About the Technical Reviewer ■JER
- Page 30 and 31: Introduction A C program is like a
- Page 32 and 33: CHAPTER 1 ■ ■ ■ Instant Hacki
- Page 34 and 35: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 36 and 37: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 38 and 39: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 40 and 41: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 42 and 43: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 44 and 45: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 46 and 47: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 48 and 49: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 50 and 51: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 72 and 73: CHAPTER 2 ■ LISTS AND TUPLES 41 T
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
- Page 86 and 87: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 88 and 89: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 98 and 99: CHAPTER 4 ■ ■ ■ Dictionaries:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 29<br />
Unicode Strings<br />
The final type of string constant is the Unicode string (or Unicode object—they don’t really<br />
belong <strong>to</strong> the same type as strings). If you don’t know what Unicode is, you probably don’t<br />
need <strong>to</strong> know about this. (If you want <strong>to</strong> find out more about it, you can go <strong>to</strong> the Unicode Web<br />
site, www.unicode.org.) Normal strings in <strong>Python</strong> are s<strong>to</strong>red internally as 8-bit ASCII, while<br />
Unicode strings are s<strong>to</strong>red as 16-bit Unicode. This allows for a more varied set of characters,<br />
including special characters from most languages in the world. I’ll restrict my treatment of<br />
Unicode strings <strong>to</strong> the following:<br />
>>> u'Hello, world!'<br />
u'Hello, world!'<br />
As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r.<br />
A Quick Summary<br />
This chapter covers quite a bit of material. Let’s take a look at what you’ve learned before<br />
moving on.<br />
Algorithms. An algorithm is a recipe telling you exactly how <strong>to</strong> perform a task. When you<br />
program a computer, you are essentially describing an algorithm in a language the computer<br />
can understand, such as <strong>Python</strong>. Such a machine-friendly description is called a program,<br />
and it mainly consists of expressions and statements.<br />
Expressions. An expression is a part of a computer program that represents a value. For<br />
example, 2+2 is an expression, representing the value 4. Simple expressions are built from<br />
literal values (such as 2 or "Hello") by using opera<strong>to</strong>rs (such as + or %) and functions (such<br />
as pow). More complicated expressions can be created by combining simpler expressions<br />
(e.g., (2+2)*(3-1)). Expressions may also contain variables.<br />
Variables. A variable is a name that represents a value. New values may be assigned <strong>to</strong><br />
variables through assignments such as x = 2. An assignment is a kind of statement.<br />
Statements. A statement is an instruction that tells the computer <strong>to</strong> do something. That<br />
may involve changing variables (through assignments), printing things <strong>to</strong> the screen (such<br />
as print "Hello, world!"), importing modules, or a host of other stuff.<br />
Functions. Functions in <strong>Python</strong> work just like functions in mathematics: They may take<br />
some arguments, and they return a result. (They may actually do lots of interesting stuff<br />
before returning, as you will find out when you learn <strong>to</strong> write your own functions in<br />
Chapter 6.)<br />
Modules. Modules are extensions that can be imported in<strong>to</strong> <strong>Python</strong> <strong>to</strong> extend its capabilities.<br />
For example, several useful mathematical functions are available in the math module.<br />
Programs. You have looked at the practicalities of writing, saving, and running <strong>Python</strong><br />
programs.