Site icon Virtual Maestro

Learning Python: part-4 Python Keywords

Advertisements
Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers
  • 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. 


False

This keyword is used to represent a boolean false. If a statement is false, “False” is printed. False in python equals to 0.


True:

This keyword is used to represent a boolean true. If a statement is true, “True” is printed. True in python equals to 1.



None:

This is a special constant used to denote a null value or a void. It is an object of its own datatype –NoneType. 



and

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.



or:

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.



not:

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.



assert:

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.




break: 



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.

continue:

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.

class:

This keyword is used to declare user defined classes.

def:

This keyword is used to declare user defined functions.

if:

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.

elif:

It is a control statement for decision making. It is short for “else if”. Used along with if statement as shown below.

else:

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:

“del” keyword is used to delete an object or it can be used to delete any value in any variable or list.

try:

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.

except:

This keyword works in conjunction with “try” to catch exceptions.

finally:

No matter what is result of the “try” block, block termed “finally” is always executed. 




raise:

This keyword is used for exception handling to explicitly raise exceptions.

for:

The for keyword is used to create a for loop. It can be used to iterate through a sequence, like a list, tuple, etc.

while:

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.

pass:

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.

import:

This statement is used to import a particular module into current program.

from:

Generally used with import, from is used to import particular functionality from the module imported.

as:

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.

lambda:

It is also known as anonymous function. This keyword is used to make inline returning functions with no statements allowed internally.

return:

This keyword is used to return from the function.

yield:

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:

with statement in Python is used in exception handling to make the code cleaner and much more readable.

in:

This keyword is used to check if a container contains a value. This keyword is also used to loop through the container.

is:

This keyword is used to test object identity, i.e to check if both the objects take same memory location or not.

global:

This keyword is used to define a variable inside the function with a global scope.

non-local:

This keyword declares a variable to point to variable of outside enclosing function, in case of nested functions.

Exit mobile version