Page 53 -
P. 53
meet python
Q: So…when iterating over a list, I Q: Seeing as Python’s lists shrink and Q: What if I need to embed a double
should always use for instead of while? grow as needed, they must not support quote in a string?
bounds-checking, right?
A: Yes, unless you have a really good A: You have two choices: either escape
reason to use (or need the extra control A: Well, lists are dynamic, in that they the double quote like this: \”, or surround
of) a while loop. The for loop takes care shrink and grow, but they are not magic, your string with single quotes.
of working from the start of your list and in that they cannot access a data item
continuing to the end. It’s next to impossible that does not exist. If you try to access a Q: Can I use any characters to name
to get stung by an off-by-one error when you nonexistent data item, Python responds with my identifiers?
use for. This is not the case with while. an IndexError, which means “out of
Q: So, lists aren’t really like arrays bounds.” A: No. Like most other programming
then, because they do so much more? Q: What’s with all the strange languages, Python has some rules that
must be adhered to when creating names.
references to Monty Python? Names can start with a letter character or
A: Well…they are in that you can access an underscore, then include any number
individual data items in your list with the A: Ah, you spotted that, eh? It turns of letter characters, numbers, and/or
standard square bracket notation, but—as out that the creator of Python, Guido van underscores in the rest of the name. Strange
you’ve seen—Python’s lists can do so much Rossum, was reading the scripts of the characters (such as %$£) are not allowed
more. At Head First Labs, we like to think of Monty Python TV shows while designing his and you’ll obviously want to use names that
lists as “arrays on steroids.” new programming language. When Guido have meaning within the context of your
Q: And they work this way only in chose “Python” as a bit of a joke (or so the code. Names like members, the_
needed a name for his new language, he
time , and people are much better
Python 3, right? legend goes). than m, t, and p, aren’t they?
Q: Q:
A: No. There are certain enhancements Do I need to know Monty Python in Yes, good naming practice is
to lists that were added in Python 3, but order to understand the examples? always important. But what about case
release 2 of Python has lists, too. All of what sensitivity?
you’ve learned about lists so far will work A: No, but as they say in the official
with lists in Releases 2 and 3 of Python. Python documentation: “it helps if you do.” A: Yes, Python is the “sensitive type,” in
Q: Why are we using Python 3? What’s But don’t worry: you’ll survive, even if you’ve that Python code is case sensitive. This
never heard of Monty Python.
wrong with Python 2, anyway? Lots of Q: means that msg and MSG are two different
names, so be careful. Python (and IDLE)
programmers seem to be using it. I notice that some of your strings will help with the problems that can occur as
are surrounded with double quotes and a result of this. For instance, you can use
A: Lots of programmers are using Python others with single quotes. What’s the an identifier in your code only if it has been
2, but the future of Python development lies difference? given a value; unassigned identifiers cause
with Release 3. Of course, moving the entire a runtime error. This means that if you type
Python community to Python 3 won’t happen A: There isn’t any. Python lets you use mgs when you meant msg, you’ll find out
overnight, so there’s an awful lot of projects either to create a string. The only rule is that pretty quickly when Python complains about
that will continue to run on Release 2 for the if you start a string with one of the quotes, your code having a NameError.
foreseeable future. Despite 2’s dominance then you have to end it with the same
at the moment, at Head First Labs we think quote; you can’t mix’n’match. As you may
the new bits in 3 are well worth the added have seen, IDLE uses single quotes when
investment in learning about them now. displaying strings within the shell.
Don’t worry: if you know 2, Python 3 is easy.
you are here 4 17