Python Keywords

Facebooktwitterredditpinterestlinkedinmailby feather

Python Keywords

Keywords are the reserved words that have special meanings to the compiler. They cannot be used as a variable. Keywords are the part of the syntax, for example:

Here return is a keyword, whereas, a and b are variables.

Below is the list of all keywords used in Python:

and else lambda
or except None
assert finally nonlocal
break try pass
continue raise global
class from return
def import False
del in True
if is for
elif not while
with yield  

Descriptionof Python keywords


Python if statement is the simplest conditional statement. It is used to decide whether a block of the statement will be executed or not.



An else statement is associated with if statement. When if statement’s condition is false then else block is executed. The following example makes it clear:



The elif is short for else-if. It is used for checking multiple conditions, which means that if the previous condition is false than check for the next condition. For example:



True keyword is used to represent a boolean true.  If the given statement is true then interpreter returns true. For example:



False Keyword is used to represent a boolean false. If the given statement is false then interpreter returns false. For example:



The assert is a keyword which is used as a debugging tool.It helps in a smooth flow of code. It helps us to find bugs more conveniently

Assertions are mainly assumptions that a programmer knows andalways wants it to be true. Hence puts them in code so that failure of that statement doesn’t allow the code to execute further.


Runtime Exception:


and, or, not are the logical operators in Python. In and operator, if both operands are true, then the condition will become true. For example:


The Truth table of and operator is given below:

A B A and B
True True True
True False False
False False False
False True False


or operator returns true if one of the statement is true


The truth table is following

A B A or B
True True True
True False True
False True True
False False False


not operator returns true if given statement is false


The truth table ofnot is given below:

A Not A
True False
False True


The class keyword is used to define a class in Python. Class is a collection of data (variables) and methods. The class can represent a real-world state. It is the main concept of OOPs (Object Oriented programming).


The del keyword is used to delete the reference of the object.


Python Keywords

In the above code, variable a is no longerusedbecause the reference of the variable is deleted.


The def keyword is used to define a function. A function is used to divide our program into smaller and modularsub-parts. It provides reusability to our program. As programs grow larger, the function makes it more manageable.


It is used inside the function to exit it and returns a value or none.



The pass keyword is used when we want to execute nothing or create a placeholder for future code, if you declare an empty function or class, you will get an error without using pass statement.

try, except

Exceptions are the runtime errors. Few examples of exception are ValueError, ZeroDivisior, NameError, TypeError, etc. try-except blocks are used to handle exceptions in Python.



The raise keyword allows to throws an exception at any time. For example:



The finally block will always be executed no matter whethertry-expect block raises an error or not. For example:



The import keyword is used to import modules into the current namespace.



The fromkeyword is used for a specific function or attributes into the current namespace.



as keyword is used to create an alias while importing a module. With the help ofas, we can provide a user-defined name while importing a module. For example: importing a Python module calendar using alias name as cal.




for is a keyword that is used for looping or iterating over a sequence (dictionary, list, string, set or tuple).

With for loop we can execute a set of statement once for each item in iterator(list, tuple, set, etc).



A while loop is executed until the given condition evaluates to false or break statement is encountered. It is also used to make an infinite loop.



The break keyword is used to terminate the loop, even the condition is true. The control of the program passes to the outer statement after the body of the break statement.



The continue keyword is used to stop the current iteration, and continues with the next statement



The with keyword is used in exception handling.

It makes code cleaner and much more readable. It simplifies the management of shared resources like file streams. For example:

An exception during the file.write() call in the above implementation can prevent the file from closing properly, which may lead to several bugs in the code.

Any changes in files do not reflect until the file is properly closed.

In the above code, there is no need to call file.close()while using withstatement.

Thus, with statement helps to avoid bugs and ensures that a resource is appropriately released.

The with statement is popularly used with file streams, as shown above, and with Locks, sockets, sub-processes, etc.


The keyword lambda is used to define an anonymous function (function with no name). It is an inline function. A standard function is defined by using def keyword.

Lambda function can have multiple arguments but only one expression. In the following example, lambda function evaluates square of 1 to 5.



When we declare a variable outside the function, it becomes global by default. If we want to access a global variable inside the function, we must use the global keyword.



The keyword nonlocal is related to the global keyword. It is used to work with variables inside a nested function (function inside a function).

If we want to modify the value of a non-local variable inside a nested function, then we must declare it with nonlocal.


Inner function:  30

Outer function:  30


The is keyword is used to test if two-variable refers to the same object or testing object identity. It returns true if they refer to thesame object and return false if they do not refer to the same object, even if the two objects are 100% the equal. For example:


In above code x and y refers to the same object.But in another example:

An empty list or dictionary is the same as another empty one. But they are not identical objects as they are stored separately in memory. It is because the list and dictionary are mutable which means value can be changed.


The None keyword is a special constant used to define a null value. It is an object of its own data type, the NoneType.

We must take special care that None does not indicate False, 0 or any empty list, dictionary, string, etc.


In the above program, return_void()function does not return a value although it performs multiplication of two number. It prints None because function has returned None automatically.


The yield statement prevents the function’s execution temporally and returnsvalues to the caller.

It can produce a sequence of values whereas return sends a specified value back to its caller.


Facebooktwitterredditpinterestlinkedinmailby feather