Python Keywords, Operands and Identifiers (names) Tutorial

In this tutorial we will learn about special keywords in Python. We also learn about identifiers and their casing, as well as operands, lvalues and rvalues.

What is a keyword?

Keywords are reserved words that have a special meaning and purpose in Python. Keywords are used to define the syntax and structure of the Python programming language.

We can’t use keywords as names for our data containers or functionality, or any other type of identifier.

In Python, keywords are case sensitive. All keywords except True , False , and None are written as one word in lowercase letters.

The following table shows a list of keywords available in Python.

Falseawaitelseimportpass
Nonebreakexceptinraise
Trueclassfinallyisreturn
andcontinueforlambdatry
asdeffromnonlocalwhile
assertdelglobalnotwith
asyncelififoryield

You don’t need to memorize the keywords in the table above. As we progress through the tutorial series we will learn more about each of them.

What is an identifier?

An identifier is a custom name that we give to elements such as variables, lists, functions, classes, class members etc. It helps developers easily read and understand code.

Identifier rules

In Python there are certain rules we have to remember when naming our elements.

1. A reserved keyword (see table above) may not be used as an identifier.

Example:
# invalid
while
else
del

2. An identifier must start with an uppercase or lowercase letter (A - Z or a - z) or an underscore ( _ ).

Example:
# valid
Name = "Monty"
name = "Monty"
_name = "Monty"

3. An identifier may not contain special characters like $, @, & etc.

Example:
# invalid
^_^ = "Smile"
n@me = "Monty"
name&surname = "Monty Python"

4. An identifier may not start with a number, but may contain numbers.

Example:
# invalid
21_Jump_Street = "Invalid"

# valid
mambo_number_5 = "Valid"

5. Identifiers are case sensitive.

Example:
learning = "Python"
Learning = "Python"
LEARNING = "Python"

print("learning: " + learning)
print("Learning: " + Learning)
print("LEARNING: " + LEARNING)

Even though the same word is used in the example above, they are treated as different variables because of their casing.

Operands, LValues and RValues

There are two kinds of expressions in Python, Lvalues and Rvalues. Lvalues and Rvalues are also known as left operand and right operand respectively, or operands in general.

LValue

An lvalue can have a value assigned to it so it’s allowed to appear on either the left or right side of an assignment.

Example: Lvalue, the left operand
# lvalue (var1) may be on the left
var1 = 10

# or on the right of the = operator
var2 = var1

RValue

An rvalue is an expression that cannot be assigned a value so it may only appear on the right side of an assignment.

Example: Rvalue, the right operand
# rvalue (10) may only be on the right
var1 = 10

# and cannot be on the left
10 = 10

Conventional identifier casing

Where and when to use which types of casing in Python is outlined nicely in the PEP-8 document, but we include it here for completeness.

1. Use lowercase letters and underscores for functions and variables, but do not use leading underscores:

Example: Variables & Functions use snake_case
# yes
name = "Monty"
first_name = "Monty"
def my_function():

# no
_name = "Monty"
def MY_FUNCTION():

Leading underscores have a special conventional meaning inside classes.

2. Use uppercase letters, separated by underscores, for variables intended to have a constant value throughout the life of the application:

Example: Constants in ALL_CAPS
# yes
TITLE = "Python Tutorial"
PI = 3.14

# no
pi = 3.14
Pi = 3.14

3. Use PascalCase for classes. The first letter of the first word, and each consecutive word is capitalized. PascalCase does not use underscores between words:

Example: Classes use PascalCase
# yes
class ExampleClass():

# no
class example_class():

Some companies or projects may have their own conventions you will have to follow, but the type of casing you use in personal projects is up to your preference. The important thing to remember is that readability and consistency is key.

Summary: Points to remember

  • Keywords are reserved for other uses in Python and may not be used as identifiers
    • Keywords (except True , False , and None ) are written as one word in lower case.
  • Identifiers are the custom names we give our elements in Python.
    • Identifiers must start with capital or lowercase letters or an underscore.
    • Identifiers may not start with a number but may contain numbers.
    • Identifiers may not start with or contain special characters like $, @, &.
    • Identifiers are case sensitive.
  • Operands are values on the left (Lvalue) and right (Rvalue) of operators.
    • Lvalues may appear on the left and right.
    • Rvalues may only appear on the right.
  • Casing conventions for identifiers are optional but recommended.
    • Use lowercase letters and underscores to separate words when naming data containers and functions or methods.
    • Use uppercase letters and underscores to separate words when naming data containers whose value(s) stay constant through the application life.
    • Use PascalCase for classes where each word starts with an uppercase letter with no underscores as separation.