Keywords in Python and C

Ashok Be
Professional Pirates
5 min readSep 3, 2018

Comparing keywords that are common and unique to both languages provides for interesting insights.

As reported elsewhere (http://j.mp/reservedWords), here are some popular languages and the count of keywords in each of them.

For quite some time, with only 31 keywords, Python (Version 2) had held the distinction of being the “language with the least number of keywords”. Of course, we need to ignore Smalltalk (6) and the not-yet-mainstream Go (25) and Elm (25).

But with the transition to Python 3, that distinction has been ceded to the C language (total keywords is 32). Therefore, I was quite curious as to what were the additional keywords that were added to the language. Here’s what I came up with:

  • In the transition from Python_2 to Python_3, actually there have been six changes to the keyword list; four were added and two were removed, resulting in a net addition of 2.
Keywords added to Python_3: {'None', 'nonlocal', 'True', 'False'}
Keywords removed from: {'print', 'exec'}
Total changes: 6

Note: Both print and exec have been removed keywords but retained as built-in functions. For an in-depth and insightful answer on behaviour of exec in Python 3 vis-à-vis Python 2, read this.

If you really think about it, it is not an apples-to-apples comparison when you comparing the count of keywords in a strictly typed language (C, Java, etc) versus a dynamic language (Python). Also, C does not have syntax that supports object-oriented programming or exception handling, whereas Python does.

Nevertheless, for the benefit of a developer/student who is familiar with core Python and is keen on familiarizing with the core part of the C language, the journey can start with an critical inspection of the keywords in both the languages. In comparison to “type-free” C, Python (sans keywords related to objected orientation and exception handling) has 15 keywords and C has 21 keywords.

-----------------------
Comparing keywords in Python and type-free C
-----------------------
Python: ['break', 'class', 'continue', 'def', 'del',
'else', 'for', 'global', 'if', 'in', 'is',
'nonlocal', 'pass', 'return', 'while'] Total - 15
`typefree` C Language: ['auto', 'break', 'const',
'continue', 'do', 'else', 'enum', 'extern', 'for',
'goto', 'if', 'register', 'return', 'sizeof',
'static', 'struct', 'typedef', 'union', 'void',
'volatile', 'while'] Total - 21

If you remove the keywords that are common to both the above lists, here’s what remains:

------ DIFFERENCE -------
extra in python: ['class', 'def', 'del',
'global', 'in', 'is', 'nonlocal', 'pass'] 8
extra in clang: ['auto', 'const', 'do', 'enum',
'extern', 'goto', 'register', 'sizeof', 'static',
'struct', 'typedef', 'union', 'volatile'] 14

My analysis below will attempt to compare and contrast the two languages by referring to the above 22 keywords (8 that exists only in Python verus 14 that exists only in C).

  • The do-while loop construct exists in C, but not in Python
  • enum exist in C to give variable-like labels to numeric values, but this is not supported in Python
  • C also has the switch-case-default keywords allowing for multi-conditional constructs which can be quite elegant when used with enums. The equivalent of this can be codified with a series non-elegant if-elif statements in Python
  • goto allows you to jump to an arbitrary location in the Ccode, and allowing for obscure source code to be written. And for that reason, it is recommended that this keyword be used sparingly; totally unsupported in Python
  • A function or class definition in Python requires def which is not required in C
  • pass - a Python statement which has no effect; the equivalent of this is best achieved in C by using the ; delimiter
  • Scope options are accomplished by using global and nonlocal in Python, whereas in C the keywords auto, static and extern are used.
  • C continues to be one of the few languages that allows you to get closer to the metal, and that is why it has keywords like register and volatile .
  • sizeof- is it a macro or a keyword? that’s a source for intense debate. The keyword is normally used in calculation of dynamic memory allocation. In contrast, explicit memory management is non-existent in Python.
  • Python supports object-oriented programming with the class keyword for which the closest equivalents would be struct and typedef in C. union, the memory-efficient construct is rather unique to C, which is not found even in other strictly typed languages.
  • Everything is an object in Python, and therefore an explicit del exists.
  • C has the ability to define that variable of any data type to be const at compile time, whereas in Python, the type of the object pre-determines whether it is mutable or immutable.
  • Member introspection in a sequence or collection is possible in Python through inkeyword - there is no equivalent of that in C.
  • Type reflection (meaning you can ask an object what type it is) and object equivalence is supported in Python and is is used to accomplish this.

The above lists of keywords were generated using a Python (Version 3) program which is listed below.

Program Listing and Output

import keyword
python3 = keyword.kwlist
# this can be obtained from a Python2 interpreter
# by using the above two statements
#
python2 = [
'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'
]
# new keywords in Python 3
additional = set(python3) - set(python2)
print("Keywords added to Python3:", additional)
removed = set(python2) - set(python3)
print("Keywords removed from:", removed)
print("Total changes:", len(additional) + len(removed))print("-----------------------")
print("Comparing keywords in Python and C")
print("-----------------------")
'''
# get keywords for C from webpages
# write a scrap program by processing links in
https://www.programiz.com/c-programming/list-all-keywords-c-language

http://tigcc.ticalc.org/doc/keywords.html#switch
'''
# Also read http://www.toves.org/books/cpy/
clang = []
with open("programiz.txt", "r") as fp:
links = fp.readlines()
for link in links:
try:
link.index("list-all-keywords-c-language#")
hashsymbol = link.find("#")
keyword = link[hashsymbol+1:-1]
#print("found keyword", keyword)
list = keyword.split("_")
clang.extend(klist)
except:
pass
clang.extend(["size_t", "NULL"])clangRemove = [
'NULL',
'char', 'int', 'float', 'double', 'long', 'size_t', 'void',
'short', 'unsigned', 'signed',
'switch', 'case', 'default',
# 'auto', 'volatile', 'register',
# 'enum', 'typedef',
]
clang = [
keyword for keyword in clang
if keyword not in clangRemove
]
pythonRemove = [
'None', 'False', 'True',
'assert', 'with', 'yield', 'lambda',
'try', 'except', 'finally', 'raise',
'import', 'from', 'as',
'and', 'or', 'not',
'elif'
]
python3 = [
keyword for keyword in python3
if keyword not in pythonRemove
]
print("Python:", sorted(python3), len(python3))
print()
print("'typefree' C Language:", sorted(clang), len(clang))
print("\n------ DIFFERENCE -------")
pextra = set(python3) - set(clang)
cextra = set(clang) - set(python3)
print("extra in python:", sorted(pextra), len(pextra))
print("extra in clang:", sorted(cextra), len(cextra))
Keywords added to Python3: {'None', 'nonlocal',
'True', 'False'}
Keywords removed from: {'print', 'exec'}
Total changes: 6
-----------------------
Comparing keywords in Python and C
-----------------------
Python: ['break', 'class', 'continue', 'def', 'del',
'else', 'for', 'global', 'if', 'in', 'is',
'nonlocal', 'pass', 'return', 'while'] 15
'typefree' C Language: ['auto', 'break', 'const',
'continue', 'do', 'else', 'enum', 'extern', 'for',
'goto', 'if', 'register', 'return', 'sizeof',
'static', 'struct', 'typedef', 'union', 'void',
'volatile', 'while'] 21
------ DIFFERENCE -------
extra in python: ['class', 'def', 'del', 'global',
'in', 'is', 'nonlocal', 'pass'] 8
extra in clang: ['auto', 'const', 'do', 'enum',
'extern', 'goto', 'register', 'sizeof', 'static',
'struct', 'typedef', 'union', 'volatile'] 14

Originally published at medium.com on September 3, 2018.

--

--

Ashok Be
Professional Pirates

Entrepreneur, struggle-to-code evangelist, paradox collector, green tea and black coffee enthusiast. And yes, squash!