Python Keywords and Identifiers

Keywords are reserved word in Python programming language that cannot be used for naming variables, constants or function names in while writing Python programs. Identifiers are the names given to the variables, constants, functions, classes etc. In this tutorial, we will learn more about what are the keywords and some rules for naming identifiers.

What are Keywords for Python?

Keywords in python represent reserved words that cannot be used as identifiers. Following are the list of keywords in Python.

andexecnot
assertfinallyor
breakforpass
classfromprint
continueglobalraise
defifreturn
delimporttry
elifinwhile
elseiswith
exceptlambdayield

Let us go through some keywords and their use:-

MethodDescription
andA logical operator that produces true if all values are right
asTo create an alias in Python
assertFor debugging purpose
breakTo break out of a loop
classUsed to create a class in Python
continueTo continue to the next iteration of a loop
defThe definition of a function starts with def keyword
delTo delete an object
elifUsed in conditional statements, same as else if
elseConditional statement used with if statement
exceptUsed with exceptions, what to do when an exception occurs
FalseBoolean value, the result of comparison operations
finallyUsed with exceptions, a block of code that will be executed no matter if there is an exception or not
forTo create a for loop
fromTo import specific parts of a module
globalTo declare a global variable
ifTo make a conditional statement
importTo import a module
inTo check if a value is present in a list, tuple, etc.
isTo test if two variables are equal
lambdaTo create an anonymous function
NoneRepresents a null value
nonlocalTo declare a non-local variable
notA logical operator to check if it is not
orA logical operator produces true if all any one of the value is right
passA null statement, a statement that will do nothing
raiseTo raise an exception
returnTo exit a function and return a value
TrueBoolean value, the result of comparison operations
tryTo make a try…except statement
whileTo create a while loop
withUsed to simplify exception handling
yieldTo end a function, returns a generator

How to see the list of keywords available in Python?

To see the list of available keywords, you can use the command as shown below:-

import keyword
print(keyword.kwlist)

What is an identifier in Python?

In Python, an identifier is a name used to identify a variable, function, class, module or other objects. Identifier begins with a letter a to z or A to Z or an underscore (_) trailed by zero or more letters, underscores, and digits (0 to 9).  An identifier is a name given to entities like class, functions, variables etc. in Python. It helps to differentiate one entity from another.

What are the naming conventions for Python identifiers?

    1. Identifier begins with a letter a to z or A to Z or an underscore (_) trailed by zero or more letters, underscores, and digits (0 to 9)
    2. We cannot use keywords as an identifier name
    3. An identifier cannot start with a digit. 5variable is an invalid identifier, however, digits can be added after the variable name.
    4. The use of special symbols like !, @, #, $, % etc. is forbidden in an identifier.
    5. An identifier can be of any length, however, it is always good to make it shorter to make it more readable.
    6. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
    7. Starting an identifier with a single leading underscore indicates that the identifier is private.
    8. Starting an identifier with two leading underscores indicates a strongly private identifier.
    9. If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

Python is a case-sensitive language. This means Variable and variable are not the same. Make sure you name them carefully.

Guidelines derived from Guido’s Recommendations

Here are the guidelines that is derived from Guido that helps you better obtain uniform programming guidelines for your python projects.

What is Python Coding Style?

Python coding style is defined by PEP-8 style which stands for Python Enhancement Proposal that provides a proper technical documentation and rules that is made standard in the Python community. Here are some of the guidelines that is provided in PEP-8.

  1. Use 4 spaces per indentation and no tabs.
  2. Do not mix tabs and spaces. Tabs create confusion and it is recommended to use only spaces.
  3. Maximum line length : 79 characters which help users with a small display.
  4. Use blank lines to separate top-level function and class definitions and single blank line to separate
  5. methods definitions inside a class and larger blocks of code inside functions.
  6. When possible, put inline comments (should be complete sentences).
  7. Use spaces around expressions and statements.

Do visit  https://www.python.org/dev/peps/pep-0008/ for PEP 8 — Style Guide for Python Code.

SHARE Python Keywords and Identifiers

You may also like...

Leave a Reply

Your email address will not be published.

Share