Text of docs.python.org › pa › 3 › whatsnew › 3.1.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

What's New In Python 3.1 — Python 3.15.2 documentation

Navigation index modules | next | previous | Python » 3.15.2 documentation » ਪਾਈਥਨ ਵਿੱਚ ਨਵਾਂ ਕੀ ਹੈ » What's New In Python 3.1 | Theme | What's New In Python 3.1 ¶ Author : Raymond Hettinger This article explains the new features in Python 3.1, compared to 3.0. Python 3.1 was released on June 27, 2009. PEP 372: Ordered Dictionaries ¶ Regular Python dictionaries iterate over key/value pairs in arbitrary order.

Over the years, a number of authors have written alternative implementations that remember the order that the keys were originally inserted. Based on the experiences from those implementations, a new collections.OrderedDict class has been introduced. The OrderedDict API is substantially the same as regular dictionaries but will iterate over keys and values in a guaranteed order depending on when a key was first inserted.

If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end. The standard library now supports use of ordered dictionaries in several modules. The configparser module uses them by default. This lets configuration files be read, modified, and then written back in their original order. The _asdict() method for collections.namedtuple() now returns an ordered dictionary with the values appearing in the same order as the underlying tuple indices.

The json module is being built-out with an object_pairs_hook to allow OrderedDicts to be built by the decoder. Support was also added for third-party tools like PyYAML . See also PEP 372 - Ordered Dictionaries PEP written by Armin Ronacher and Raymond Hettinger. Implementation written by Raymond Hettinger. Since an ordered dictionary remembers its insertion order, it can be used in conjunction with sorting to make a sorted dictionary: >>> # regular unsorted dictionary >>> d = { 'banana' : 3 , 'apple' : 4 , 'pear' : 1 , 'orange' : 2 } >>> # dictionary sorted by key >>> OrderedDict ( sorted ( d . items (), key = lambda t : t [ 0 ])) OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) >>> # dictionary sorted by value >>> OrderedDict ( sorted ( d . items (), key = lambda t : t [ 1 ])) OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]) >>> # dictionary sorted by length of the key string >>> OrderedDict ( sorted ( d . items (), key = lambda t : len ( t [ 0 ]))) OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)]) The new sorted dictionaries maintain their sort order when entries are deleted.

But when new keys are added, the keys are appended to the end and the sort is not maintained. PEP 378: Format Specifier for Thousands Separator ¶ The built-in format() function and the str.format() method use a mini-language that now includes a simple, non-locale aware way to format a number with a thousands separator. That provides a way to humanize a program's output, improving its professional appearance and readability: >>> format ( 1234567 , ',d' ) '1,234,567' >>> format ( 1234567.89 , ',.2f' ) '1,234,567.89' >>> format ( 12345.6 + 8901234.12 j , ',f' ) '12,345.600000+8,901,234.120000j' >>> format ( Decimal ( '1234567.89' ), ',f' ) '1,234,567.89' The supported types are int , float , complex and decimal.Decimal .

Discussions are underway about how to specify alternative separators like dots, spaces, apostrophes, or underscores. Locale-aware applications should use the existing n format specifier which already has some support for thousands separators. See also PEP 378 - Format Specifier for Thousands Separator PEP written by Raymond Hettinger and implemented by Eric Smith and Mark Dickinson. Other Language Changes ¶ Some smaller changes made to the core Python language are: Directories and zip archives containing a __main__.py file can now be executed directly by passing their name to the interpreter.

The directory/zipfile is automatically inserted as the first entry in sys.path. (Suggestion and initial patch by Andy Chu; revised patch by Phillip J. Eby and Nick Coghlan; bpo-1739468 .) The int() type gained a bit_length method that returns the number of bits necessary to represent its argument in binary: >>> n = 37 >>> bin ( 37 ) '0b100101' >>> n . bit_length () 6 >>> n = 2 ** 123 - 1 >>> n . bit_length () 123 >>> ( n + 1 ) . bit_length () 124 (Contributed by Fredrik Johansson, Victor Stinner, Raymond Hettinger, and Mark Dickinson; bpo-3439 .) The fields in format() strings can now be automatically numbered: >>> 'Sir {} of {} ' . format ( 'Gallahad' , 'Camelot' ) 'Sir Gallahad of Camelot' Formerly, the string would have required numbered fields such as: 'Sir {0} of {1}' . (Contributed by Eric Smith; bpo-5237 .) The string.maketrans() function is deprecated and is replaced by new static methods, bytes.maketrans() and bytearray.maketrans() .

This change solves the confusion around which types were supported by the string module. Now, str , bytes , and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type. (Contributed by Georg Brandl; bpo-5675 .) The syntax of the with statement now allows multiple context managers in a single statement: >>> with open ( 'mylog.txt' ) as infile , open ( 'a.out' , 'w' ) as outfile : ... for line in infile : ... if '<critical>' in line : ... outfile . write ( line ) With the new syntax, the contextlib.nested() function is no longer needed and is now deprecated. (Contributed by Georg Brandl and Mattias Brändström; appspot issue 53094 .) round(x, n) now returns an integer if x is an integer.

Previously it returned a float: >>> round ( 1123 , - 2 ) 1100 (Contributed by Mark Dickinson; bpo-4707 .) Python now uses David Gay's algorithm for finding the shortest floating-point representation that doesn't change its value. This should help mitigate some of the confusion surrounding binary floating-point numbers. The significance is easily seen with a number like 1.1 which does not have an exact equivalent in binary floating point.

Since there is no exact equivalent, an expression like float('1.1') evaluates to the nearest representable value which is 0x1.199999999999ap+0 in hex or 1.100000000000000088817841970012523233890533447265625 in decimal. That nearest value was and still is used in subsequent floating-point calculations. What is new is how the number gets displayed. Formerly, Python used a simple approach. The value of repr(1.1) was computed as format(1.1, '.17g') which evaluated to '1.1000000000000001' .

The advantage of using 17 digits was that it relied on IEEE-754 guarantees to assure that eval(repr(1.1)) would round-trip exactly to its original value. The disadvantage is that many people found the output to be confusing (mistaking intrinsic limitations of binary floating-point representation as being a problem with Python itself). The new algorithm for repr(1.1) is smarter and returns '1.1' . Effectively, it searches all equivalent string representations (ones that get stored with the same underlying float value) and returns the shortest representation.

The new algorithm tends to emit cleaner representations when possible, but it does not change the underlying values. So, it is still the case that 1.1 + 2.2 != 3.3 even though the representations may suggest otherwise. The new algorithm depends on certain features in the underlying floating-point implementation. If the required features are not found, the old algorithm will continue to be used. Also, the text pickle protocols assure cross-platform portability by using the old algorithm. (Contributed by Eric Smith and Mark Dickinson; bpo-1580 ) New, Improved, and Deprecated Modules ¶ Added a collections.Counter class to support convenient counting of unique items in a sequence or iterable: >>> Counter ([ 'red' , 'blue' , 'red' , 'green' , 'blue' , 'blue' ]) Counter({'blue': 3, 'red': 2, 'green': 1}) (Contributed by Raymond Hettinger; bpo-1696199 .) Added a new m

The rest of this page is on the live site.