Session 15 – Python: Keywords and Identifiers

15.1 Keywords in Python

Keywords is a term to describe stock Python one word commands. Keywords are always interpreted as a command and thus cannot be used in any other context. Keywords are case-sensitive.

Lets take a look at all the Python keywords with the code below:

#import keyword module, also known as defining what keyword is to Python
import keyword

#from keyword, list all keywords (kw)
keyword.kwlist

For more information regarding keyword see documentation here.

The first command uses the word ‘import’ which is similar to R’s ‘library’ function. ‘import’ will import the module entirely from Python for immediate use. It must be restated every time you start a new Python shell (terminal). It is common practice to include all ‘import’ commands first in your scripts, we will revisit this later.

You are not restricted to ‘import’ everything from a module. If you would like to use a specific part of the ‘keyword’ module for example, you can simply state ‘from keyword import [whatever part you want]’. However, in this case keyword is such a small module that this option is not available to us as it does not contain smaller parts to import.

Let us practice with a few of these keywords:

#Set variable as an interger
x = 2

#IF
if x > 1:
    print("x is greater than 1")
#notice no output for the script below:
if x > 3:
    print("x is greater than 3")

#ELSE
if x > 3:
    print("x is greater than 3")
else:
    print("x is not greater than 3")

#ELSE
if x > 1:
    print("x is greater than 1")
else:
    print("x is not greater than 1")

#OR
if x > 3 or x > 1:
    print("x is greater than 3 -OR- x is greater than 1")
else:
    print("x is not greater than 3 -AND- x is not greater than 1")

#AND
if x > 3 and x > 1:
    print("x is greater than 3 -AND- x is greater than 1")
else:
    print("x is not greater than 3 -OR- x is not greater than 1")

#ELIF
if x > 3:
    print("x is greater than 3")
elif x == 2:
    print("x is not greater than 3, but x is equal to 2")
else:
    print("x is not greater than 3 AND x is not equal to 2")

#TRY #EXCEPT
try:
    y > x
except NameError: y = 4

#NONE
try:
    y > x
except NameError: y = None

print(y)

#IS
if y is None:
    print("y is none")
else:
    print("y was not none")

#NOT
if y is not None:
    print("y is defined")
else:
    print("y was not defined")

#IMPORT #AS
import keyword as squidward
squidward.iskeyword("else")
squidward.iskeyword("cans")

There are 33+ keywords that are listed with our keyboard list command. It is important to learn these keywords. The best way to learn is by practicing these keywords in the python shell. You should be able to write a few lines of code for each keyword with thorough practice.

Keywords are key to a successful understanding of python.

15.2 Identifiers in Python

Unlike Keywords, which are chosen by the python programmers, identifiers are names chosen by the user to define a variable, file, destination, module, function, or any other object.

There are several rules to creating an idenitifier:

  1. What is allowed: Uppercase letters, lowercase letters, numbers, underscores (i.e. My_File_3)

  2. What is not allowed: Usage of identifiers that are identical to Keywords (i.e. dont use ‘for’ as an identifier), usage of a number to start an identifier (i.e. dont use ‘9_my_file’).

  3. What is also not allowed: idenitifers with special characters will not work (i.e. %), and identifiers should be kept short. If they are too long errors can occur (definately do not create identifiers longer than 79 characters).

  4. Some advice: Make identifiers short but descriptive, we are looking for a concise meaning behind the term(s) chosen to help aid in code readiblity, which will in turn assist in code debugging.

Let us make some identifiers and test their validity:

#for python3 we can use the .isidentifier() command to check our identifiers to see if they are valid (True) or not (False).
'My_file_2'.isidentifier
'2_My_file'.isidentifier
'My_file_@'.isidentifier
'for'.isidentifier

#we can create variables
score_1 = 95
print(score_1)
score_2 = 45
print(score_2)

#we can add variables
score_1 + score_2

#we can add variables and store the information
sum_of_scores = score_1 + score_2

#beware, if intended or not redeclaring a variable will replace the contents
score_1 = 95
print(score_1)
score_1 = 55
print(score_1)