Tutorial

Keywords & Identifiers in Python

3 min read

There are certain groups of words in every language that, when combined, form a complete sentence. Identifiers and Keywords are similar terms used in computer languages while writing code.

So in this article, we are going to discuss two important Python programming elements, which are known as Keywords and Identifiers.

Keywords in Python

Keywords in Python are special predefined and reserved words that are used to perform a certain task.

Every programming language has them.

These cannot be used to declare a function, variable, or identifier.

These are very easy to understand and we do not need to import them into our code.

All keywords in Python are case-sensitive.

They are written in lowercase except for the True, False, and None keywords.

There are over 30 keywords in Python 3. Some of the important keywords in Python are:

  • and – a logical operator that returns true if both the operands are true or else returns false.
  • break – used to break from a loop.
  • continue – used to continue to the next iteration of a loop.
  • def – used to define a function.
  • elif – condition statement used for the else if condition.
  • else – conditional statement that is executed if the “if condition” is false.
  • except – Used in exceptions.
  • finally – used with exceptions to execute a block of code that will be executed no matter if there is an exception or not.
  • for – Used in for loop.
  • global – Used to declare a global variable.
  • if – a conditional statement that executes if the condition is true.
  • return – used to exit a function and return a value.
  • try – used to make a try-except statement.
  • while – used in the while loop.

There are several excellent Python IDEs available. Each of these will highlight certain terms in your code to make them stand out from other ones.

This will enable you to recognize Python keywords while you’re working rapidly and avoid using them improperly.

Identifiers in Python

Python identifiers are user-defined names given to variables, functions, classes, modules, or other entities.

Identifiers are case-sensitive in Python since it is a case-sensitive computer programming language.

Variable and identifier are sometimes mistakenly thought to be the same thing, however, they are separate.

Identifiers should have meaningful names to make the code more readable.

Also, we cannot use keywords as variable names as they are reserved built-in names in Python.

Rules to Name Identifiers:

  1. Identifiers are a combination of digits, letters, and underscore( _ ) that can be written in either uppercase or lowercase.
  2. Keywords are not allowed to be identifiers.
  3. Identifiers are not allowed to be starting with numbers or digits.
  4. Special symbols such as !, @, #, $, %, etc cannot be used.
  5. Identifiers are also case-sensitive and they are not allowed to have white spaces.

If any of the above-stated rules are not followed, then the compiler will return a SyntaxError.

Hope you like the tutorial on keywords and identifiers in Python. You might also like to learn Pyhtoh with the help of a tutor.