Keywords in Python and C
How to start comparing two programming languages? Begin at the core by analyzing the keywords that are exclusive to both languages.
Comparing Python and C through Keywords
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 back to the good old C language (total keywords is 32). In any case, 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, there have been six changes to the keyword list — four were added and two were removed, resulting in a net addition of two.
Net Change is +6–4 = +2
Keywords added to Python_3: {'None', 'nonlocal', 'True', 'False'}
Keywords removed from: {'print', 'exec'}
Total changes: 6Note: Both print and exec have been removed as 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.
Core Python versus Core C
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. While “type-free” C has 21 keywords, Python (sans keywords related to objected orientation and exception handling) has 15 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'] 8extra 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).
Part 0
- The
do-whileloop construct exists in C, but not in Python enumexist in C to give variable-like labels to numeric values, but this is not supported in PythonCalso has theswitch-case-defaultkeywords allowing for multi-conditional constructs which can be quite elegant when used withenums. The equivalent of this can be codified with a series non-elegantif-elifstatements in Pythongotoallows you to jump to an arbitrary location in theCcode, 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
defwhich 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
Part 1
- Scope options are accomplished by using
globalandnonlocalin Python, whereas in C the keywordsauto,staticandexternare used. - C continues to be one of the few languages that allows you to get close to the metal, and that is why it has keywords like
registerandvolatile. 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.
Part 2
- Python supports object-oriented programming with the
classkeyword for which the closest equivalents would bestructandtypedefin 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
delexists. - C has the ability to define that variable of any data type to be
constat compile time, whereas in Python, the type of the object pre-determines whether it is mutable or immutable.
Part 3
- 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
isis used to accomplish this.
Program listing
The above lists of keywords were generated using a Python (Version 3) program which is listed below.
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
'''
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:
passclang.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))
Program Output
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
Useful links
- Short tutorial http://techbeamers.com/python-keywords-identifiers-variables/
- Tutorial for each of the Python keywords: http://j.mp/python3Keywords
- C for Python Programmers http://www.toves.org/books/cpy/

