Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 23 It’s always a good idea to make your code readable on its own as well, even without the comments. Luckily, Python is an excellent language for writing readable programs. Strings Now what was all that raw_input and "Hello, " + name + "!" stuff about? Let’s tackle the "Hello" part first and leave raw_input for later. The first program in this chapter was simply print "Hello, world!" It is customary to begin with a program like this in programming tutorials—the problem is that I haven’t really explained how it works yet. Well, you know the basics of the print statement (I’ll have more to say about that later), but what is "Hello, world!"? It’s called a string (as in “a string of characters”). Strings are found in almost every useful, real-world Python program and have many uses, the main one being to represent a bit of text, such as the exclamation “Hello, world!” Single-Quoted Strings and Escaping Quotes Strings are values, just like numbers are: >>> "Hello, world!" 'Hello, world!' There is one thing that may be a bit surprising about this example, though: When Python printed out our string, it used single quotes, whereas we used double quotes. What’s the difference? Actually, there is no difference: >>> 'Hello, world!' 'Hello, world!' Here, too, we use single quotes, and the result is the same. So why allow both? Because in some cases it may be useful: >>> "Let's go!" "Let's go!" >>> '"Hello, world!" she said' '"Hello, world!" she said' In the preceding code, the first string contains a single quote (or apostrophe, as we should perhaps call it in this context), and therefore we can’t use single quotes to enclose the string. If we did, the interpreter would complain (and rightly so): >>> 'Let's go!' SyntaxError: invalid syntax Here, the string is 'Let', and Python doesn’t quite know what to do with the following s (or the rest of the line, for that matter). In the second string, we use double quotes as part of our sentence. Therefore, we have to use single quotes to enclose our string, for the same reasons as stated previously. Or, actually
24 CHAPTER 1 ■ INSTANT HACKING: THE BASICS we don’t have to. It’s just convenient. An alternative is to use the backslash character (\) to escape the quotes in the string, like this: >>> 'Let\'s go!' "Let's go!" Python understands that the middle single quote is a character in the string and not the end of the string. (Even so, Python chooses to use double quotes when printing out the string.) The same works with double quotes, as you might expect: >>> "\"Hello, world!\" she said" '"Hello, world!" she said' Escaping quotes like this can be useful, and sometimes necessary. For example, what would you do without the backslash if your string contained both a single quote and a double quote, as in the string 'Let\'s say "Hello, world!"'? ■Note Tired of backslashes? As you will see later in this chapter, you can avoid most of them by using long strings and raw strings (which can be combined). Concatenating Strings Just to keep whipping this slightly tortured example, let me show you another way of writing the same string: >>> "Let's say " '"Hello, world!"' 'Let\'s say "Hello, world!"' I’ve simply written two strings, one after the other, and Python automatically concatenates them (makes them into one string). This mechanism isn’t used very often, but it can be useful at times. However, it only works when you actually write both strings at the same time, directly following one another: >>> x = "Hello, " >>> y = "world!" >>> x y SyntaxError: invalid syntax In other words, this is just a special way of writing strings, not a general method of concatenating them. How, then, do you concatenate strings? Just like you add numbers: >>> "Hello, " + "world!" 'Hello, world!' >>> x = "Hello, " >>> y = "world!" >>> x + y 'Hello, world!'
- Page 4: For Ranveig
- Page 7 and 8: ■CHAPTER 23 Project 4: In the New
- 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 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: 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
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 23<br />
It’s always a good idea <strong>to</strong> make your code readable on its own as well, even without the<br />
comments. Luckily, <strong>Python</strong> is an excellent language for writing readable programs.<br />
Strings<br />
Now what was all that raw_input and "Hello, " + name + "!" stuff about? Let’s tackle the<br />
"Hello" part first and leave raw_input for later.<br />
The first program in this chapter was simply<br />
print "Hello, world!"<br />
It is cus<strong>to</strong>mary <strong>to</strong> begin with a program like this in programming tu<strong>to</strong>rials—the problem is that I<br />
haven’t really explained how it works yet. Well, you know the basics of the print statement (I’ll have<br />
more <strong>to</strong> say about that later), but what is "Hello, world!"? It’s called a string (as in “a string of characters”).<br />
Strings are found in almost every useful, real-world <strong>Python</strong> program and have many uses,<br />
the main one being <strong>to</strong> represent a bit of text, such as the exclamation “Hello, world!”<br />
Single-Quoted Strings and Escaping Quotes<br />
Strings are values, just like numbers are:<br />
>>> "Hello, world!"<br />
'Hello, world!'<br />
There is one thing that may be a bit surprising about this example, though: When <strong>Python</strong><br />
printed out our string, it used single quotes, whereas we used double quotes. What’s the difference?<br />
Actually, there is no difference:<br />
>>> 'Hello, world!'<br />
'Hello, world!'<br />
Here, <strong>to</strong>o, we use single quotes, and the result is the same. So why allow both? Because in some<br />
cases it may be useful:<br />
>>> "Let's go!"<br />
"Let's go!"<br />
>>> '"Hello, world!" she said'<br />
'"Hello, world!" she said'<br />
In the preceding code, the first string contains a single quote (or apostrophe, as we should<br />
perhaps call it in this context), and therefore we can’t use single quotes <strong>to</strong> enclose the string.<br />
If we did, the interpreter would complain (and rightly so):<br />
>>> 'Let's go!'<br />
SyntaxError: invalid syntax<br />
Here, the string is 'Let', and <strong>Python</strong> doesn’t quite know what <strong>to</strong> do with the following s (or the<br />
rest of the line, for that matter).<br />
In the second string, we use double quotes as part of our sentence. Therefore, we have <strong>to</strong><br />
use single quotes <strong>to</strong> enclose our string, for the same reasons as stated previously. Or, actually