Python – Keywords and Identifiers

In this article we are going to discuss about keywords and identifiers in Python.

Keywords are prearranged and predefined words in python. Every keywords are written in lower case except True and False. If we count there are 33 keywords in python. Keywords are case sensitive. We can not create any function or identifiers with matching keyword name. I python these keywords are  made for particular purpose.

All Python Keywords are listed below:

1andIt is a logical operator. If both the operands are true it returns true otherwise false.
2OrIt is also a logical operator. Returns true if anyone operand is true otherwise return false.
3notThis is again a logical operator. Returns True if the operand is false else return false.
4if Conditional statement.
5elifElif is a condition statement used with if statement the elif statement is executed if the previous conditions were not true
6elseElse is used with if and elif conditional statement the else block is executed if the given condition is not true.
7forThis is created for a loop.
8whileThis keyword is used to create a while loop.
9breakThis is used to terminate the loop.
10asThis is used to create an alternative.
11defIt helps us to define functions.
12lambdaIt used to define the anonymous function.
13passThis is a null statement that means it will do nothing.
14returnIt will return a value and exit the function.
15TrueThis is a Boolean value.
16FalseThis is also a Boolean value.
17tryIt makes a try-except statement.
18withThe with keyword is used to simplify exception handling.
19assertThis function is used for debugging purposes. Usually used to check the correctness of code
20classIt helps us to define a class.
21continueIt continues to the next iteration of a loop
22delIt deletes a reference to an object.
23exceptUsed with exceptions, what to do when an exception occurs
24finallyFinally is use with exceptions, a block of code that will be executed no matter if there is an exception or not.
25fromThe form is used to import specific parts of any module.
26globalThis declares a global variable.
27importThis is used to import a module.
28inIt’s used to check if a value is present in a list, tuple, etc, or not.
29isThis is used to check if the two variables are equal or not.
30NoneThis is a special constant used to denote a null value or avoid. It’s important to remember, 0, any empty container(e.g empty list) do not compute to None
31nonlocalIt’s declared a non-local variable.
32raiseThis raises an exception
33yieldIt’s ends a function and returns a generator.

Python Identifiers

Python identifiers are generally used to recognize a variable, function, class etc. There are some rules which we should follow while choosing a name for identifier-

1.Identifier should start with a character or Underscore after that use digit.

2.Characters are A-Z or a-z, an Underscore ( _ ) , and digit (0-9).

  • For example, value_count, dataLoader etc. are some valid identifier names.

3.We should not use special characters ( #, @, $, %, ! ) in identifiers.

4.No limitation on the length of the identifier.

5.Identifiers are case sensitive, i.e., ‘define & ‘Define are two different identifiers in Python.

6.Avoid using two underscore while giving identifiers name  like __len__ or _load__

Conclusion:

In this article we have discussed all about keywords and identifiers in Python in details. Thank You!