Опубликован: 12.07.2013 | Доступ: свободный | Студентов: 733 / 20 | Длительность: 37:41:00
Дополнительный материал 1:

Differences Between Python 2 and 3

< Лекция 20 || Дополнительный материал 1 || Дополнительный материал 2 >

Differences Between Python 2 and 3

Most of the programs in this book make use of the newer version 3 of Python. The Pygame games make use of Python 2 because the Pygame library is not yet compatible with Python 3. Python 3 corrects many of the faults with version 2 of the langugae, however, these changes can also make it impossible to run Python 3 programs with the Python 2 interpreter, and vice versa.

There are only a few changes between the two versions, and this appendix will go through the ones relevant to this book. Learning both Python 2 and 3 is fairly simple. All of the modules imported by the programs in this book (except for the pygame module) are part of the standard library and work with both Python 2 and Python 3.

In short, use Python 3 unless you need to use a library that is not yet compatible with version 3. Learning both Python 2 and 3 is easy because there are only a few changes between them.

The print() Function and the print statement

In Python 3, print() is a function just like input() or len(). The function version requires parentheses just like any other function call (although you can add parentheses to the print statement optionally).

The print statement in Python 2 will always print a newline character at the end of the string. To make it print a space instead, put a comma at the end of the print statement:

>>> # Python 2
>>> print "Hello",

To print something besides a newline at the end of the print() function in Python 3, use the keyword argument end:

>>> # Python 3
>>> print("Hello", end="")

The input() and raw_input() Functions

In Python 2, the function to get input from the keyboard is raw_input(). In Python 3, the input() function does this. You can simply rename the function wherever it appears in your code.

>>> # Python 2
>>> name = raw_input()
>>> # Python 3 
>>> name = input()

The range() Function's Return Value

In Python 2, the range() function returns an actual list with the integers. In Python 3, the range() function returns a "range object". Both can be used exactly the same way in for loops:

>>> for i in range(10):   # Works in Python 2 and
3
...  
print(i)

However, if you want to create an actual list of integers in Python 3, you must convert the "range object" into a list with the list() function:

>>> # Python 2
>>> listOfInts = range(10)
>>> # Python 3
>>> listOfInts = list(range(10))

Division with the / Operator

In Python 2, doing division with the / operator results in a floating point number (that is, a number with a decimal point) only if one of the numbers is a float itself:

>>> # Python 2
>>> 25.0 / 8
3.125
>>> 25 / 8.0
3.125

However, in Python 2, if both of the numbers are integers, then the result of division is the rounded down integer. In Python 3, the result is a floating point number no matter what:

>>> # Python 2 
>>> 25 / 8 
3
>>> # Python 3 
>>> 25 / 8 
3.125

Formatting Strings with the format() Method and %s

In both Python 2 and 3, you can include %s inside a string and follow it with a list of values for each %s such as:

>>> # Python 2 and 3
>>> 'My name is %s and I am from %s.' % ('Al',
'Houston')
'My name is Al and I am from Houston.'

However, Python 3 adds a new string method called format(). This string lets you provide a list of arguments as parameters to format(). Instead of %s, you use {0} and {1} and {2} and so on:

>>> # Python 3
>>> 'My name is {0} and I am from {1}.'.format
('Al', 'Houston')
'My name is Al and I am from Houston.'

The numbers inside the curly braces are the index of the parameters to format(). So switching them around in the string will switch around where the parameters to format() are placed:

>>> # Python 3
>>> 'My name is {1} and I am from {0}.'.format
('Al', 'Houston')
'My name is Houston and I am from Al.'

One nice feature is that you can use the same parameter to format() multiple times in the string without making extra parameters to format():

>>> # Python 3
>>> 'My name is {0}, {0}, {0}, {0} and I am from
{1}.'.format('Jimmy Four Times', 'Houston')
'My name is Jimmy Four Times, Jimmy Four Times,
Jimmy Four Times, Jimmy Four Times and I am from
Houston.'

Along with numeric indexes, you can also put text inside the curly braces. This text can match to keyword arguments passed to format():

>>> # Python 3
>>> 'My name is {0} and I like {thing} and I am
from {hometown}.'.format('Al', hometown='Houston',
thing='cats')
'My name is Al and I like cats and I am from
Houston.'
< Лекция 20 || Дополнительный материал 1 || Дополнительный материал 2 >
Сергей Хвощев
Сергей Хвощев
Россия, Ленинград, ВВМУ им Фрунзе, 1980