Keywords in Python

Python Keywords are some predefined and reserved words in Python that have special meanings. Keywords are used to define the syntax of the coding. The keyword cannot be used as an identifier, function, or variable name. All the keywords in Python are written in lowercase except True and False. There are 35 keywords in Python 3.11.

In Python, there is an inbuilt keyword module that provides an iskeyword() function that can be used to check whether a given string is a valid keyword or not. Furthermore, we can check the name of the keywords in Python by using the kwlist attribute of the keyword module.

Python Keywords and Identifiers

Every language contains words and a set of rules that would make a sentence meaningful. Similarly, in Python programming language, there are a set of predefined words, called Keywords which along with Identifiers will form meaningful sentences when used together. Python keywords cannot be used as the names of variables, functions, and classes.

In this article, we will learn about Python keywords and identifiers and how to use them to perform some tasks.

Similar Reads

Keywords in Python

Python Keywords are some predefined and reserved words in Python that have special meanings. Keywords are used to define the syntax of the coding. The keyword cannot be used as an identifier, function, or variable name. All the keywords in Python are written in lowercase except True and False. There are 35 keywords in Python 3.11....

Rules for Keywords in Python

Python keywords cannot be used as identifiers. All the keywords in Python should be in lowercase except True and False....

List of Python Keywords

Keywords    Description and This is a logical operator which returns true if both the operands are true else returns false. or This is also a logical operator which returns true if anyone operand is true else returns false. not This is again a logical operator it returns True if the operand is false else returns false. if This is used to make a conditional statement. elif Elif is a condition statement used with an if statement. The elif statement is executed if the previous conditions were not true. else Else is used with if and elif conditional statements. The else block is executed if the given condition is not true. for This is used to create a loop. while This keyword is used to create a while loop. break This is used to terminate the loop. as This is used to create an alternative. def It helps us to define functions. lambda It is used to define the anonymous function. pass This is a null statement which means it will do nothing. return It will return a value and exit the function. True This is a boolean value. False This is also a boolean value. try It makes a try-except statement. with The with keyword is used to simplify exception handling. assert This function is used for debugging purposes. Usually used to check the correctness of code class It helps us to define a class. continue It continues to the next iteration of a loop del It deletes a reference to an object. except Used with exceptions, what to do when an exception occurs finally Finally is used with exceptions, a block of code that will be executed no matter if there is an exception or not. from It is used to import specific parts of any module. global This declares a global variable. import This is used to import a module. in It’s used to check whether a value is present in a list, range, tuple, etc. is This is used to check if the two variables are equal or not. none This 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 nonlocal It’s declared a non-local variable. raise This raises an exception. yield It ends a function and returns a generator. async It is used to create asynchronous coroutine. await It releases the flow of control back to the event loop....

Identifiers in Python

...

Rules for Naming Python Identifiers

Identifier is a user-defined name given to a variable, function, class, module, etc. The identifier is a combination of character digits and an underscore. They are case-sensitive i.e., ‘num’ and ‘Num’ and ‘NUM’ are three different identifiers in python. It is a good programming practice to give meaningful names to identifiers to make the code understandable....

Examples of Python Identifiers

It cannot be a reserved python keyword. It should not contain white space. It can be a combination of A-Z, a-z, 0-9, or underscore. It should start with an alphabet character or an underscore ( _ ). It should not contain any special character other than an underscore ( _ )....

Python Keywords and Identifiers Examples

Valid identifiers: var1 _var1 _1_var var_1 Invalid Identifiers !var1 1var 1_var var#1 var 1...

FAQs on Python Keywords and Identifiers

Example 1: Example of and, or, not, True, False keywords....