I've been teaching myself Python by working through Dive Into Python by Mark Pilgrim. I thoroughly recommend it, as do other Stack Overflow users.
However, the last update to Dive Into Python was five years ago. I look forward to reading the new Dive into Python 3 When I make the switch to 3.x, but for now, using django means I'll stick to 2.x.
I'm interested to know what new features of Python I'm missing out on, if I've used Dive Into Python as my primary resource for learning the language. A couple of examples that I've come across are
Is there anything else I'm missing out on?
edit: As Bastien points out in his answer, I could just read the What's New in Python pages, but sometimes it's fun to discover a useful tip on Stack Overflow rather than struggle through the complete, comprehensive answer in the official documentation.
Check out What's New in Python. It has all the versions in the 2.x series. Per Alex's comments, you'll want to look at all Python 2.x for x > 2.
Highlights for day-to-day coding:
Enumeration: Instead of doing:
for i in xrange(len(sequence)):
val = sequence[i]
pass
You can now more succinctly write:
for i, val in enumerate(iterable):
pass
This is important because it works for non-getitemable iterables (you would otherwise have to use an incrementing index counter alongside value iteration).
Logging: a sane alternative to print-based debugging, standardized in a Log4j-style library module.
Booleans: True and False, added for clarity: return True
clearer intention than return 1
.
Generators: An expressive form of lazy evaluation
evens = (i for i in xrange(limit) if i % 2 == 0)
Extended slices: Builtins support strides in slices.
assert [1, 2, 3, 4][::2] == [1, 3]
Sets: For O(1) lookup semantics, you no longer have to do:
pseudo_set = {'foo': None, 'bar': None}
assert 'foo' in pseudo_set
You can now do:
set_ = set(['foo', 'bar'])
assert 'foo' in set_
Reverse iteration: reversed(sequence)
is more readable than sequence[::-1]
.
Subprocess: Unifies all the ways you might want to invoke a subprocess -- capturing outputs, feeding input, blocking or non-blocking.
Conditional expressions: There's an issue with the idiom:
a and b or c
Namely, when b is falsy. b if a else c
resolves that issue.
Context management: Resource acquisition/release simplified via the with
statement.
with open(filename) as file:
print file.read()
# File is closed outside the `with` block.
Better string formatting: Too much to describe -- see Python documentation under str.format()
.
Mark(author of the book) had some comments on this. I've shamelessly copied the related paragraph here:
"""If you choose Python 2, I can only recommend "Dive Into Python" chapters 2-7, 13-15, and 17. The rest of the book is horribly out of date."""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With