This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Python Programming 101

Keywords in Python 2 & 3

Brylie Oxley's picture
Sun, 2011-04-24 00:10

I received the following question:
> Two keywords are not in Python 3.1, would there be a problem for a python  programmer if one using Python 2.7 and another using Python 3.1?

To explore this question, we can look at some neat functionality in Python, specifically, set comparison.

Lets check out the Keywords for Python 3 and 2.x and how they differ:

===Python 3.1.2===

>>> import keyword  # The Keyword module will tell us the available keywords.
>>> print( keyword.kwlist )
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

>>> len(keyword.kwlist) # How  many keywords are available in Python 3?
33

===Python 2.6.6===
>>> import keyword # Again, importing the Keyword Module.
>>> print keyword.kwlist # no parenthesis for Python 2
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>> len(keyword.kwlist) # Checking the number of keywords in the keyword list.
31

"""
This shows, as stated, that there is a difference of two keywords between Python 2 and 3.

Lets take a further look at the difference of and intersection between the two sets of keywords (p2_keywords & p3_keywords)
"""

===Python 3.1.2===
"""
In order to easily compare the keywords for each Python version, we will use Python's built in set theory tools.

I will paste the list of Python 2 keywords into a variable called p2_keywords and assign the list of Python 3 keywords to a variable called p3_keywords. Both lists are converted to sets using the method set( keyword_list ).
"""
>>> import keyword
>>> p2_keywords = set( ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'] )
>>> p3_keywords = set(keyword.kwlist)

# Lets check what type each of our variables are

>>> type(p2_keywords)
<class 'set'>
>>> type(p3_keywords)
<class 'set'>

"""
The set class has two methods that we can use to check the overlap (intersection) as well as the difference between these two sets.
"""
>>>intersection = p3_keywords.intersection(p2_keywords)
>>> print(intersection)
{'and', 'elif', 'is', 'global', 'as', 'in', 'if', 'from', 'raise', 'for', 'except', 'finally', 'import', 'pass', 'return', 'else', 'break', 'not', 'with', 'class', 'assert', 'yield', 'try', 'while', 'continue', 'del', 'or', 'def', 'lambda'}

>>> len(intersection) # How many keywords do they have in common?
29

>>> p3_keywords.difference(p2_keywords) # Keywords that are new in Python 3
{'False', 'None', 'True', 'nonlocal'}

>>> p2_keywords.difference(p3_keywords) # Keywords removed since Python 3
{'print', 'exec'}

=== Summary ===
The set() function creates a set out of a list. Sets have special methods for comparison including difference() and intersection().

Python 3 has gained four new keywords:

  • False
  • None
  • True
  • nonlocal

Python 3 converted two Python 2 keywords into Python 3 functions:

  • print()
  • exec()

Programmers need to be careful when using these keywords in order for their Python 2 programs to be readily compatible with Python 3. Python 2.7 is designed as a transitional language to help remind programmers to make changes in their programs to match the new python 3 features. There is also a migration script called "2to3" that helps developers convert legacy Python scripts to be compatible with new Python 3 syntax.