- They are used to define the syntax and structure of the Python language.
- In Python, keywords are case sensitive.
- There are 33 keywords in Python 3.7.
- All the keywords (except True, False and None) are in lowercase.
- Below is the list of keywords in Python 3.7
I will not be using just syntax to explain these keywords, rather I will use some simple yet enough code examples considering new plus experienced people. For those who are new to python, if you find it difficult to understand the complete code, just try to see the context and usage of keyword.
This keyword is used to represent a boolean true. If a statement is true, “True” is printed. True in python equals to 1.
This is a special constant used to denote a null value or a void. It is an object of its own datatype –NoneType.
This is a logical operator in python. Widely used in decision making with all conditions to match as in example below.
The truth table for “and” is depicted below.
This is a logical operator in python just like and. However, unlike and, it is used to match any condition from given conditions set.
The truth table for “or” is depicted below.
This logical operator inverts the truth value to its opposite value as in example 1 below also can be used for checking membership as in example 2.
The truth table for “not” is depicted below.
This function is used in debugging purposes to check the correctiveness of code. If a statement evaluated to true, nothing happens, but when it is false, “AssertionError” is raised.
This function is used to control the flow of loops. The break keyword is used to break out of current loop if condition is true. Else the next statement in loop is execute as shown below.
This function is also used to control the flow of code like break. However, this keyword skips the current iteration of the loop, but does not end the loop as in example below.
This keyword is used to declare user defined classes.
This keyword is used to declare user defined functions.
This keyword is a control statement for decision making. True expression forces control to go in “if” statement block. False expression forces control to go in next (elif or else) block.
It is a control statement for decision making. It is short for “else if”. Used along with if statement as shown below.
This keyword is a control statement for decision making. False expression forces control to go in “else” statement block. Used as if … else or if … elif … else.
“del” keyword is used to delete an object or it can be used to delete any value in any variable or list.
This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in “try” block is checked, if there is any type of error, except block is executed. for example check the finally keyword section.
This keyword works in conjunction with “try” to catch exceptions.
No matter what is result of the “try” block, block termed “finally” is always executed.
This keyword is used for exception handling to explicitly raise exceptions.
The for keyword is used to create a for loop. It can be used to iterate through a sequence, like a list, tuple, etc.
This keyword is used to control flow and looping. With the while loop we can execute a set of statements as long as a condition is true.
It is the null statement in python. Nothing happens when this is encountered. This is also used to prevent indentation errors and used as a placeholder in empty classes or functions.
This statement is used to import a particular module into current program.
Generally used with import, from is used to import particular functionality from the module imported.
This keyword is used to create the alias for the module imported. i.e giving a new name to the imported module.. E.g import math as mymath.
It is also known as anonymous function. This keyword is used to make inline returning functions with no statements allowed internally.
This keyword is used to return from the function.
This keyword is used like return statement but it returns a generator. The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run.
with statement in Python is used in exception handling to make the code cleaner and much more readable.
This keyword is used to check if a container contains a value. This keyword is also used to loop through the container.
This keyword is used to test object identity, i.e to check if both the objects take same memory location or not.
This keyword is used to define a variable inside the function with a global scope.
This keyword declares a variable to point to variable of outside enclosing function, in case of nested functions.