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

Python Programming 101

Revision of Keywords in Python 2 & 3 from Sun, 2011-04-24 00:13

The revisions let you track differences between multiple versions of a post.

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) # Lets see how  many keywords are available in Python 3
33

===Python 2.6.6===
>>> import keyword # Again, importing the Keyword Module.
>>> print keyword.kwlist
['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.
"""
>>> 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'}