Python Keywords

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

if

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

Output:

else

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:

Output

elif

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:

Output:

True

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

Output:

False

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

Output:

assert

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.

Output:

Runtime Exception:

and

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:

Output:

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

or operator returns true if one of the statement is true

Output:

The truth table is following

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

not

not operator returns true if given statement is false

Output:

The truth table ofnot is given below:

A Not A
True False
False True

class

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).

del

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

Output:

Python Keywords

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

def

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.

return

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

Output:

pass

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.

Output:

raise

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

Output:

finally

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

Output:

import

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

Output:

from

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

Output:

as

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.

Output:

January

for

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).

Output:

while

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.

Output:

break

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.

Output:

continue

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

Output:

with

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.

lambda

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.

Output:

global

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.

Output:

nonlocal

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.

Outside:

Inner function:  30

Outer function:  30

is

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:

Output:

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.

None

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.

Output:

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.

yield

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.

Output: