Python Tutorial - Keywords and Identifiers

Jinku Hu Sep 17, 2020
  1. Python Keywords
  2. Python Identifiers
Python Tutorial - Keywords and Identifiers

In this section, We will discuss Python keywords and identifiers.

Python Keywords

Keywords are the predefined words that have special meaning and that cannot be used to name any variable, function, class, etc. Keywords are also called reserved words that are actually reserved for special purposes.

Python has 33 case-sensitive keywords. Most of them except True, False and None are written in lowercase. The following is the list of Python keywords:

Keywords Description
class defines a class in Python OOP.
as used with with as when you want to execute two operations together as a pair
and is a logical and operator. and will generate True when both conditions are true else False will be returned.
assert ensures if a condition is True
break is used to break flow of control while working with looping construct on the basis of certain condition.
continue skips the current iteration of a looping construct on the basis of a condition.
def defines a function in Python
del deletes any reference of an object.
elif represents else if construct
else else is block of statements to be executed when a condition with if becomes False. (else is optional)
except handles exceptions (run time errors)
finally A finally clause is always executed before leaving the try statement, whether an exception has occurred or not
for represents for loop
from used with import to import any function, program from a module
global declares a global variable
if represents the if statement, the body of which is executed when condition is True
import imports a function, class, variable from a module
in 1. Membership check Operator 2. Traversing a sequence object in a for loop
is checks the equivalence of two quantities
lambda creates lambda function (anonymous function)
not is a unary operator (operates on one operand). It negates the value True to False and vice versa.
or is a logical or operator. or will generate True when any of the condition is true else False will be returned.
pass represents a null statement. pass is used as a placeholder in Python.
raise raises an exception
return returns value from a function
try represents a block in which there are some lines of a code that can result in an exception
while represents while loop
with ensures that a code is clarified (all exceptions handled using try catch)
yield return generator (generates one item in each iteration) from a function
nonlocal declares non-local variable when working with nested function. nonlocal variable in nested function is known to outer function.
None represents a null value
True A Boolean value returned when a Boolean expression is evaluated.
False A Boolean value returned when a Boolean expression is evaluated.

Python Identifiers

Identifiers are names of functions, variables, class, etc. Reserved words are not allowed to be the names of identifiers.

Rules for Naming Identifiers

  1. An identifier can have letters (both uppercase or lowercase), digits (0 to 9) or underscore (_), for example, last_name1, my_first_name and CapName are legal identifiers.
  2. You cannot use digits to start an identifier, for example, 1last_name is an illegal identifier.
  3. Keywords are not allowed to be the names of identifiers.
  4. Special symbols such as $, !, @, #, %, etc. are not allowed in an identifier.
  5. Python identifier has no length limitation.
  6. Blank spaces are not allowed.

Points to Ponder

  1. As Python is case sensitive meaning that uppercase and lowercase variables are different. So last_name and LAST_name are two different variables.
  2. Use meaningful names for variables to increase the readability of your codes.
  3. If your variable contains multiple words, they should be separated by underscore.
  4. Or you can use camel case to separate multiple words, that is, the first letter of each word to be upper-case, for example, CapitalizedWords.
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook